-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleLinkedList.c
More file actions
149 lines (140 loc) · 2.66 KB
/
Copy pathDoubleLinkedList.c
File metadata and controls
149 lines (140 loc) · 2.66 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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int item;
struct node *llink;
struct node *rlink;
};
typedef struct node *NODE;
NODE first=NULL;
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
return x;
}
NODE create_new()
{
NODE temp;
temp=getnode();
printf("\n\tEnter ITEM :: ");
scanf("%d",&(temp->item));
temp->llink=NULL;
temp->rlink=NULL;
return temp;
}
void insert_front()
{
NODE temp;
temp = create_new();
if(first == NULL)
first = temp;
else
{
temp->rlink = first;
first->llink = temp;
first = temp;
}
}
void insert_end()
{
NODE temp,cur;
temp = create_new();
if(first == NULL)
first = temp;
else
{
cur = first;
while(cur->rlink !=NULL)
{ cur = cur->rlink;}
cur->rlink = temp;
temp->llink = cur;
}
}
void delete_front()
{
NODE temp=first;
if(first==NULL)
printf("\n\tEMPTY");
else if(first->rlink==NULL)
{
printf("Element deleted is %d\n",first->item);
free(temp);
first = NULL;
}
else
{
printf("Element deleted is %d\n",first->item);
first=temp->rlink;
first->llink=NULL;
free(temp);
}
}
void delete_end()
{
NODE cur;
if(first==NULL)
printf("\n\tEMPTY");
else if(first->rlink==NULL)
{
printf("Element deleted is %d\n",first->item);
free(first);
first = NULL;
}
else
{
cur=first;
while(cur->rlink!=NULL)
{
cur=cur->rlink;
}
printf("Element deleted is %d",cur->item);
cur->llink->rlink=NULL;
free(cur);
cur=NULL;
}
}
void display()
{
if(first==NULL)
printf("\n\tLINKED LIST EMPTY\n");
else if(first->rlink==NULL)
printf("\nDoubly Linked List is :: %d",first->item);
else
{
NODE cur = first;
printf("\nDoubly Linked List is :: ");
while(cur->rlink!=NULL)
{
printf(" %d ",cur->item);
cur=cur->rlink;
}
printf(" %d ",cur->item);
}
}
void main()
{int option;
while(1)
{ option=0;
printf("\nDouble Linked List\n---------------------");
printf("\n1. Insert at front");
printf("\n2. Insert at end");
printf("\n3. Delete at front");
printf("\n4. Delete at end");
printf("\n5. Display list");
printf("\n6. Exit");
printf("\nEnter Choice :: ");
scanf("%d",&option);
switch(option)
{
case 1 : insert_front();break;
case 2 : insert_end();break;
case 3 : delete_front();break;
case 4 : delete_end();break;
case 5 : display();break;
case 6 : exit(0);
default: printf("Invalid Choice");
}
}
}