-
Notifications
You must be signed in to change notification settings - Fork 8
/
LINKEDLI.C
184 lines (178 loc) · 3.05 KB
/
LINKEDLI.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
180
181
182
183
184
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
typedef struct listnode* listpointer;
struct listnode
{
int data;
listpointer next;
};
listpointer fnode=NULL,p;
int listSize=0;
void create();
void list();
void insert(int,int);
void delv(int);
void delp(int);
void main()
{
int choice;
int n,pos;
do
{
clrscr();
printf("\n\tLINKED LIST OPERATIONS\n");
printf("\n\t\t1. CREATE");
printf("\n\t\t2. LIST");
printf("\n\t\t3. INSERT");
printf("\n\t\t4. DELETE BY VALUE");
printf("\n\t\t5. DELETE BY POSITION");
printf("\n\t\t6. EXIT");
printf("\n\n\tEnter your choice(1-6) : ");
scanf("%d",&choice);
switch(choice)
{
case 1:create();
break;
case 2:list();
break;
case 3:printf("\nEnter the data : ");
scanf("%d",&n);
printf("Enter the position(1-%d) : ",listSize+1);
scanf("%d",&pos);
insert(n,pos);
break;
case 4:printf("\nEnter the data : ");
scanf("%d",&n);
delv(n);
break;
case 5:printf("\nEnter the position(1-%d) : ",listSize);
scanf("%d",&pos);
delp(pos);
break;
default:continue;
}
getch();
}while(choice!=6);
}
void create()
{
int i;
if(fnode!=NULL)
{
printf("\nList already created...");
return;
}
printf("\nEnter the number of elements : ");
scanf("%d",&listSize);
if(listSize<1) return;
printf("\nEnter %d elements into list :\n",listSize);
fnode=(listpointer)malloc(sizeof(*fnode));
scanf("%d",&fnode->data);
fnode->next=NULL;
p=fnode;
for(i=0;i<listSize-1;i++)
{
p->next=(listpointer)malloc(sizeof(*p));
p=p->next;
scanf("%d",&p->data);
}
p->next=NULL;
printf("\nList created and %d elements inserted into the list",listSize);
}
void list()
{
if(fnode==NULL)
{
printf("\nList is empty...");
return;
}
printf("\nElements in the list :\n\n");
for(p=fnode;p!=NULL;p=p->next)
printf(" %d",p->data);
}
void insert(int x,int loc)
{
int i;
listpointer node;
if(loc<1 || loc>listSize+1)
{
printf("\nInvalid location...");
return;
}
node=(listpointer)malloc(sizeof(*node));
node->data=x;
if(loc==1)
{
node->next=fnode;
fnode=node;
}
else
{
for(i=1,p=fnode;i!=loc-1;i++,p=p->next)
;
node->next=p->next;
p->next=node;
}
listSize++;
printf("\nOne element inserted...");
}
void delv(int x)
{
listpointer d;
if(fnode==NULL)
{
printf("\nList is empty...");
return;
}
if(fnode->data==x)
{
d=fnode;
fnode=fnode->next;
}
else
{
for(p=fnode;p->next!=NULL && p->next->data!=x;p=p->next)
;
if(p->next==NULL)
{
printf("\nElement not found...");
return;
}
d=p->next;
p->next=d->next;
}
free(d);
listSize--;
printf("\nOne element deleted...");
}
void delp(int loc)
{
int i;
listpointer d;
if(fnode==NULL)
{
printf("\nList is empty...");
return;
}
if(loc<1 || loc>listSize)
{
printf("\nInvalid location...");
return;
}
if(loc==1)
{
d=fnode;
fnode=fnode->next;
}
else
{
for(i=1,p=fnode;i!=loc-1;i++,p=p->next)
;
d=p->next;
p->next=d->next;
}
free(d);
listSize--;
printf("\nOne element deleted...");
}