Sei sulla pagina 1di 14

Faculty of Engineering, Science and Technology (FEST)

Indus University, Karachi

Lab 2: Introduction and Implementation of Queue

Queue:
A queue is a linear list of elements in which deletion of an element can take place only at
one end called the front and insertion can take place on the other end which is termed as
the rear. The term front and rear are frequently used while describing queues in a linked list.

In the concept of a queue, the first element to be inserted in the queue will be the first
element to be deleted or removed from the list. So Queue is said to follow the FIFO (First In
First Out) structure. A real-life scenario in the form of example for queue will be the queue
of people waiting to accomplish a particular task where the first person in the queue is the
first person to be served first.

Other examples can also be noted within a computer system where the queue of tasks
arranged in the list to perform for the line printer, for accessing the disk storage, or even in
the time-sharing system for the use of CPU. So basically queue is used within a single
program where there are multiple programs kept in the queue or one task may create other
tasks which must have to be executed in turn by keeping them in the queue.

Student Name: Rimsha Pervaiz_1307-2018 Page |1


Instructor: Miss Faiza Sumbul
Faculty of Engineering, Science and Technology (FEST)
Indus University, Karachi

Queue as an ADT (Abstract Data Type)


The meaning of an abstract data type clearly says that for a data structure to be abstract, it
should have the below-mentioned characteristics:

 First, there should be a particular way in which components are related to each other
 Second, a statement for the operation that can be performed on elements of abstract data
type must have to be specified

Thus for defining a Queue as an abstract data type, these are the following criteria:

 Initialize a queue to be empty


 Check whether a queue is empty or not
 Check whether a queue is full or not
 Insert a new element after the last element in a queue, if the queue is not full
 Delete the first element in a queue, if it is not empty

Representation of Queue as an Array:


Queue is a linear data structure can be represented by using arrays. Here is a program
showing the implementation of a queue using an array.

Example:
#include <iostream>

using namespace std;

int queue[100], n = 100, front = - 1, rear = - 1;

void Insert() {

int val;

if (rear == n - 1)

cout<<"Queue Overflow"<<endl;

else {

if (front == - 1)

front = 0;

Student Name: Rimsha Pervaiz_1307-2018 Page |2


Instructor: Miss Faiza Sumbul
Faculty of Engineering, Science and Technology (FEST)
Indus University, Karachi

cout<<"Insert the element in queue : "<<endl;

cin>>val;

rear++;

queue[rear] = val;

void Delete() {

if (front == - 1 || front > rear) {

cout<<"Queue Underflow ";

return ;

} else {

cout<<"Element deleted from queue is : "<< queue[front] <<endl;

front++;;

void Display() {

if (front == - 1)

cout<<"Queue is empty"<<endl;

else {

cout<<"Queue elements are : ";

for (int i = front; i <= rear; i++)

cout<<queue[i]<<" ";

cout<<endl;

Student Name: Rimsha Pervaiz_1307-2018 Page |3


Instructor: Miss Faiza Sumbul
Faculty of Engineering, Science and Technology (FEST)
Indus University, Karachi

int main() {

int ch;

cout<<"1) Insert element to queue"<<endl;

cout<<"2) Delete element from queue"<<endl;

cout<<"3) Display all the elements of queue"<<endl;

cout<<"4) Exit"<<endl;

do {

cout<<"Enter your choice : "<<endl;

cin<<ch;

switch (ch) {

case 1: Insert();

break;

case 2: Delete();

break;

case 3: Display();

break;

case 4: cout<<"Exit"<<endl;

break;

default: cout<<"Invalid choice"<<endl;

} while(ch!=4);

return 0;

Student Name: Rimsha Pervaiz_1307-2018 Page |4


Instructor: Miss Faiza Sumbul
Faculty of Engineering, Science and Technology (FEST)
Indus University, Karachi

The output of the above program is as follows:

1) Insert element to queue


2) Delete element from queue
3) Display all the elements of queue
4) Exit
Enter your choice : 1
Insert the element in queue : 4
Enter your choice : 1
Insert the element in queue : 3
Enter your choice : 1
Insert the element in queue : 5
Enter your choice : 2
Element deleted from queue is : 4
Enter your choice : 3
Queue elements are : 3 5
Enter your choice : 7
Invalid choice
Enter your choice : 4
Exit

In the above program, the function Insert() inserts an element into the queue. If the rear
is equal to n-1, then the queue is full and overflow is displayed. If front is -1, it is
incremented by 1. Then rear is incremented by 1 and the element is inserted in index of
rear. This is shown below −

void Insert() {

int val;

if (rear == n - 1)

cout<<"Queue Overflow"<<endl;

else {

if (front == - 1)

front = 0;

cout<<"Insert the element in queue : "<<endl;

cin>>val;

rear++;

Student Name: Rimsha Pervaiz_1307-2018 Page |5


Instructor: Miss Faiza Sumbul
Faculty of Engineering, Science and Technology (FEST)
Indus University, Karachi

queue[rear] = val;

In the function Delete(), if there are no elements in the queue then it is underflow
condition. Otherwise the element at front is displayed and front is incremented by one.
This is shown below −

void Delete() {

if (front == - 1 || front > rear) {

cout<<"Queue Underflow ";

return ;

else {

cout<<"Element deleted from queue is : "<< queue[front] <<endl;

front++;;

In the function display(), if front is -1 then queue is empty. Otherwise all the queue
elements are displayed using a for loop. This is shown below −

void Display() {

if (front == - 1)

cout<<"Queue is empty"<<endl;

else {

cout<<"Queue elements are : ";

for (int i = front; i <= rear; i++)

cout<<queue[i]<<" ";

Student Name: Rimsha Pervaiz_1307-2018 Page |6


Instructor: Miss Faiza Sumbul
Faculty of Engineering, Science and Technology (FEST)
Indus University, Karachi

cout<<endl;

The function main() provides a choice to the user if they want to insert, delete or display
the queue. According to the user response, the appropriate function is called using
switch. If the user enters an invalid response, then that is printed. The code snippet for
this is given below −

int main() {

int ch;

cout<<"1) Insert element to queue"<<endl;

cout<<"2) Delete element from queue"<<endl;

cout<<"3) Display all the elements of queue"<<endl;

cout<<"4) Exit"<<endl;

do {

cout<<"Enter your choice : "<<endl;

cin>>ch;

switch (ch) {

case 1: Insert();

break;

case 2: Delete();

break;

case 3: Display();

break;

case 4: cout<<"Exit"<<endl;

break;

default: cout<<"Invalid choice"<<endl;

Student Name: Rimsha Pervaiz_1307-2018 Page |7


Instructor: Miss Faiza Sumbul
Faculty of Engineering, Science and Technology (FEST)
Indus University, Karachi

} while(ch!=4);

return 0;

Tasks:
1. Write a Program to Implement a Queue using an Array.
2. Write a Program to Implement Queue Data Structure using Linked list .

Student Name: Rimsha Pervaiz_1307-2018 Page |8


Instructor: Miss Faiza Sumbul
Faculty of Engineering, Science and Technology (FEST)
Indus University, Karachi

Task#1
Write a Program to Implement a Queue using an Array.

Program:

#include <stdio.h>
#include <stdlib.h>

int MAX=50;
void insert();
void Delete();
void display();
int queue_array[50];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
Delete();
break;
case 3:
display();
break;
case 4:
exit(EXIT_FAILURE);
default:
printf("Wrong choice \n");
}
}
}

Student Name: Rimsha Pervaiz_1307-2018 Page |9


Instructor: Miss Faiza Sumbul
Faculty of Engineering, Science and Technology (FEST)
Indus University, Karachi

void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
}

void Delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element Deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
} Output:
}

void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}

Student Name: Rimsha Pervaiz_1307-2018 P a g e | 10


Instructor: Miss Faiza Sumbul
Faculty of Engineering, Science and Technology (FEST)
Indus University, Karachi

Task#2
Write a Program to Implement Queue Data Structure using Linked list.

Program:

#include <stdio.h>
#include <stdlib.h>

struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;

int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();

int count = 0;

int main()
{
int no, ch, e;

printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue size");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);

Student Name: Rimsha Pervaiz_1307-2018 P a g e | 11


Instructor: Miss Faiza Sumbul
Faculty of Engineering, Science and Technology (FEST)
Indus University, Karachi

enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
return 0;
}

void create()
{
front = rear = NULL;
}

void queuesize()
{
printf("\n Queue size : %d", count);
}

void enq(int data)


{

Student Name: Rimsha Pervaiz_1307-2018 P a g e | 12


Instructor: Miss Faiza Sumbul
Faculty of Engineering, Science and Technology (FEST)
Indus University, Karachi

if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
}
else
{
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;

rear = temp;
}
count++;
}

void display()
{
front1 = front;

if ((front1 == NULL) && (rear == NULL))


{
printf("Queue is empty");
return;
}
while (front1 != rear)
{
printf("%d ", front1->info);
front1 = front1->ptr;
}
if (front1 == rear)
printf("%d", front1->info);
}

void deq()
{
front1 = front;

if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");

Student Name: Rimsha Pervaiz_1307-2018 P a g e | 13


Instructor: Miss Faiza Sumbul
Faculty of Engineering, Science and Technology (FEST)
Indus University, Karachi

return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
}
else
{
printf("\n Dequed value : %d", front->info); Output:
free(front);
front = NULL;
rear = NULL;
}
count--;
}

int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
}

void empty()
{
if ((front == NULL) && (rear == NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
}

Student Name: Rimsha Pervaiz_1307-2018 P a g e | 14


Instructor: Miss Faiza Sumbul

Potrebbero piacerti anche