-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlinked_list_single.c
More file actions
200 lines (183 loc) · 3.88 KB
/
Copy pathlinked_list_single.c
File metadata and controls
200 lines (183 loc) · 3.88 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
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
#include<stdio.h>
#include<stdlib.h>
typedef struct list
{
int data;
struct list* link;
}node;
node *getnode();
node *ins_front(int,node*);
node *ins_rear(int,node*);
node *del_front(node*);
node *del_rear(node*);
node *del_specific(int,node*);
void display(node*);
int main()
{
node* first=NULL;
int ch;
for(;;)
{
printf("1.insert front\n");
printf("2.insert rear\n");
printf("3.delete front\n");
printf("4.delete rear\n");
printf("5.delete specific element\n");
printf("6.traverse\n");
printf("7.exit\n");
printf("choose : ");
scanf("%d",&ch);
int ele;
switch(ch)
{
case 1:
printf("Enter the element : ");
scanf("%d",&ele);
first=ins_front(ele,first);
break;
case 2:
printf("enter an element : ");
scanf("%d",&ele);
first=ins_rear(ele,first);
break;
case 3:
first=del_front(first);
break;
case 4:
first=del_rear(first);
break;
case 5:
printf("enter the data,whose node is to be deletd : ");
scanf("%d",&ele);
first=del_specific(ele,first);
case 6:
if(first==NULL)
printf("list is empty\n");
else
display(first);
break;
case 7:
printf("code terminated ...\n");
exit(0);
}
}
}
//this is a commnit
node *getnode()
{
node *temp=(node*)malloc(sizeof(node));
if(temp==NULL)
{
printf("no memmory allocated \n");
return NULL;
}
return temp;
}
node *ins_front(int ele,node* fir)
{
node *new=getnode();
new->data=ele;
new->link=NULL;
if(fir==NULL)
return new;
new->link=fir;
return new;
}
node *ins_rear(int ele,node* first)
{
node *new=getnode();
new->data=ele;
new->link=NULL;
if(first==NULL)
return new;
node* temp=first;
while(temp->link!=NULL)
temp=temp->link;
temp->link=new;
return first;
}
node* del_front(node* first)
{
if(first==NULL)
return NULL;
if(first->link==NULL)
{
printf("the deleted is %d",first->data);
free(first);
return NULL;
}
node *temp=first;
first=first->link;
printf("the deleted is %d",temp->data);
free(temp);
return first;
}
node* del_rear(node* first)
{
if(first==NULL)
return NULL;
if(first->link==NULL)
{
printf("delete is %d",first->data);
free(first);
return NULL;
}
node *prev=NULL,*cur;
cur=first;
while(cur->link!=NULL)
{
prev=cur;
cur=cur->link;
}
printf("the deleted is %d\n",cur->data);
free(cur);
prev->link=NULL;
return first;
}
node* del_specific(int ele,node *first)
{
if(first==NULL)
{
printf("List is empty");
return NULL;
}
if(first->data==ele)
{
node* temp=first;
printf("deleted is %d",temp->data);
first=first->link;
free(temp);
return first;
}
node* prev=NULL,*cur=first;
while(cur!=NULL)
{
if(cur->data==ele)
{
printf("deleted element = %d",cur->data);
prev->link=cur->link;
free(cur);
return first;
}
prev=cur;
cur=cur->link;
if(cur==NULL)
{
printf("element not found");
return first;
}
}
}
void display(node* f)
{
node* temp=f;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->link;
}
}
/*stack and queue operations are valid in singly linked list
insert front and delete front
insert rear and delete rear are stack operations*/
//hello hi how are you