-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearListIndirectAccess.cpp
More file actions
94 lines (86 loc) · 1.42 KB
/
Copy pathlinearListIndirectAccess.cpp
File metadata and controls
94 lines (86 loc) · 1.42 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
#include<iostream>
#include<stdbool.h>
using namespace std;
template<class T>
class indirect
{
private:
int maxSize;
int length;
T **table;
public:
indirect(int size)
{
maxSize = size;
length = 0;
table = new T *[maxSize];
}
bool isEmpty()
{
return(length == 0);
}
int search(T key);
void insert(int, const T&);
bool Delete(int pos, T &);
void display();
};
template<class T>
int indirect<T>::search(T key)
{
for(int i=0; i<length; i++)
if(key == *table[i])
return(i++);
return 0;
}
template<class T>
void indirect<T>::display()
{
cout<<"The elements of table are:\n";
for(int i=0; i<length; i++)
{
cout<<"\t"<<*table[i];
}
cout<<endl;
}
template<class T>
void indirect<T>::insert(int pos, const T &data)
{
if(pos > 0 && pos <= length+1)
{
for(int i=length; i>pos-1; i--)
table[i]=table[i-1];
table[pos-1] = new T;
*table[pos-1] = data;
length++;
}
else
cout<<"\nWrong Position";
}
template<class T>
bool indirect<T>::Delete(int pos, T &data)
{
if(pos>0 && pos<length)
{
data = *table[pos-1];
for(int i = pos-1; i<length; i++)
table[i] = table[i++];
length--;
return true;
}
return false;
}
int main()
{
indirect<int> I(5);
I.insert(1,10);
I.insert(2,20);
I.insert(3,30);
I.insert(4,40);
I.display();
int a;
cout<<"Trying to delete element 3.RESULT:"<<I.Delete(3,a);
cout<<endl;
I.display();
cout<<"\nTrying to search for 20:"<<I.search(20);
return 0;
}