-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDequeueUsingSLL.c
More file actions
156 lines (148 loc) · 3.27 KB
/
DequeueUsingSLL.c
File metadata and controls
156 lines (148 loc) · 3.27 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
150
151
152
153
154
155
156
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
//#include<alloc.h>
struct node
{
int info;
struct node *link;
};
typedef struct node *NODE;
// c function to get a node from the available list
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf(" OUT OF MEMORY \n");
exit(0);
}
return x;
}
// c function to deallocate or free a node
void freenode(NODE x)
{
free(x);
}
// c function to insert a node at the front end of the list
NODE insert_front(int item,NODE first)
{
NODE temp;
temp=getnode();
temp->info=item;
temp->link=first;
return temp;
}
// c function to insert an item at the rear end of the list
NODE insert_rear(int item,NODE first)
{
NODE temp,cur;
temp=getnode();
temp->info=item;
temp->link=NULL;
if(first==NULL)
{
return temp;
}
cur=first;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=temp;
return first;
}
// c function to delete a node/item from the front end
NODE delete_front(NODE first)
{
NODE temp;
if(first==NULL)
{
printf(" List is empty and cannot be deleted \n");
return;
}
temp=first;
temp=temp->link;
printf(" Item deleted=%d\n",first->info);
freenode(first);
return temp;
}
// c function to delete an item from the rear end of the list
NODE delete_rear(NODE first)
{
NODE cur,prev;
if(first==NULL)
{
printf(" List is empty and cannot be deleted\n");
return first;
}
if(first->link=NULL)
{
printf(" Item to be deleted is %d\n",first->info);
freenode(first);
return NULL; //list is empty
}
prev=NULL;
cur=first;
while(cur->link!=NULL)
{
prev=cur;
cur=cur->link;
}
printf(" Item deleted is %d\n",cur->info);
freenode(cur);
prev->link=NULL;
return first;
}
// c function to display the contents of singly linked list
void display(NODE first)
{
NODE temp;
if(first==NULL)
{
printf(" List is empty \n");
return;
}
printf(" The contents of the list are : \n");
temp=first;
while(temp!=NULL)
{
printf("%d\n",temp->info);
temp=temp->link;
}
printf("\n");
}
// main
void main()
{
NODE first=NULL; //to start,list is empty
int choice,item;
int q[5];
for(;;)
{
printf(" 1.Insert at front end 2.Insert at rear end \n");
printf(" 3.Delete at front end 4.Delete at rear end \n");
printf(" 5.Display 6.Quit \n");
printf(" Enter choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf(" Enter item to be inserted\n");
scanf("%d",&item);
first=insert_front(item,first);
break;
case 2:printf(" Enter item to be inserted\n");
scanf("%d",&item);
first=insert_rear(item,first);
break;
case 3:first=delete_front(first);
break;
case 4:first=delete_rear(first);
break;
case 5:display(first);
break;
default:exit(0);
}//end of switch
}//end of for loop
}//end of main