SUBJECT-COMPUTER
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
if(front==0 && rear==0)
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");
}
}
public static void main(String args[])
Scanner sc=new Scanner(System.in);
System.out.print ("Enter the size of Queue : ");
int cap=sc.nextInt();
Queue ob=new Queue(cap);
System.out.println("1 for add");
System.out.println("2 for remove");
System.out.println("3 for display");
System.out.println("4 for exit");
System.out.println("enter a choice");
int ch=sc.nextInt();
if (ch==1)
System.out.print("Enter a number : ");
int v=sc.nextInt();
ob.added(v);
else if (ch==2)
System.out.print("Value popped = ");
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:
Define a class Dequeue with the following details:
Class name: Dequeue
Data members/instance variables:
arr[]: array to hold up to 100 integer elements.
lim: stores the limit of the dequeue.
front: to point to the index of front end.
rear: to point to the index of rear end.
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().
The main function and algorithm need not be written.
class Dequeue{
int arr[];
int lim;
int front;
int rear;
public Dequeue(int l){
lim = l;
front = 0;
rear = 0;
}
public void addFront(int val){
if(front - 1 >= 0)
arr[--front] = val;
else
System.out.println("Overflow from front");
}
public void addRear(int val){
if(rear + 1 < lim)
arr[++rear] = val;
else
System.out.println("Overflow from rear");
}
public int popFront(){
if(front + 1 <= rear)
return arr[front--];
return -9999; }
public int popRear(){
if(rear - 1 >= front)
return arr[rear--];
return -9999;
}
}
Stack:
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).
PUSH Operation (Inserting element in the stack)
POP Operation (Deleting element from the stack)
Complete Java Program implementing Operations on Stack:
Example3:
class Stack
int ST[]; // Array to implement stack
int size; // Maximum size of the stack
int top; // Index of topmost element (Stack Pointer)
Stack() // Default constructor
size = 0;
top = 0;
Stack(int cap) // Parameterised Constructor
size = cap;
ST = new int[size];
top = -1; // Initialising top with -1
void push(int n) // Function to insert element in Stack
if(top == size-1) // Condition for Overflow
System.out.println("OVERFLOW");
else
{
top = top + 1;
ST[top] = n; // Storing value in Stack
int pop() // Function to delete element from Stack
if(top == -1) // Condition for Underflow
System.out.println("UNDERFLOW");
return -999;
else
int val = ST[top]; // Storing the element which will be removed
top = top - 1;
return val;
void display()
if(top == -1)
System.out.println("The stack is empty");
}
else
System.out.println("The elements in the stack are : ");
for(int i = top; i>=0; i--)
System.out.println(ST[i]);
NOTE- All the answers should be given in your notebook
Q1- Write the above given programs in your notebook and try them on computer also.
---------------------