-
Notifications
You must be signed in to change notification settings - Fork 0
/
singly_linked_list.cpp
212 lines (197 loc) · 4.42 KB
/
singly_linked_list.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include<stdio.h>
#include<stdlib.h>
#define NULL_VALUE -99999
#define SUCCESS_VALUE 99999
struct listNode{
int item;
struct listNode * next;
};
struct listNode * list;
void initializeList(){
list = 0; //initially set to NULL
}
int insertItem(int item) //insert at the beginning of the linked list
{
struct listNode * newNode ;
newNode = (struct listNode*) malloc (sizeof(struct listNode)) ;
newNode->item = item ;
newNode->next = list ; //point to previous first node
list = newNode ; //set list to point to newnode as this is now the first node
return SUCCESS_VALUE ;
}
int deleteItem(int item){
struct listNode *temp, *prev ;
temp = list ; //start at the beginning
while (temp != 0){
if (temp->item == item) break ;
prev = temp;
temp = temp->next ; //move to next node
}
if (temp == 0) return NULL_VALUE ; //item not found to delete
if (temp == list) //delete the first node
{
list = list->next ;
free(temp) ;
}
else
{
prev->next = temp->next ;
free(temp);
}
return SUCCESS_VALUE ;
}
struct listNode * searchItem(int item){
struct listNode * temp ;
temp = list ; //start at the beginning
while (temp != 0){
if (temp->item == item) return temp ;
temp = temp->next ; //move to next node
}
free(temp);
return 0 ; //0 means invalid pointer in C, also called NULL value in C
}
void printList(){
struct listNode * temp;
temp = list;
while(temp!=0){
printf("%d->", temp->item);
temp = temp->next;
}
printf("\n");
free(temp);
}
int insertLast(int item){
struct listNode * temp ;
struct listNode *newNode;
newNode = (struct listNode*) malloc (sizeof(struct listNode)) ;
newNode->item=item;
if(list==0){
newNode->next=0;
list=newNode;
return SUCCESS_VALUE;
}
temp = list;
while(temp->next!=0)
{
temp = temp->next;
}
temp->next=newNode;
newNode->next=0;
return SUCCESS_VALUE;
}
int insertBefore(int oldItem, int newItem){
struct listNode * temp,*prev;
struct listNode * newNode;
newNode = (struct listNode*) malloc (sizeof(struct listNode)) ;
newNode->item = newItem ;
if(list==0){
newNode->next=list;
list=newNode;
return SUCCESS_VALUE;
}
temp = list;
while(temp!=0&&temp->item!=oldItem)
{
prev=temp;
temp = temp->next;
}
if(temp==list){
newNode->next=list;
list=newNode;
}
else if(temp==0){
newNode->next=list;
list=newNode;
}
else{
prev->next=newNode;
newNode->next=temp;
}
return SUCCESS_VALUE;
}
int deleteAfter(int item){
if(list==0) return NULL_VALUE;
struct listNode *temp;
temp = list ; //start at the beginning
while (temp!=0&&temp->item != item){
temp = temp->next ; //move to next node
}
if (temp == 0||temp->next==0){
return NULL_VALUE ;
} //item not found to delete
else
{
temp->next = temp->next->next ;
free(temp->next);
}
return SUCCESS_VALUE ;
}
int deleteLast(){
struct listNode *temp,*prev;
if(list==0) return NULL_VALUE;
temp=list;
while(temp->next!=0) {
prev=temp;
temp=temp->next;
}
if(temp==list){
free(temp);
list=0;
return SUCCESS_VALUE;
}
prev->next=0;
free(temp);
return SUCCESS_VALUE;
}
int main(void){
initializeList();
while(1){
printf("1. Insert new item. 2. Delete item. 3. Search item. \n");
printf("4. Insert Last 5. Insert Before(Old,New)\n");
printf("6. Delete After(item) 7. Delete Last\n");
printf("8. Print. 9. exit.\n");
int ch;
scanf("%d",&ch);
if(ch==1){
int item;
scanf("%d", &item);
insertItem(item);
}
else if(ch==2){
int item;
scanf("%d", &item);
deleteItem(item);
}
else if(ch==3){
int item;
scanf("%d", &item);
struct listNode * res = searchItem(item);
if(res!=0) printf("Found.\n");
else printf("Not found.\n");
}
else if(ch==4){
int item;
scanf("%d", &item);
insertLast(item);
}
else if(ch==5){
int old,New;
scanf("%d%d", &old,&New);
insertBefore(old,New);
}
else if(ch==6){
int item;
scanf("%d", &item);
deleteAfter(item);
}
else if(ch==7){
deleteLast();
}
else if(ch==8){
printList();
}
else if(ch==9){
break;
}
}
}