Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Queue Operations Using Linkedlist

Uploaded by

shaikofficial66
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Queue Operations Using Linkedlist

Uploaded by

shaikofficial66
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Write a program to implement Queue operations using Linked List

#include<stdio.h>
struct node
{
int data;
struct node *next;
};
struct node *front = NULL;
struct node *rear = NULL;
void insertque(int);
void deleteque();
void display();
void main()
{
int choice, value;
clrscr();
while(choice!=4)
{
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to insert: ");
scanf("%d", &value);
insertque(value);
break;
case 2: deleteque();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("\nInvalid choice\n");
break;
}
}
}
void insertque(int value)
{
struct node *newNode;
newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = value;
newNode->next = NULL;
if(front == NULL && rear == NULL)
{
front = rear = newNode;
}
else
{
rear->next = newNode;
rear = newNode;
}
printf("\nElement inserted!\n");
}
void deleteque()
{
if(front == NULL && rear == NULL)
{
printf("\nQueue is Empty!\n");
}
else
{
struct node *temp;
temp = front;
if(front == rear)
{
front=rear=NULL;
printf("\nDeleted element is: %d\n", temp->data);
free(temp);
}
else
{
front = front -> next;
printf("\nDeleted element is: %d\n", temp->data);
free(temp);
}
}
}

void display()
{
if(front == NULL && rear == NULL)
{
printf("\nQueue is Empty!\n");
}
else
{
struct node *ptr;
ptr = front;
printf("\nQueue elements are:\n");
while(ptr!=NULL)
{
printf("%d--->",ptr->data);
ptr = ptr->next;
}
}
}

You might also like