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

implementing queue using sinlge linked list code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

implementing queue using sinlge linked list code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

//implementing queue using sinlge linked list

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

}struct node *front=NULL, *temp ,*rear;

int n; //number of nodes

void create();

void display();

void enqueue();

void dequeue();

void peek();

void main()

printf("enter the n value:\n");

scanf("%d",&n);

int a;

printf("(enter 1 to create\nenter 2 to display\nenter 3 to enqueue\nenter 4 to dequeue\nenter 5


to peek\nenter 6 to exit)\n\nenter any number:\n ");

scanf("%d",&a);

switch(a)

case 1:create();

break;

case 2:display();

break;

case 3:enqueue();
break;

case 4:dequeue();

break;

case 5:peek();

break;

case 6:break;

default:break;

//creating a queue using linked list

void create()

temp=front;

int i;

for(i=1;i<=n;i++)

struct node *newnode;

newnode=(struct node *)malloc(sizeof(struct node));

printf("enter the element:\n");

scanf("%d",&newnode->data);

newnode->next=NULL;

if(front == NULL)

front=newnode;

else

temp->next=newnode;

temp=temp->next;

}
//for finding the rear

rear = temp;

display();

//displaying elements

void display()

if(front==NULL)

printf("Queue is empty\n");

create();

else

temp=front;

while(temp!=NULL)

printf("%d\n",temp->data);

temp=temp->next;

int a;

printf("(enter 3 to enqueue\nenter 4 to dequeue\nenter 5 to peek\nenter 6 to exit)\n\nenter any


number:\n ");

scanf("%d",&a);

switch(a)

case 3:enqueue();

break;

case 4:dequeue();

break;
case 5:peek();

break;

case 6:break;

default:break;

//inserting elements

void enqueue()

temp=rear;

if(front==NULL)

printf("Queue is empty\n");

create();

else

struct node *newnode;

newnode=(struct node *)malloc(sizeof(struct node));

printf("enter the element:\n");

scanf("%d",&newnode->data);

newnode->next=NULL;

//deleting elements

void dequeue()

You might also like