0% found this document useful (0 votes)
57 views4 pages

Queue Using Linked List 8

The document describes implementing a queue using a linked list with functions for enqueue and display. The enqueue function inserts an element into the queue and returns an error if the queue is full. The display function prints all elements currently in the queue. Code snippets are provided for a main function that tests the queue functions by allowing the user to enqueue elements and display the queue.

Uploaded by

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

Queue Using Linked List 8

The document describes implementing a queue using a linked list with functions for enqueue and display. The enqueue function inserts an element into the queue and returns an error if the queue is full. The display function prints all elements currently in the queue. Code snippets are provided for a main function that tests the queue functions by allowing the user to enqueue elements and display the queue.

Uploaded by

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

S.No: 8 Exp.

Name: Implementation of Queue using Linked List - enqueue() and display() Date:

I D: 20K61A0561     Page No:            


Aim:
Implementation of queue using linked list

Fil l the missing code in function enqueue(int ele) and display() in the below code.

The function enqueue(int ele) inserts an element into the queue and it gives "Queue is overflow." error if the maximum
capacity of the queue is reached.

The function display() prints al l the elements of the queue.

Note: Do use printf() with '\n' at the end of display().


Source Code:

QueueListMain1.c

#include <stdlib.h>

#include <stdio.h>

#include "QueueListEnqueueDisplay.c"

int main() {

int op, x;

while(1) {
printf("1.Enqueue 2.Display 3.Exit\n");

printf("Enter your option : ");

scanf("%d",&op);

switch(op) {

case 1:

printf("Enter element : ");

scanf("%d",&x);

enqueue(x);

break;

case 2:

display();

break;

case 3:

exit(0);
}
Sasi Institute of Technology and Engineering (Autonomous)
}

QueueListEnqueueDisplay.c

struct queue {

int data;

struct queue *next;

};

typedef struct queue *Q;

Q front = NULL, rear = NULL;

void enqueue(int);

void enqueue(int ele)

I D: 20K61A0561     Page No:            


struct queue*temp;

temp=(struct queue*)malloc(sizeof(struct queue));

if(temp==NULL)

printf("Queue is overflow.\n");

else

temp->data=ele;

if(front==NULL)

front=temp;
rear=temp;

front->next=NULL;

rear->next=NULL;

else

rear->next=temp;

rear=temp;

rear->next=NULL;

printf("Successfully inserted.\n");

void display();
void display()

struct queue*temp;

temp=front;

if(front==NULL)

printf("Queue is empty.\n");

else

printf("Elements in the queue : ");

while(temp!=NULL)

Sasi Institute of Technology and Engineering (Autonomous)

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

temp=temp->next;

printf("\n");

Execution Results - Al l test cases have succeeded!

Test Case - 1

User Output
Test Case - 1
1.Enqueue 2.Display 3.Exit 1

I D: 20K61A0561     Page No:            


Enter your option : 1
Enter element : 1
Successfully inserted. 2
1.Enqueue 2.Display 3.Exit 2
Enter your option : 2
Elements in the queue : 1 1
1.Enqueue 2.Display 3.Exit 1
Enter your option : 1
Enter element : 3
Successfully inserted. 2
1.Enqueue 2.Display 3.Exit 2
Enter your option : 2
Elements in the queue : 1 3 3
1.Enqueue 2.Display 3.Exit 3
Enter your option : 3

Test Case - 2

User Output
1.Enqueue 2.Display 3.Exit 1
Enter your option : 1
Enter element : 2
Successfully inserted. 1
1.Enqueue 2.Display 3.Exit 1
Enter your option : 1
Enter element : 1
Successfully inserted. 1
1.Enqueue 2.Display 3.Exit 1
Enter your option : 1
Enter element : 3
Successfully inserted. 2
1.Enqueue 2.Display 3.Exit 2
Enter your option : 2
Elements in the queue : 2 1 3 1
1.Enqueue 2.Display 3.Exit 1
Enter your option : 1 Sasi Institute of Technology and Engineering (Autonomous)
Enter element : 2
Successfully inserted. 3
1.Enqueue 2.Display 3.Exit 3
Enter your option : 3

Test Case - 3

User Output
1.Enqueue 2.Display 3.Exit 1
Enter your option : 1
Enter element : 1
Successfully inserted. 1
1.Enqueue 2.Display 3.Exit 1
Test Case - 3
Enter your option : 1

I D: 20K61A0561     Page No:            


Enter element : 3
Successfully inserted. 1
1.Enqueue 2.Display 3.Exit 1
Enter your option : 1
Enter element : 2
Successfully inserted. 2
1.Enqueue 2.Display 3.Exit 2
Enter your option : 2
Elements in the queue : 1 3 2 3
1.Enqueue 2.Display 3.Exit 3
Enter your option : 3

Sasi Institute of Technology and Engineering (Autonomous)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy