Queue Using Linked List
This commit is contained in:
@@ -31,4 +31,5 @@
|
|||||||
|29 | BinarySearch.c | BinarySearch.exe | WAP to search an element in an array using Binary Search |
|
|29 | BinarySearch.c | BinarySearch.exe | WAP to search an element in an array using Binary Search |
|
||||||
|30 | SelectionSort.c | SelectionSort.exe | WAP to sort an array using selection sort |
|
|30 | SelectionSort.c | SelectionSort.exe | WAP to sort an array using selection sort |
|
||||||
|31 | StackUsingLinkedList.c | StackUsingLinkedList.exe | WAP to Implement a Stack using Linked List |
|
|31 | StackUsingLinkedList.c | StackUsingLinkedList.exe | WAP to Implement a Stack using Linked List |
|
||||||
|32 | SinglyLinkedList.c | SinglyLinkedList.exe | WAP to implement singly linked lists |
|
|32 | SinglyLinkedList.c | SinglyLinkedList.exe | WAP to implement singly linked lists |
|
||||||
|
|33 | QueueUsingLinkedList.c | QueueUsingLinkedList.exe | WAP to implement a Queue using Linked List |
|
||||||
106
QueueUsingLinkedList.c
Normal file
106
QueueUsingLinkedList.c
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
// WAP to implement Queue using linked list.
|
||||||
|
|
||||||
|
#include<stdio.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
|
||||||
|
struct node
|
||||||
|
{
|
||||||
|
int data;
|
||||||
|
struct node *next;
|
||||||
|
};
|
||||||
|
struct node *front;
|
||||||
|
struct node *rear;
|
||||||
|
void insert();
|
||||||
|
void delete();
|
||||||
|
void display();
|
||||||
|
void main ()
|
||||||
|
{
|
||||||
|
int choice;
|
||||||
|
while(choice != 4)
|
||||||
|
{
|
||||||
|
printf("\n*************************Main Menu*****************************\n");
|
||||||
|
printf("\n=================================================================\n");
|
||||||
|
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
|
||||||
|
printf("\nEnter your choice ?");
|
||||||
|
scanf("%d",& choice);
|
||||||
|
switch(choice)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
insert();
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
delete();
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
display();
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
exit(0);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("\nEnter valid choice??\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void insert()
|
||||||
|
{
|
||||||
|
struct node *ptr;
|
||||||
|
int item;
|
||||||
|
ptr = (struct node *) malloc (sizeof(struct node));
|
||||||
|
if(ptr == NULL)
|
||||||
|
{
|
||||||
|
printf("\nOVERFLOW\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("\nEnter value?\n");
|
||||||
|
scanf("%d",&item);
|
||||||
|
ptr -> data = item;
|
||||||
|
if(front == NULL)
|
||||||
|
{
|
||||||
|
front = ptr;
|
||||||
|
rear = ptr;
|
||||||
|
front -> next = NULL;
|
||||||
|
rear -> next = NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rear -> next = ptr;
|
||||||
|
rear = ptr;
|
||||||
|
rear->next = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void delete ()
|
||||||
|
{
|
||||||
|
struct node *ptr;
|
||||||
|
if(front == NULL)
|
||||||
|
{
|
||||||
|
printf("\nUNDERFLOW\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ptr = front;
|
||||||
|
front = front -> next;
|
||||||
|
free(ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void display()
|
||||||
|
{
|
||||||
|
struct node *ptr;
|
||||||
|
ptr = front;
|
||||||
|
if(front == NULL)
|
||||||
|
{
|
||||||
|
printf("\nEmpty queue\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ printf("\nprinting values .....\n");
|
||||||
|
while(ptr != NULL)
|
||||||
|
{
|
||||||
|
printf("\n%d\n",ptr -> data);
|
||||||
|
ptr = ptr -> next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
QueueUsingLinkedList.exe
Normal file
BIN
QueueUsingLinkedList.exe
Normal file
Binary file not shown.
Reference in New Issue
Block a user