-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraybasedlist .txt
More file actions
123 lines (76 loc) · 1.67 KB
/
arraybasedlist .txt
File metadata and controls
123 lines (76 loc) · 1.67 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
#include <iostream>
using namespace std;
const int max = 10;
class Chain
{
public:
Chain() :size(0)
{
}
void printall();
void delete_(int k, int&x, bool&success);//delete element at position with index k and save it in x;
void insert(int k, int x, bool &success);//insert element x at position with index k
//void swap(int);
private:
int list[max];
int size;
};
Chain mylist;
bool flag;
int y;
void Chain::insert(int k, int x, bool &success)
{
if (size < max&& k<=size)
{
for (int i =size-1; i >= k; i--)
{
list[i + 1] = list[i];
}
success = true;//set sucess to be true
list[k] = x;//insert the item into the list
size++;//increase the size
cout << " the size is" << size << endl;
}
}
void Chain::printall()
{
for (int i = 0; i <size; i++)
{
cout << list[i] << " ";
}
cout << endl;
}
void Chain::delete_(int k,int &x,bool & success)
{
if (k <= size&&k>=0&&size>0)
{
success = true;
x = list[k];
for (int i = k; i < size; i++)
{
list[i] = list[i + 1];
}
size--;
}
cout << " the size is" << size << endl;
}
void main()
{
bool success = false;
int x = NULL;
mylist.insert(0, 2, success);
mylist.insert(1, 5, success);
mylist.insert(2, 8, success);
mylist.insert(3, 10, success);
mylist.insert(4, 12, success);
mylist.insert(5, 18,success);
mylist.insert(6, 21, success);
mylist.insert(2, 6, success);
mylist.insert(9, 25, success);
mylist.printall();
// 2 5 6 8 10 12 18 21 25
mylist.delete_(0, x, success);
mylist.delete_(8, x ,success);
mylist.printall();
// 5 6 8 10 12 18 21
}