-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinglyll.c
178 lines (170 loc) · 2.45 KB
/
singlyll.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
#include<stdio.h>
#include<stdlib.h>
void insert();
void insert_beg();
void insert_end();
void insert_pos();
void del_beg();
void del_end();
void del_pos();
void display();
struct node
{
int data;
struct node*link;
};
struct node *head,*new,*ptr,*ptr1,*temp;
int item,pos,op,ch,i;
void insert()
{
new=(struct node*) malloc (sizeof(struct node));
printf("Enter the data to be stored\n");
scanf("%d",&item);
new->data=item;
new->link=NULL;
}
void insert_beg()
{
insert();
if(head==NULL)
{
head=new;
}
else
{
new->link=head;
head=new;
}
}
void insert_end()
{
insert();
if(head==NULL)
{
head=new;
}
else
{
ptr=head;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
ptr->link=new;
}
}
void insert_pos()
{
printf("Enter the position to which item to be inserted\n");
scanf("%d",&pos);
if(pos==1)
{
insert_beg();
}
else
{
insert();
for(i=1;i<pos-1;i++)
{
ptr=ptr->link;
}
new->link=ptr->link;
ptr->link=new;
}
}
void del_beg()
{
if(head==NULL)
{
printf("The LL is empty\nDeletion not possible\n");
}
else
{
temp=head;
printf("The deleted item is %d\n",temp->data);
head=temp->link;
free(temp);
}
}
void del_end()
{
if(head==NULL)
{
printf("The LL is empty\nDeletion not possible\n");
}
else
{
ptr=head;
while(ptr->link!=NULL)
{
ptr1=ptr;
ptr=ptr->link;
}
temp=ptr;
printf("The deleted item is %d",temp->data);
ptr1->link=NULL;
free(temp);
}
}
void del_pos()
{
printf("Enter the position of item to be deleted\n");
scanf("%d",&pos);
if(pos==1)
{
del_beg();
}
else
{
ptr=head;
for(i=1;i<pos-1;i++)
{
ptr1=ptr;
ptr=ptr->link;
}
temp=ptr;
printf("The deleted item is %d ",temp->data);
ptr1->link=temp->link;
free(temp);
}
}
void display()
{
printf("The current LL is \n");
ptr=head;
while(ptr->link!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->link;
}
printf("%d\t",ptr->data);
}
void main()
{
do{
printf("1--Insert at beg\t2--Insert at end\t3--Insert at pos\n4--Deletion at beg\t5--Deletion at end\t6--Deletion from pos\n7--Display\n");
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_beg();
break;
case 2: insert_end();
break;
case 3: insert_pos();
break;
case 4: del_beg();
break;
case 5: del_end();
break;
case 6: del_pos();
break;
case 7:display();
break;
}
printf("\n");
printf("DO YOU WANT TO CONTINUE?\n");
printf("1--YES\t2--NO\n");
scanf("%d",&op);
}while(op==1);
}