DSA EXP3 - ShwetaJoshi
DSA EXP3 - ShwetaJoshi
DSA EXP3 - ShwetaJoshi
Theory: A linear queue is a linear data structure that serves the request first, which has been arrived
first. It consists of data elements which are connected in a linear fashion. It has two pointers, i.e., front
and rear, where the insertion takes place from the front end, and deletion occurs from the front end.
A Circular Queue is a special version of linear queue where the last element of the queue is
connected to the first element of the queue forming a circle. The operations are performed based on
FIFO (First In First Out) principle. It is also called ‘Ring Buffer’.
Linear Queue:
Step 2 - If it is NOT EMPTY, then print the front value i.e. queue[front] to be deleted.
Step 3-Check the value of front pointer. If it is equal to rear it means it is the last element of the queue.
The queue will become empty after deleting this element. In this case set front and rear both pointers to
-1.
Step 4: Else increment the front value by one (front ++).
Step 5: Exit
Step 2 - If it is NOT EMPTY, then traverse the queue from front position to rear position. Define an
integer variable 'i' and set 'i = front'.Display 'queue[i]' value and increment 'i' value by one (i++).
Repeat the same until 'i' value reaches to rear (i <= rear)
Department of Electronics & Telecommunication Engineering
Data Structures & Algorithms Lab (DJ19ECSBL1)
Step 3: Exit
Circular Queue:
Step 1 - Check whether queue is FULL. ((rear == SIZE-1 && front == 0) || (front == rear+1))
Step 2: Else if inserted element is first element of the queue then set front=rear=0
Step 3 - If space is there in the array towards LHS (rear == SIZE - 1 && front != 0), then
implement queue in circular fashion set rear = 0.
Step 6: Exit
If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not possible!!!" and terminate
the function.
Step 3-If front is equal to rear it means deleted element is the last value of the queue. The queue has
become empty. In this case set front and rear both pointers to -1.
Step 4-If front is pointing to the last index value then set it to the first index value(circular array)
Step 6-Exit
Step 2 - If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front'.
Department of Electronics & Telecommunication Engineering
Data Structures & Algorithms Lab (DJ19ECSBL1)
Step 3 - Check whether 'front <= rear', if it is TRUE, it means rear is left or equal to the front,then
display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until 'i <= rear'
becomes FALSE.
Step 4 - If 'front <= rear' is FALSE, then display 'queue[i]' value and increment 'i' value by one (i++).
Repeat the same until'i <= SIZE - 1' becomes FALSE.
Step 6 - Set i to 0.
Step 7 - Again display 'cQueue[i]' value and increment i value by one (i++). Repeat the same until 'i
<= rear' becomes FALSE.
Code:
#include <iostream>
#include <malloc.h>
struct node
int data;
};
void insertbegin()
int value;
Department of Electronics & Telecommunication Engineering
Data Structures & Algorithms Lab (DJ19ECSBL1)
cin>>value;
newnode->data=value;
if(head==NULL)
head=newnode;
head ->next=NULL;
else
newnode ->next=head;
head=newnode;
void insertend()
int value;
cin>>value;
newnode->data=value;
Department of Electronics & Telecommunication Engineering
Data Structures & Algorithms Lab (DJ19ECSBL1)
temp=head;
if(head==NULL)
head=newnode;
head->next=NULL;
return;
while((temp->next)!=NULL)
temp=temp->next;
temp->next=newnode;
newnode->next=NULL;
void display()
if(head==NULL)
return;
}
Department of Electronics & Telecommunication Engineering
Data Structures & Algorithms Lab (DJ19ECSBL1)
temp=head;
while(temp!=NULL)
cout<<temp->data<<" ";
temp=temp->next;
cout<<endl;
void insertafter()
int value,num1;
cin>>value;
newnode->data=value;
cin>>num1;
temp= head;
while((temp->data)!=num1)
Department of Electronics & Telecommunication Engineering
Data Structures & Algorithms Lab (DJ19ECSBL1)
temp=temp->next;
newnode->next=temp->next;
temp->next=newnode;
void insertbefore()
int value,num1;
cin>>value;
newnode->data=value;
cin>>num1;
temp= head;
while((temp->data)!=num1)
Department of Electronics & Telecommunication Engineering
Data Structures & Algorithms Lab (DJ19ECSBL1)
q=temp;
temp=temp->next;
q->next=newnode;
newnode->next=temp;
void deletebegin()
if(head==NULL)
cout<<"underflow";
else
temp=head;
if(head->next==NULL)
head=NULL;
free(temp);
Department of Electronics & Telecommunication Engineering
Data Structures & Algorithms Lab (DJ19ECSBL1)
else
head=head->next;
free(temp);
void deleteend()
if(head==NULL)
cout<<"underflow";
else
temp=head;
if(temp->next==NULL)
temp=NULL;
free(temp);
else
Department of Electronics & Telecommunication Engineering
Data Structures & Algorithms Lab (DJ19ECSBL1)
while(temp->next!=NULL)
q=temp;
temp=temp->next;
q->next=NULL;
free (temp);
void deleteposition()
int b;
cin>>b;
if(head==NULL)
cout<<"underflow";
}
Department of Electronics & Telecommunication Engineering
Data Structures & Algorithms Lab (DJ19ECSBL1)
else
temp=head;
if(temp->data==b)
deletebegin();
else
while(temp->data!=b)
q=temp;
temp=temp->next;
q->next= temp->next;
free (temp);
int main()
{
Department of Electronics & Telecommunication Engineering
Data Structures & Algorithms Lab (DJ19ECSBL1)
while(1)
int choice;
cout<<"1)Insert Begin 2)Insert End 3)Display 4)insert after 5)insert before 6)deletebegin
7)deleteend 8)deleteposition: ";
cin>>choice;
switch (choice)
case 1:
insertbegin();
break;
case 2:
insertend();
break;
case 3:
display();
break;
case 4:
insertafter();
break;
case 5:
insertbefore();
break;
case 6:
Department of Electronics & Telecommunication Engineering
Data Structures & Algorithms Lab (DJ19ECSBL1)
deletebegin();
break;
case 7:
deleteend();
break;
case 8:
deleteposition();
break;
default:
break;
return 0;
Conclusion:
We have successfully implemented Insert Begin, Insert End, Insert After, Insert Before, Delete Begin,
Delete End and Delete Position