Nothing Special   »   [go: up one dir, main page]

Queue Using Linked List 8

Download as pdf or txt
Download as pdf or txt
You are on page 1of 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