-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDHEADLIS.C
169 lines (163 loc) · 3.15 KB
/
DHEADLIS.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
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
typedef struct listnode* listpointer;
struct listnode
{
int data;
listpointer next,prev;
};
listpointer head,node,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;
listpointer head=(listpointer)malloc(sizeof(*head));
head->next=head->prev=head;
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(head->next!=head)
{
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);
node=head;
for(i=0;i<listSize;i++)
{
node->next=(listpointer)malloc(sizeof(*node));
node->next->prev=node;
node=node->next;
scanf("%d",&node->data);
}
node->next=head;
head->prev=node;
printf("\nList created with %d elements.",listSize);
}
void list()
{
if(head->next==head)
{
printf("\nList is empty...");
return;
}
printf("\nElements in the list :\n\n");
for(node=head->next;node!=head;node=node->next)
printf(" %d",node->data);
}
void insert(int x,int loc)
{
int i;
if(loc<1 || loc>listSize+1)
{
printf("\nInvalid location...");
return;
}
node=(listpointer)malloc(sizeof(*node));
node->data=x;
p=head;
if(loc<=listSize/2)
for(i=0;i!=loc-1;i++,p=p->next)
;
else
for(i=listSize+1;i!=loc-1;i--,p=p->prev)
;
node->next=p->next;
node->prev=p;
p->next=node;
node->next->prev=node;
listSize++;
printf("\nOne element inserted...");
}
void delv(int x)
{
if(head->next==head)
{
printf("\nList is empty...");
return;
}
for(node=head->next;node!=head && node->data!=x;node=node->next)
;
if(node==head)
{
printf("\nElement not found...");
return;
}
node->prev->next=node->next;
node->next->prev=node->prev;
free(node);
listSize--;
printf("\nOne element deleted...");
}
void delp(int loc)
{
int i;
if(head->next==head)
{
printf("\nList is empty...");
return;
}
if(loc<1 || loc>listSize)
{
printf("\nInvalid location...");
return;
}
if(loc<=listSize/2)
for(i=1,node=head->next;i!=loc;i++,node=node->next)
;
else
for(i=listSize,node=head->prev;i!=loc;i--,node=node->prev)
;
node->prev->next=node->next;
node->next->prev=node->prev;
free(node);
listSize--;
printf("\nOne element deleted...");
}