KEMBAR78
Data Structures | PDF | Queue (Abstract Data Type) | Computer Programming
0% found this document useful (0 votes)
4 views30 pages

Data Structures

Data structure pdf

Uploaded by

poolgod2762
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)
4 views30 pages

Data Structures

Data structure pdf

Uploaded by

poolgod2762
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/ 30

UNIT III

Stacks: Introduction to Stacks, Stack as an Abstract Data Type, Representation of Stacks


through Array, Representation of Stacks through Linked Lists, Stacks and Recursion.
Queues: Introduction, Queue as an Abstract data Type, Representation of Queues, Circular
Queues,Double Ended Queues- Deques, Priority Queues, Application of Queues.

STACKS:
A stack is a linear data structure in which the elements are processed in LIFO manner (Last In
First Out)which means the element which is inserted last will be the first one to remove.
The insertions and deletions in a stack can be performed at only one end. That end is called
“TOP of thestack”.
The following are the list of operations that can be performed on a stack.
a. Push : The insertion operation is called as push operation.
b. Pop : The deletion operation is called as pop operation.
c. Traverse : visiting each and every data item of a stack is called traverse operation.
A stack is a linear list of data items which are processed as last in first out mechanism. Which
means the element which was pushed last will be the first one to pop out.
The following picture illustrates a stack.

OVERFLOW: Inserting an element in an already filled stack is called overflow situation. Before
insertingan element into the stack, we need to take care about the overflow situation.
UNDERFLOW: The Pop operation with an empty stack is called as underflow situation. So,
before poppingan element from the stack, we need to take care about the underflow situation.

STACK OPERATIONS
Stacks can be implemented by using following two ways:
1. Stacks using arrays.
2. Stacks using linked list.

1. STACK USING ARRAYS: The stacks are very easily implemented with the help of arrays. Here
we have two operations, push() and pop().

(A) Algorithm: push( ) Operation


[ initially top = - 1 , MaxSize = 10 ]
Step-1 : [ check for overflow ]
if(top+1>=MaxSize)
Display “OVER FLOW”
else
top = top+1
Read a value into item
Stack[top] = item
Step-2 : Exit

DATA STRUCTURES USING ‘C’ ( Unit-III )


IMPLEMENTATION :

(B) Algorithm: pop( ) operation

Step-1 : [ check for underflow ]


if( top = = - 1)
Display “underflow”
Else
item = stack[ top ]
top = top – 1;
Display “ popped item is “+item

Step-2 : Exit

DATA STRUCTURES USING ‘C’ ( Unit-III )


IMPLEMENTATION :
1. Pop( )
Since 4 ! = -1, so top = top – 1; display
element to beout is 50. Poppedelement is : 50

2. Pop( )
Since 3 ! = -1, so top = top – 1; display
element to beout is 40. Poppedelement is : 40

3. Pop( )
Since 2 ! = -1, so top = top – 1; display
element to beout is 30. Poppedelement is : 30

4. Pop( )
Since 1 ! = -1, so top = top – 1; display
element to beout is 20. Poppedelement is : 20
5. Pop( )
Since 0 ! = -1, so top = top – 1; display
element to beout is 10. Poppedelement is : 10
6. Pop( ) Since 0 > = 5, so top = top – 1 Stack is
underflow.

(C) Algorithm: Traversal


Step-1: if(top = = -1)
Display “Stack is empty”
else
set i = top
while ( i>=0 )
Display stack [ i ]
i = i -1
step-2: Exit

DATA STRUCTURES USING ‘C’ ( Unit-III )


IMPLEMENTATION

1. Consider the following stack. And set i=top.

2. Since i != -1display a[ i ] that is 50 and set i=i-1

3. Since i != -1display a[ i ] that is 40 and set i=i-1

4. Since i != -1display a[ i ] that is 30 and set i=i-1

5. Since i != -1display a[ i ] that is 20 and set i=i-1

6. Since i != -1 display a[ i ] that is 10 and set i=i-1

7. Since i = -1 display “No more elements in stack”.

DATA STRUCTURES USING ‘C’ ( Unit-III )


2. STACK USING LINKED LIST
While representing stacks using linked list we have to identify only a single node that is top.

(A) Algorithm: push( ) operation


Step-1: Read an element into item.
Create a new node temp.
Set temp.data=item, temp.link=null
Step-2: if( top = = null )
top = temp
else
temp.next=top
top=temp.
step-3: Exit

IMPLEMENTATION
1. Read an element into item since top = = null
2. Item=10, create a node temp.
3. Set temp.data=item, temp.link=null

4. since top==null set top=temp.

5. Item=20, create a node temp and set temp.data=20, temp.link=null.

6. Set temp.link=top and top=temp.

7. Item=30, create a node temp and set temp.data=30, temp.link=null

8. Set temp.link=top and top=temp

DATA STRUCTURES USING ‘C’ ( Unit-III )


(B) Algorithm: pop( ) operation
Step-1: [ check for underflow ]
if ( top = = null )
display “Stack Underflow”
else
item=top.data
display “deleted item is” item
top=top.link
Step 2 : Exit

IMPLEMENTATION:
Consider the following stack as Example.

1. Since top != null , item=top.data =30 and top=top.link

item=30
2. Since top != null , item=top.data = 20 and top=top.link

item=20
3. Since top != null , item=top.data = 10 and top = top.link.
4. Since top=null “stack is under flow”.

(C) Algorithm: Traversal( )


Step-1: [ check for underflow ]
if ( top = = null )
display “stack is underflow”
else
temp=top
while(temp != null )
display temp.data
temp=temp.link
Step-2 : Exit

DATA STRUCTURES USING ‘C’ ( Unit-III )


IMPLEMENTATION:
Consider the following stack with three nodes.

1. Since top != null, set temp=top.

2. Since temp != null Display temp.data that is 30 and temp=temp.link.


3. Since temp != null Display temp.data that is 20 and temp=temp.link

4. Since temp != null display temp.data that is 10 and temp=temp.link.


5. Since temp = null stop process.

DATA STRUCTURES USING ‘C’ ( Unit-III )


QUEUE
A Queue is a linear data structure which follows FIFO mechanism, which means the
elementwhich wasinserted first in to the Queue will be the first one to go out.
The insertion in Queue will be done at one end and deletion will be done at another
end.The place where insertions are made is called “rear” of the Queue.
The place where deletions are made is called “front” of the Queue.
In a Queue, the elements are inserted from rear end and deleted from front end.
The following are the list of operations that can be performed on Queue.

EnQueue: The insertion operation is called EnQueue(ENQ).


DeQueue: The deletion operation is called DeQueue(DNQ).
Traverse: Visiting each and every element of a Queue is called “Traversing”.

A Queue is a linear list of data items, which are processed as FIFO mechanism, which means the
elementsENQued will be first to be DNDued. The following picture illustrate a Queue.

OVERFLOW: Inserting an extra element in an already filled Queue is called Overflow. Before
inserting an elementinto a Queue, we need to take care about the overflow.

UNDERFLOW: The deletion operation with an empty Queue is called Underflow. So, before deleting
an element fromthe Queue, we need to take care about the underflow situation.

Queues can be implemented in two ways.


1. Queues using Arrays.
2. Queues using LinkedList.

1. Queues using Arrays


A Queue can be easily implemented with the help of arrays.

(A) Algorithm: Insertion Operation:


Initially front = rear = -1
Step-1: [ check for Overflow ]
If( rear + 1 >= maxsize )
Display “Queue Overflow”
Else
Read an element into item.
if(rear = = -1) then
Rear = front = 0
Else
Rear = rear + 1
a[rear] = item
Step-2: Exit

DATA STRUCTURES USING ‘C’ ( Unit-III )


IMPLEMENTATION:
1. Initially front = -1 and rear = -1 and maxsize=5
2. Since (rear+1)=0>=5 is false
3. Item=10
4. Since rear = -1, so front = rear = 0

5. Q[rear]=item
6. Since, (rear+1)=1>=5 is false
7. Item=20
8. Since rear != -1 rear = rear + 1, Q[rear]=20

9. Since, (rear+1)=2>=5 is false


10. Item=30
11. Since rear != -1 rear = rear + 1, Q[rear]=30

12. Since, (rear+1)=3>=5 is false


13. Item=40
14. Since rear != -1 rear = rear + 1, Q[rear]=40

15. Since, (rear+1)=4>=5 is false


16. Item=50
17. Since rear != -1 rear = rear + 1, Q[rear]=50

18. Since, (rear+1)=5>=5 is true, display “Queue Overflow”.

(B) Algorithm: Deletion Operation:


Step-1: [ check for underflow ]
If( front = -1 or front > rear )
Display “ Queue is underflow”
Else
item=Q[front]
front=front+1
Display :Deleted item is” + item
Step-2: Exit

DATA STRUCTURES USING ‘C’ ( Unit-III )


IMPLEMENTATION:
1. Let us consider the following Queue with maxzise=5.

2. Since front != -1 and front>rear is false


3. Item=Q[front]=10 and front=front+1=1

Display “deleted element is 10”


4. Since front != -1 and front>rear is false
5. Item=Q[front]=20 and front=front+1=2

Display “deleted element is 20”


6. Since front != -1 and front>rear is false
7. Item=Q[front]=30 and front=front+1=3

Display “deleted element is 30”


8. Since front != -1 and front>rear is false
9. Item=Q[front]=40 and front=front+1=4

Display “deleted element is 40”


10. Since front != -1 and front>rear is false
11. Item=Q[front]=50 and front=front+1=5

Display “deleted element is 50”


12. Since front != -1 and front>rear is true display “ Queue is underflow”.

(C) Algorithm: Traversing


Step-1: set i = front
Step-2: while (i<=rear)
display Q[ i ]
i=i+1
Step-3: Exit

DATA STRUCTURES USING ‘C’ ( Unit-III )


IMPLEMENTATION:
1. Consider the following Queue with maxsize=5 and set i=front.

2. Since i<=rear is true display Q[ i ] =10 and i=i+1=1

3. Since i<=rear is true display Q[ i ] =20 and i=i+1=2

4. Since i<=rear is true display Q[ i ] =30 and i=i+1=3

5. Since i<=rear is true display Q[ i ] =40 and i=i+1=4

6. Since i<=rear is true display Q[ i ] =50 and i=i+1=5

7. Since i<=rear is false stop process.

Types of Queues:
There are several types of Queues, each one is used in different type of application. The following
are thedifferent types of Queues.
1. Circular Queues
2. Priority Queues
3. Double Ended Queues (DEQueues)
1. CIRCULAR QUEUES:
Let us consider a situation in ordinary Queue that the max-size of the Queue is 5 and 5
insertions arealready performed and 2 deletions are performed. After performing deletions the
Queue is as follows:

One more insertion operation is not permitted in the above Queue because the rear
reaches the max-size of theQueue even though there is a space left for 2 elements before front.
In order to overcome such a kind of problem, we implement Queue as a circular one. In a
circular Queuethere is no beginning and there is no ending. If the rear reaches end of Queue, it
move towards front and the space utilized properly.
The circular Queues can also be implemented in two ways.
1. Using Arrays

DATA STRUCTURES USING ‘C’ ( Unit-III )


2. Using Linked List.

1. Insertion( ) 2. Insertion( )
Item=10 Item=20

3. Insertion( ) 4. Insertion( )
Item=30 Item=40

5. Insertion( )
6. Deletion( )
Item=50

7. Deletion( ) 8. Deletion( )

DATA STRUCTURES USING ‘C’ ( Unit-III )


9. Insertion( ) 10. Insertion(
Item=60 )Item=70

12. Insertion( The next iteration of rear is


11. Insertion( )
)Item=90 equal to front. So, the
Item=80
Queue is overflow.

(A) Algorithm: Insertion( )


Step-1: [ check for overflow ]
If((rear+1) % max-size = = front) then
Display “ Queue is Overflow”
Else
read a value into item
if( rear = = -1) then
rare=front=0
else
rear = ( rear + 1 ) % max-size
Q[rear] = item
Step 2 : Exit

DATA STRUCTURES USING ‘C’ ( Unit-III )


Algorithm: Deletion( )

Step-1: [ check for underflow ]


If( front = = -1 ) then
Display “Queue is underflow”
Else
item=Q[front]
if( front = = rear ) then
Front = rear = -1
Else
Front = (front+1)%max-size
display “element deleted”+item
Step-2: Exit

(B) Algorithm: Traversing( )

Step-1: set i=front


Step-2: while ( i != (rear + 1)%max-size )
display Q[ i ]
i=(i+1)%max-size
Step-3: Exit

DOUBLE ENDED QUEUES


A Double Ended Queue is an abstract data structure that implements a Queue in which insertions and
deletions will be done either at the front end or at the rear end pf the Queue. The operations that can be
performed on double ended queue are
1. Insert an item from front end
2. Insert an item from rear end
3. Delete an item from front end
4. Delete an item from rear end
5. Display the elements of Queue

A dequeue is a double-ended queue. You can insert items at either end and delete them
from either end. The methods might be called insertLeft() and insertRight(), and removeLeft() and
removeRight(). If you restrict yourself to insertLeft() and removeLeft() (or their equivalents on the
right), then the deque acts like a stack. If you restrict yourself to insertLeft() and removeRight()
(or the opposite pair), then it acts like a queue.
A dequeue provides a more versatile data structure than either a stack or a queue, and is
sometimes used in container class libraries to serve both purposes. However, it's not used as
often as stacks and queues, so we won't explore it further here.
PRIORITY QUEUES
A priority queue is a more specialized data structure than a stack or a queue. However, it's
a useful tool in a surprising number of situations. Like an ordinary queue, a priority queue has a
front and a rear, and items are removed from the front. However, in a priority queue, items are
ordered by key value, so that the item with the lowest key (or in some implementations the
highest key) is always at the front. Items are inserted in the proper position to maintain the order.
Here's how the mail sorting analogy applies to a priority queue. Every time the postman
hands you a letter, you insert it into your pile of pending letters according to its priority. If it must
be answered immediately (the phone company is about to disconnect your modem line), it goes
on top, while if it can wait for a leisurely answer (a letter from your Aunt Mabel), it goes on the

DATA STRUCTURES USING ‘C’ ( Unit-III )


bottom. When you have time to answer your mail, you start by taking the letter off the top (the
front of the queue), thus ensuring that the most important letters are answered first.
LAB PROGAMS

1. Write a Program on Stack using Arrays

#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t ");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}

DATA STRUCTURES USING ‘C’ ( Unit-III )


}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");

}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}

DATA STRUCTURES USING ‘C’ ( Unit-III )


OUTPUT:

Enter the size of STACK[MAX=100]:10

STACK OPERATIONS USING ARRAY

1. PUSH
2. POP
3. DISPLAY
4. EXIT
 Enter the Choice:1
Enter a value to be pushed:12

 Enter the Choice:1


Enter a value to be pushed:24

 Enter the Choice:1


Enter a value to be pushed:98

 Enter the Choice:3

The elements in STACK


98
24
12
Press Next Choice
 Enter the Choice:2
The popped elements is 98
 Enter the Choice:3
The elements in STACK
24
12
Press Next Choice
 Enter the Choice:4
EXIT POINT

DATA STRUCTURES USING ‘C’ ( Unit-III )


2. Write a Program on Stack using linked list

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

struct node
{
int data;
struct node *next;
}*top;

/* Initialize an empty stack */


void initialize()
{
top = NULL;
}

/* Checks if Stack is empty or not */


int isEmpty()
{
if (top == NULL)
return 1;
else
return 0;
}
/* Returns the top element of Stack */
int peek()
{
return top->data;
}

/* Count stack elements */


int getStackSize(struct node *head)
{
/* Input Validation */
if (head == NULL) {
printf("Error : Invalid stack pointer !!!\n");
return;
}
int length = 0;
while(head != NULL)
{
head = head->next;
length++;
}
return length;
}
/* Push an Element in Stack */

DATA STRUCTURES USING ‘C’ ( Unit-III )


void push(int num)
{
struct node *temp;
temp =(struct node *)malloc(1*sizeof(struct node));
temp->data = num;

if (top == NULL)
{
top = temp;
top->next = NULL;
}
else
{
temp->next = top;
top = temp;
}
}
/* Pop Operation: Removes Top Element of the Stack */
void pop()
{
struct node *temp;
if (isEmpty(top))
{
printf("\nStack is Empty\n");
return;
}
else
{
temp = top;
top = top->next;
printf("Removed Element : %d\n", temp->data);
free(temp);
}
}
/* Prints the linked list representation of a stack */
void printStack(struct node *nodePtr)
{
while (nodePtr != NULL)
{
printf("%d", nodePtr->data);
nodePtr = nodePtr->next;
if(nodePtr != NULL)
printf("-->");
}
printf("\n");
}

DATA STRUCTURES USING ‘C’ ( Unit-III )


void main()
{
/* Initialize Stack */
initialize();
/* Push Elements in stack */
push(1);
push(2);
push(3);
push(4);
/* Prints Size of Stack */
printf("Stack Size : %d\n", getStackSize(top));
/* Printing top element of Stack */
printf("\nTop Element : %d\n", peek());
/* Printing Stack */
printf("Stack as linked List\n");
printStack(top);
/* Removing elements from stack */
pop();
pop();
pop();
pop();
pop();
printStack(top);
return;
}

Output :
Stack Size : 4

Top Element : 4
Stack as linked List
4-->3-->2-->1
Removed Element : 4
Removed Element : 3
Removed Element : 2
Removed Element : 1

Stack is Empty
3. Write a Program on Queue using Arrays

#include<stdio.h>
#define n= 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}

OUTPUT:
Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
 Enter the Choice:1
Enter no 1:10
 Enter the Choice:1
Enter no 2:54
 Enter the Choice:1
Enter no 3:98
 Enter the Choice:1
Enter no 4:234
 Enter the Choice:3
Queue Elements are:
10
54
98
234
 Enter the Choice:2
Deleted Element is 10
 Enter the Choice:3
Queue Elements are:
54
98
234
 Enter the Choice:4
4. Write a program on Queue using linked list

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

struct node
{
int data;
struct node *next;
} *front, *back;

/* Create an empty queue */


void initialize()
{
front = back = NULL;
}
/* Returns queue size */
int getQueueSize()
{
struct node *temp = front;
int count = 0;
if(front == NULL && back == NULL)
return 0;
while(temp != back)
{
count++;
temp = temp->next;
}
if(temp == back)
count++;
return count;
}
/* Returns Frnt Element of the Queue */
int getFrontElement()
{
return front->data;
}
/* Returns the Rear Element of the Queue */
int getBackElement()
{
return back->data;
}
/* Check's if Queue is empty or not */
void isEmpty()
if (front == NULL && back == NULL)
printf("Empty Queue\n");
else
printf("Queue is not Empty\n");
}
/* Adding elements in Queue */
void enqueue(int num)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = num;
temp->next = NULL;

if (back == NULL)
{
front = back = temp;
}
else
{
back->next = temp;
back = temp;
}
}
/* Removes an element from front of the queue */
void dequeue()
{
struct node *temp;
if (front == NULL)
{
printf("\nQueue is Empty \n");
return;
}
else
{
temp = front;
front = front->next;
if(front == NULL){
back = NULL;
}
printf("Removed Element : %d\n", temp->data);
free(temp);
}
}
/* Print's Queue */
void printQueue()
{
struct node *temp = front;
if ((front == NULL) && (back == NULL))
printf("Queue is Empty\n");
return;
}
while (temp != NULL)
{
printf("%d", temp->data);
temp = temp->next;
if(temp != NULL)
printf("-->");
}
}
int main()
{
/* Initializing Queue */
initialize();
/* Adding elements in Queue */
enqueue(1);
enqueue(3);
enqueue(7);
enqueue(5);
enqueue(10);
/* Printing Queue */
printQueue();
/* Printing size of Queue */
printf("\nSize of Queue : %d\n", getQueueSize());
/* Printing front and rear element of Queue */
printf("Front Element : %d\n", getFrontElement());
printf("Rear Element : %d\n", getBackElement());
/* Removing Elementd from Queue */
dequeue();
dequeue();
dequeue();
dequeue();
dequeue();
dequeue();
return 0;
}

Output :
1-->3-->7-->5-->10
Size of Queue : 5
Front Element : 1
Rear Element : 10
Removed Element : 1
Removed Element : 3
Removed Element : 7
Removed Element : 5
Removed Element : 10
Queue is Empty
5. Write a program on circular queues

#include <stdio.h>
# define max 6
int queue[max]; // array declaration
int front=-1;
int rear=-1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max; // rear is incremented
queue[rear]=element; // assigning a value to the queue at the rear position.
}
}
// function to delete the element from the queue
int dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
// function to display the elements of a queue
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x; // variables declaration

while(choice<4 && choice!=0) // while loop


{
printf("\n Press 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
printf("\nEnter your choice");
scanf("%d", &choice);

switch(choice)
{

case 1:
printf("Enter the element which is to be inserted");
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
}}
return 0;
}
6. Write a program on circular queuelist using linked list

#include <stdio.h>
// Declaration of struct type node
struct node
{
int data;
struct node *next;
};
struct node *front=-1;
struct node *rear=-1;
// function to insert the element in the Queue
void enqueue(int x)
{
struct node *newnode; // declaration of pointer of struct node type.
newnode=(struct node *)malloc(sizeof(struct node));
// allocating the memory to the newnode
newnode->data=x;
newnode->next=0;
if(rear==-1) // checking whether the Queue is empty or not.
{
front=rear=newnode;
rear->next=front;
}
else
{
rear->next=newnode;
rear=newnode;
rear->next=front;
}
}
// function to delete the element from the queue
void dequeue()
{
struct node *temp; // declaration of pointer of node type
temp=front;
if((front==-1)&&(rear==-1)) // checking whether the queue is empty or not
{
printf("\nQueue is empty");
}
else if(front==rear) // checking whether the single element is left in the queue
{
front=rear=-1;
free(temp);
}
else
{
front=front->next;
rear->next=front;
free(temp);
}
}
// function to get the front of the queue
int peek()
{
if((front==-1) &&(rear==-1))
{
printf("\nQueue is empty");
}
else
{
printf("\nThe front element is %d", front->data);
}
}
// function to display all the elements of the queue
void display()
{
struct node *temp;
temp=front;
printf("\n The elements in a Queue are : ");
if((front==-1) && (rear==-1))
{
printf("Queue is empty");
}
else
{
while(temp->next!=front)
{
printf("%d,", temp->data);
temp=temp->next;
}
printf("%d", temp->data);
}
}
void main()
{
enqueue(34);
enqueue(10);
enqueue(23);
display();
dequeue();
peek();
}
Output :

You might also like