-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueUsingSLL.c
More file actions
112 lines (105 loc) · 2.18 KB
/
QueueUsingSLL.c
File metadata and controls
112 lines (105 loc) · 2.18 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
#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 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 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 rear end 2.Delete at front end \n");
printf(" 3.Display 4.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_rear(item,first);
break;
case 2:first=delete_front(first);
break;
case 3:display(first);
break;
default:exit(0);
}//end of switch
}//end of for loop
}//end of main