- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.cpp
More file actions
Latest commit
146 lines (133 loc) · 2.5 KB
/
Copy pathArray.cpp
File metadata and controls
146 lines (133 loc) · 2.5 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include<stdio.h>
#include<iostream>
usingnamespacestd;
classArray
{
private:
int capacity;
int lastIndex;
int *p;
public:
Array(int);
boolis_empty();
boolis_full();
voidappend(int);
voidinsert(int,int);
voidedit(int,int);
voiddel(int);
intgetItem(int);
intcount();
intgetElementIndex(int);
~Array();
};
Array::Array(int)
{
p=NULL;
}
Array::Array (int size)
{
capacity = size;
lastIndex = -1;
if(p != NULL)
delete []p;
p = newint[capacity];
}
boolArray::is_empty()
{
return (lastIndex == -1);
}
boolArray::is_full()
{
return (lastIndex+1 == capacity);
}
voidArray::append(int data)
{
if(!is_full())
{
lastIndex++;
p[lastIndex]=data;
}
else
cout<<"Overflow: appending not possible";
}
voidArray::insert(int index, int data)
{
int i;
try{
if(index<0 || index > lastIndex+1)
throw1;
if(is_full())
throw2;
for(i = lastIndex; i>=index; i--)
p[i+1] = p[i];
p[index] = data;
lastIndex++;
}
catch(int e){
if(e==1)
cout<<"Invalid Index";
elseif(e==2)
cout<<"Array is full, so insert of new element is not possible.";
}
}
voidArray::edit(int index, int data)
{
try{
if(index<0 || index > lastIndex+1)
throw1;
p[index] = data;
}
catch(int e)
{
if(e==1)
cout<<"Invalid Index";
}
}
voidArray::del(int index)
{
int i;
try{
if(index < 0 || index > capacity)
throw1;
for(i = index; i < lastIndex; i++)
p[i] = p[i+1];
lastIndex--;
}
catch(int e){
if(e==1)
cout<<"Invalid Index";
}
}
intArray::getItem(int index)
{
try{
if(index < 0 || index > lastIndex)
throw1;
return p[index];
}
catch(int e){
if(e==1)
cout<<"Invalid Index";
}
return -1;
}
intArray::count()
{
return lastIndex+1;
}
intArray::getElementIndex(int element)
{
int i;
for(i=0; i<=lastIndex; i++)
{
if(p[i] == element)
return i;
else
return -1;
}
}
Array::~Array()
{
delete []p;
// program end here
}