Write a program to implement the Simple Queue using Linked List.
/*
File: DSPrgm09.c
Author: Aditya Saini
Date: Nov 12, 2019
Description: Program to implement the Simple Queue using Linked List.
*/
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
//Structure definition for linked list
typedef struct node
{
int data;
struct node *next;
}node;
node *start = NULL;
int count = 0;
void enqueue (void);
void dequeue (void);
void display (void);
//Main function
int main (void)
{
int choice;
//Menu
while (1)
{
printf ("\t***Menu***\n");
printf ("1. Enqueue\n");
printf ("2. Dequeue\n");
printf ("3. Display\n");
printf ("4. Exit\n");
printf ("\nChoice: ");
scanf ("%d", &choice);
switch (choice)
{
case 1:
enqueue ( );
break;
case 2:
dequeue ( );
break;
case 3:
display ( );
break;
case 4:
return 0;
default:
printf ("\nError! Wrong Choice. \n");
}
}
return 0;
};
//Function to enqueue
void enqueue (void)
{
node *temp, *end;
if (count > SIZE)
{
printf ("Error! Queue Full\n\n");
return;
}
temp = (node *) malloc (sizeof (node));
if (temp == NULL)
{
printf ("Error! Memory Full\n\n");
return;
}
printf ("Input element to insert: ");
scanf ("%d", &temp -> data);
temp ->next = NULL;
if (start == NULL)
start = temp;
else
{
end = start;
while (end -> next != NULL)
end = end -> next;
end -> next = temp;
}
count++;
printf ("\n");
};
//Function to dequeue
void dequeue (void)
{
node *temp;
if (count < 1)
{
printf ("Error! Empty Queue\n\n");
return;
}
temp = start;
start = start -> next;
printf ("%d deleted\n\n", temp -> data);
free (temp);
count--;
};
//Function to display the elements in the queue
void display (void)
{
node *temp;
if (count < 1)
{
printf ("Error! Empty Queue\n\n");
return;
}
temp = start;
printf ("Queue: ");
while (temp != NULL)
{
printf (" %d ", temp -> data);
temp = temp -> next;
}
printf ("\n\n");
};
File: DSPrgm09.c
Author: Aditya Saini
Date: Nov 12, 2019
Description: Program to implement the Simple Queue using Linked List.
*/
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20
//Structure definition for linked list
typedef struct node
{
int data;
struct node *next;
}node;
node *start = NULL;
int count = 0;
void enqueue (void);
void dequeue (void);
void display (void);
//Main function
int main (void)
{
int choice;
//Menu
while (1)
{
printf ("\t***Menu***\n");
printf ("1. Enqueue\n");
printf ("2. Dequeue\n");
printf ("3. Display\n");
printf ("4. Exit\n");
printf ("\nChoice: ");
scanf ("%d", &choice);
switch (choice)
{
case 1:
enqueue ( );
break;
case 2:
dequeue ( );
break;
case 3:
display ( );
break;
case 4:
return 0;
default:
printf ("\nError! Wrong Choice. \n");
}
}
return 0;
};
//Function to enqueue
void enqueue (void)
{
node *temp, *end;
if (count > SIZE)
{
printf ("Error! Queue Full\n\n");
return;
}
temp = (node *) malloc (sizeof (node));
if (temp == NULL)
{
printf ("Error! Memory Full\n\n");
return;
}
printf ("Input element to insert: ");
scanf ("%d", &temp -> data);
temp ->next = NULL;
if (start == NULL)
start = temp;
else
{
end = start;
while (end -> next != NULL)
end = end -> next;
end -> next = temp;
}
count++;
printf ("\n");
};
//Function to dequeue
void dequeue (void)
{
node *temp;
if (count < 1)
{
printf ("Error! Empty Queue\n\n");
return;
}
temp = start;
start = start -> next;
printf ("%d deleted\n\n", temp -> data);
free (temp);
count--;
};
//Function to display the elements in the queue
void display (void)
{
node *temp;
if (count < 1)
{
printf ("Error! Empty Queue\n\n");
return;
}
temp = start;
printf ("Queue: ");
while (temp != NULL)
{
printf (" %d ", temp -> data);
temp = temp -> next;
}
printf ("\n\n");
};
Output
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 9
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 2
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 3
Queue: 9 2 1 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 2
9 deleted
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 2
2 deleted
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 3
Input element to insert: 1 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 4
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 9
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 2
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 1
Input element to insert: 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 3
Queue: 9 2 1 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 2
9 deleted
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 2
2 deleted
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 3
Input element to insert: 1 1
***Menu***
1. Enqueue
2. Dequeue
3. Display
4. Exit
Choice: 4
No comments:
Post a Comment
Please do not post spam links.