-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrayDSA_revision.cpp
99 lines (83 loc) · 1.21 KB
/
arrayDSA_revision.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
#include<string.h>
using namespace std;
#define INVALID_ARRAY 1
#define EMPTY_ARRAY 2
#define INVALID_INDEX 3
class Array
{
private:
int capacity;
int lastIndex;
int *ptr;
public:
Array(int);
bool isEmpty();
void append(int);
void insert(int,int);
void edit(int,int);
void delete(int);
bool isFull();
int get(int);
int count();
~Array();
int find(int);
};
Array::Array(int capacity)
{
this->capacity=capacity;
lastIndex=-1;
ptr=new int[capacity];
}
bool Array::isEmpty()
{
return lastIndex==-1;
}
void Array::append(int data)
{
if(lastIndex<capacity-1)
{
lastIndex++;
ptr[lastIndex]=data;
}
}
void Array::insert(int index,int data)
{
for(int i=lastIndex;i>=index;i++)
ptr[i+1]=ptr[i];
ptr[i]=data;
lastIndex++;
}
void Array::edit(int index,int data)
{
ptr[index]=data;
}
void Array::delete(int index)
{
for(int i=index;i<lastIndex;i++)
ptr[i]=ptr[i+1];
lastIndex--;
}
bool Array::isFull()
{
return lastIndex==capacity-1;
}
int Array::get(int index)
{
return ptr[index];
}
int Array::count()
{
return lastIndex+1;
}
Array::~Array()
{
delete []ptr;
}
Array::find(int data)
{
for(int i=0;i<=lastIndex;i++)
if(ptr[i]==data)
return i;
return -1;
}