Data Structure
Data Structure
CLASS-12
CHAPTER 13– SIMPLE DATA STRUCTURE
Queue:
Queue represents a data structure designed to have elements inserted at the end of the queue,
and elements removed from the beginning of the queue. This is similar to how a queue in a
supermarket works. The Java Queue interface is a subtype of the Java Collection interface.
Example1:
Queue:
import java.util.*;
class Queue
int que[],size,front,rear;
Queue(int cap)
size=cap;
front=0;
rear=0;
que=new int[size];
void added(int v)
if(rear==size-1)
System.out.print("Queue OVERFLOW");
}
else
front=1;
rear=1;
else
rear=rear+1;
que[rear]=v;
int popfront()
int value=-9999;
if(front==0&&rear==0)
System.out.print("Queue UNDERFLOW");
return value;
else
{
if(front==rear)
front=0;
rear=0;
else
front=front+1;
return value;
void display()
if(front==0&&rear==0)
System.out.print("Queue is Empty");
else
for(int i=front;i<=rear;i++)
System.out.print(que[i]+"\t");
}
}
int cap=sc.nextInt();
System.out.println("enter a choice");
int ch=sc.nextInt();
if (ch==1)
int v=sc.nextInt();
ob.added(v);
else if (ch==2)
ob.popfront();
}
else if(ch==3)
ob.display();
else
System.out.print(System.exit(0));
Deque:
The Java Deque represents a double ended queue,meaning a queue where you can add
and remove elements to and from both ends of the queue. The name Deque is an
abbreviation of Double Ended Queue.
Example2:
Member functions:
Dequeue(int l): constructor to initialize data members lim = l; front = rear = 0.
void addFront(int val): to add integer from the front if possible else display the message
“Overflow from front”.
void addRear(int val): to add integer from the rear if possible else display the message
“Overflow from rear”.
int popFront(): returns element from front if deletion is possible from the front end,
otherwise returns -9999.
int popRear(): returns element from rear if deletion is possible from the rear end,
otherwise returns -9999.
Specify the class Dequeue giving details of the constructor, void addFront(int), void
addRear(int), int popFront() and int popRear().
Stack is a linear data structure which enables the user to add and remove elements from the
top end only, using the concept of LIFO (Last In First Out).
size = 0;
top = 0;
size = cap;
ST = new int[size];
System.out.println("OVERFLOW");
else
{
top = top + 1;
System.out.println("UNDERFLOW");
return -999;
else
top = top - 1;
return val;
void display()
if(top == -1)
}
else
System.out.println(ST[i]);
Q1- Write the above given programs in your notebook and try them on computer also.
---------------------