-
Notifications
You must be signed in to change notification settings - Fork 0
/
SINGLY_LLIST.c
164 lines (153 loc) · 2.88 KB
/
SINGLY_LLIST.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*newnd, *first=NULL, *last=NULL,*temp, *ptr;
struct node* create(int d)
{
newnd=((struct node*)malloc(sizeof(struct node)));
newnd->data=d;
newnd->next=NULL;
return newnd;
}
void firstt()
{
int v;
printf("\nEnter value: ");
scanf("%d", &v);
newnd=create(v);
if(first==NULL && last==NULL)
{
first=last=newnd;
first->next=NULL;
last->next=NULL;
}
else
{
temp=first;
first=newnd;
first->next=temp;
}
}
void lastt()
{
int v;
printf("\nEnter value: ");
scanf("%d", &v);
newnd=create(v);
last=first;
if(first==NULL && last==NULL)
{
first=last=newnd;
first->next=NULL;
last->next=NULL;
}
else
{
while(last->next != NULL)
last=last->next;
last->next=newnd;
last=newnd;
last->next=NULL;
}
}
void pos()
{
int p;
struct node *prev;
ptr=first;
printf("Enter position: ");
scanf("%d",&p);
int c;
for(c=1;c<p;c++)
{
prev=ptr;
ptr=ptr->next;
}
int v;
printf("\nEnter value: ");
scanf("%d", &v);
newnd=create(v);
prev->next=newnd;
newnd->next=ptr;
}
void del()
{
int p;
struct node *prev;
ptr=first;
printf("Enter position: ");
scanf("%d",&p);
int c=1;
for(c=1;c<p;c++)
{
prev=ptr;
ptr=ptr->next;
}
if(ptr == first)
{
first=first->next;
free(ptr);
}
else if(ptr == last)
{
last=prev;
last->next=NULL;
free(ptr);
}
else
{
prev->next=ptr->next;
free(ptr);
}
}
struct node* reverse(struct node* nd)
{
if (nd == NULL)
return NULL;
if (nd->next == NULL) {
first = nd;
return nd;
}
struct node* nd1 = reverse(nd->next);
nd1->next = nd;
nd->next = NULL;
return nd;
}
void display()
{
ptr=first;
printf("\nLinked List: ");
while(ptr!=NULL)
{
printf("%d ", ptr->data);
ptr=ptr->next;
}
}
int main()
{
int ch;
while(1)
{
printf("\n\n1->INSERT FROM FRONT\n2-> INSERT FROM END\n3->INSERT AT POS\n4-> DELETE FORM POS\n5-> DISPLAY\n6-> REVERSE\n7-> EXIT\n\nEnter: ");
scanf("%d",&ch);
system("cls");
if(ch==1)
firstt();
else if(ch==2)
lastt();
else if(ch==3)
pos();
else if(ch==4)
del();
else if(ch==5)
display();
else if(ch==6)
reverse(first);
else
exit(0);
}
return 0;
}