-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.cpp
142 lines (128 loc) · 2.54 KB
/
LinkedList.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
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
#include <iostream>
using namespace std;
template <class t>
struct node {
t data;
node <t> *next;
node (){
next = 0;
}
node (t item){
data = item;
next = 0;
}
};
template <class t>
void printLinkedList (node <t> *test);
template <class t>
bool AddFirst (node <t> *&test , t item);
template <class t>
bool DelFirst (node <t> *&test );
template <class t>
bool AddLast (node <t> *&test , t item);
template <class t>
bool DelLast (node <t> *&test );
template <class t>
bool DelAny (node <t> *&test , t item);
bool AddInOrder (node <int> *&test , int item);
int main(int argc, char *argv[])
{
node <int> *head = new node <int> (10) ;
AddLast(head , rand()%50);
AddLast(head , rand()%50);
AddLast(head , 13);
AddLast(head , rand()%50);
AddLast(head , 4);
printLinkedList(head);
DelAny (head ,44);
printLinkedList(head);
return 0;
}
template <class t>
bool DelAny (node <t> *&test , t item){
if (test == 0)return 0;
node <t> *follow ,*pre ;
follow = pre = test ;
while (follow!=0 && follow->data!=item){
pre=follow;
follow=follow->next;
}
if (follow ==NULL)return 0;
if (pre==follow)
test = follow->next;
else
pre->next=follow->next;
delete follow;
return 1;
}
template <class t>
bool DelLast (node <t> *&test ){
if (test == 0)return 0;
node <int> *follow ,*pre ;
follow = test ;
pre =0;
while (follow->next!=0){
pre =follow ;
follow = follow->next;
}
if (pre ==NULL)
test= NULL;
else
pre->next =NULL;
delete follow;
return 1;
}
template <class t>
bool DelFirst (node <t> *&test ){
if (test == 0)return 0;
node <t> *temp = test ;
test=test->next;
delete temp;
return 1;
}
bool AddInOrder (node <int> *&test , int item){
node <int> *temp = new node <int> (item);
if (temp == NULL)return 0;
node <int> *follow ,*pre ;
follow = test ;
pre =0;
while (follow!=0 && follow->data <item){
pre = follow ;
follow = follow ->next;
}
temp->next= follow;
if (pre == NULL)
test= temp;
else{
pre->next = temp;
}
return 1;
}
template <class t>
bool AddLast (node <t> *&test , t item){
node <t> *temp = new node <t> (item);
if (temp == 0)return 0;
node <t> *follow = test ;
while (follow->next != NULL){
follow = follow->next ;
}
follow->next = temp;
return 1;
}
template <class t>
bool AddFirst (node <t> *&test , t item){
node <t> *temp = new node <t> (item);
if (temp == 0)return 0;
temp->next = test;
test = temp;
return 1;
}
template <class t>
void printLinkedList (node <t> *test){
node <t> *temp = test ;
while (temp!= 0){
cout<<temp->data<<" | ";
temp = temp->next;
}
cout<<endl;
}