-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayRep.cpp
More file actions
172 lines (150 loc) · 2.77 KB
/
arrayRep.cpp
File metadata and controls
172 lines (150 loc) · 2.77 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//Array formula based representation
#include<iostream>
#include<stdlib.h>
#include<stdbool.h>
using namespace std;
template<class T> //class template
class linearList
{
private:
T *element;
int maxSize;
int n; //number of elements
public:
linearList(int msize = 10);
~linearList();
int getMaxSize();
bool isEmpty();
T find(int pos);
int search(T x);
void insert(int pos, T x);
T deleteIt(int pos);
void display();
};//class linearList
//creation
template<class T>
linearList<T>::linearList(int msize)
{
maxSize = msize;
n = 0;
element = new T[maxSize];
}
//destruction
template<class T>
linearList<T>::~linearList()
{
delete[] element; //tocheck;
}
//check for empty
template<class T>
bool linearList<T>::isEmpty()
{
return(n==0);
}
//Find the element
template<class T>
T linearList<T>::find(int pos)
{
if(pos > n)
return -1; //check return type
else
return(element[pos-1]);
}
//search
template<class T>
int linearList<T>::search(T x)
{
for(int i=0; i<n; i++)
{
if(element[i] == x)
return(i+1);
}
return -1;
}
//insert
template<class T>
void linearList<T>::insert(int pos, T x)
{
if(n==maxSize)
cout<<"list is full\n";
else if(pos>0 && n < maxSize)
{
for(int i=n; i>pos-1; i--)
element[i]=element[i-1];
element[pos-1]=x;
n++;
}
else
cout<<"\nsorry, wrong position\n";
}
//delete an element
template<class T>
T linearList<T>::deleteIt(int pos)
{
if(pos<n)
{
T ele = element[pos-1]; //deleted item
for(int i = pos-1; i<n; i++)
{
element[i] = element[i+1];
}
n--;
return ele;
}
else
{
cout<<"\nsorry! wrong pos\n";
return -1;
}
}
//display
template<class T>
void linearList<T>::display()
{
cout<<"\nThe elements are:\n";
for(int i=0; i<n; i++)
{
cout<<element[i]<<"\t";
}
}
//main
template<class T>
int linearList<T>::getMaxSize()
{
return maxSize;
}
int main()
{
for(int i=0; i<40; i++)
cout<<"*";
cout<<"\ntesting with an integer\n";
linearList<int> l1(5);
cout<<"\nchecking for empty:"<<l1.isEmpty();
for(int j=0;j<=l1.getMaxSize();j++)
{
l1.insert(j,j);
}
l1.display();
cout<<"\nchecking for empty:"<<l1.isEmpty();
cout<<"\n Trying to find:"<<l1.find(2);
cout<<"\ndoing a search for 5 in list:"<<l1.search(5);
cout<<"\ndeleting:"<<l1.deleteIt(3);
l1.display();
linearList<float> l2;
cout<<"\ntesting with a float\n";
cout<<"\nchecking for empty:"<<l2.isEmpty();
l2.insert(1,2.3);
l2.insert(2,3.3);
l2.insert(3,4.3);
l2.display();
cout<<"\nchecking for empty:"<<l2.isEmpty();
cout<<"\n Trying to find:"<<l2.find(2.3);
cout<<"\ndoing a search for 5 in list:"<<l2.search(5.3);
cout<<"\ndeleting:"<<l2.deleteIt(3.3);
l2.display();
cout<<"\nTESTS SUCCESFULL\n";
for(int i=0; i<40; i++)
cout<<"*";
cout<<endl;
return 0;
}