#include <stdlib.h>
struct node {
int data;
struct node *link;
};
struct node *temp,*front, *rear; /* Global Declarations */
void Insert(int);
int Delete();
void Display();
main() {
/* Main Program */
int opn, elem;
front = rear = NULL;
do {
clrscr();
printf("\n1. Insertion \n2. Deletion\n3. Display\n4. Exit\n");
printf("\nEnter your option : ");
scanf("%d", &opn);
switch (opn) {
case 1:
clrscr();
printf("\nEnter Item : ");
scanf("%d", &elem);
Insert(elem);
break;
case 2:
clrscr();
elem = Delete();
if (elem != -1)
printf(" Deleted Node(From Front)with the Data: %d\n", elem);
break;
case 3:
clrscr();
printf("Linked List Implementation of Queue: Status:\n");
Display();
break;
case 4:
clrscr();
printf("\n\n Terminating \n\n");
break;
default:
printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
getch();
} while (opn != 4);
}
void Insert(int info)
{
temp = (struct node *) malloc(sizeof(struct node));
temp->data = info;
temp->link = NULL;
if (front == NULL)
{
front = rear = temp;
}
else {
rear->link = temp;
rear = temp;
}
}
int Delete() {
int info;
// struct node *t;
if (front == NULL) {
printf(" Underflow!!!");
return -1;
} else {
temp = front;
info = front->data;
if (front == rear)
rear = NULL;
front = front->link;
temp->link = NULL;
free(temp);
return (info);
}
}
void Display()
{
if (front == NULL)
printf("Empty Queue\n");
else
{
temp = front;
while (temp)
{
printf("%d\n", temp->data);
temp = temp -> link;
}
}
}
OUTPUT
1. Insertion
2. Deletion
3. Display
4. Exit
Enter Your Choice : 1
Enter Item : 10
1. Insertion
2. Deletion
3. Display
4. Exit
Enter Your Choice : 1
Enter Item : 20
1. Insertion
2. Deletion
3. Display
4. Exit
Enter Your Choice : 1
Enter Item : 30
1. Insertion
2. Deletion
3. Display
4. Exit
Enter Your Choice : 1
Enter Item : 40
1. Insertion
2. Deletion
3. Display
4. Exit
Enter Your Choice : 1
Enter Item : 50
1. Insertion
2. Deletion
3. Display
4. Exit
Enter Your Choice : 3
Linked List Implementation of Queue: Status:
10
20
30
40
50
1. Insertion
2. Deletion
3. Display
4. Exit
Enter Your Choice : 2
Deleted Node(From Front)with the Data: 10
1. Insertion
2. Deletion
3. Display
4. Exit
Enter Your Choice : 2
Deleted Node(From Front)with the Data: 20
1. Insertion
2. Deletion
3. Display
4. Exit
Enter Your Choice : 2
Deleted Node(From Front)with the Data: 30
1. Insertion
2. Deletion
3. Display
4. Exit
Enter Your Choice : 2
Deleted Node(From Front)with the Data: 40
1. Insertion
2. Deletion
3. Display
4. Exit
Enter Your Choice : 2
Deleted Node(From Front)with the Data: 50
1. Insertion
2. Deletion
3. Display
4. Exit
Enter Your Choice : 2
OUEUE EMPTY
Copyright ©
All Notes on BCA