forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinglyLinkedList.c
179 lines (141 loc) · 3.52 KB
/
SinglyLinkedList.c
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
#include <stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *START;
void createEmptyList(){
START = NULL;
}
void insertAtBeggining(int item){
NODE *newNode;
newNode =(NODE*) malloc(sizeof(NODE));
newNode->data = item;
if(START==(NODE*)NULL){
START = newNode;
START->next = (NODE*)NULL;
return;
}
newNode->next = START;
START = newNode;
}
void insertAtEnd(int item){
NODE *newNode;
newNode =(NODE*) malloc(sizeof(NODE));
newNode->data=item;
newNode->next = NULL;
if(START==NULL){
START=newNode;
return;
}
NODE *temp = START;
while(temp->next!=NULL){
temp = temp->next;
}
temp->next = newNode;
}
void insertAtSpe(int item, int pos){
if(START==NULL){
printf("No such position found...\n");
return;
}
NODE *temp = START;
for(int i=1; i<pos-1; i++){
temp = temp->next;
if(temp==(NODE*)NULL){
printf("No such position found....");
return;
}
}
NODE *newNode;
newNode = (NODE*)malloc(sizeof(NODE));
newNode->data = item;
newNode->next = temp->next;
temp->next = newNode;
}
void traverse(){
NODE *temp = START;
if(START==(NODE*)NULL){
printf("List is empty....");
return;
}
while(temp!=(NODE*)NULL){
printf("%d\n",temp->data);
temp = temp->next;
}
}
void delete(int item){
if(item==START->data){
NODE *temp = START;
START= START->next;
free(temp);
return;
}
NODE *temp;
temp = START;
while(temp->next->data!=item){
temp = temp->next;
if(temp->next == NULL){
printf("No such data found....");
return;
}
}
NODE *p = temp->next;
temp->next = temp->next->next;
free(p);
}
void search(int item){
NODE *temp = START;
int i=1;
while(temp->data!=item){
if(temp->next==NULL){
printf("data not found\n");
return;
}
temp=temp->next;
i++;
}
printf("%d found in position %d\n",item,i);
}
int main(){
int choice , item , pos;
createEmptyList();
while(1){
printf("What do you want to perform?\n1.Insertion at Beginning\n2.Traverse\n3.Insertion at End\n4.Insertion at Specified Position\n5.Delete data\n6.Searching data\n7.Exit\nEnter your choice:\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter data to insert : ");
scanf("%d",&item);
insertAtBeggining(item);
break;
case 2:
traverse();
break;
case 3:
printf("Enter data to insert: ");
scanf("%d",&item);
insertAtEnd(item);
break;
case 4:
printf("Enter data to insert and position: ");
scanf("%d%d",&item,&pos);
insertAtSpe(item,pos);
break;
case 5: printf("Enter data to delete :");
scanf("%d",&item);
delete(item);
break;
case 6:
printf("Enter data to search: \n");
scanf("%d",&item);
search(item);
break;
case 7: return 0;
default:
printf("Wrong choice\n");
}
}
}