Вы находитесь на странице: 1из 47

Unit –II

1. STACKS: Stack is one of the important linear data structures which allow the elements to
insert and delete at only one end called Top of the stack. The insertion operation is referred to as
PUSH operation and the deletion operation is referred to as POP operation in a stack. Since, the
elements are inserted and deleted at the same end we get the elements in the reverse order to that
of the order that they are inserted. This principle is called Last-In-First-Out (LIFO). That is the
element which is inserted last will be deleted first and the element which is inserted first can be
deleted last.

The most accessible element of the stack is called Top of the stack where as least
accessible element is called Bottom of the stack. The following figure shows the effects of push
and pop operations on the stack.

We can perform the following operations on a stack:


 Creating an empty stack
 Determine whether the stack is empty or not
 Determine the size of the stack
 Push an element into stack
 Pop an element from the stack
 Display an element at top of the stack
 Traversing (displaying) all the elements present in the stack.

2. Stack ADT: A stack may be specified as an abstract data type (ADT) in which we provide a
specification of the instances as well as of the operations that are to be performed. The operations
we need to perform on a stack can be expressed as methods in an interface as follows:
interface StackADT
{ void create( );
boolean isEmpty( );
int count( );
void push(Object data);
Object pop( );
void peek( );
void traverse( );

1
}
Implementation of Stack
Since a stack is also a list, it can be implemented using an array or it can be implemented
using a linked representation.

3. Array Implementation of a Stack


The elements in the Stack are of generic type Object. The elements form a linear structure
in which elements follow one after the other. The Stack ADT supports the following operations.
1. create( ) : Creates an empty stack.
2. isEmpty( ) : returns true if stack is empty, false otherwise
3. count( ) : returns the stack size
4. push(x) : inserts x at top of the stack
5. pop( ) : removes an element at top of the stack
6. peek( ) : Displays an element at top of the stack
7. traverse( ) : output the stack elements from top to bottom

The Stack ADT is translated into a Java interface as follows:


interface StackADT
{ void create( );
boolean isEmpty( );
int count( );
void push(Object data);
Object pop( );
void peek( );
void traverse( );
}
To implement the above said Stack operations let us make the following assumptions:
1. Let stack be an Object array with memory for specified capacity elements.
2. Let top be the integer type identifier which represents number of elements in the stack.
For each push and pop operation, the value of top should be incremented and
decremented by 1 respectively.
Note: 1. To run the following program first write import java.util.*; statement in the code.
2. Before writing the following implementation class write the interface StackADT.

class StackArray implements StackADT


{ int top = -1;
Object stack[];
int capacity = 10;
public void create()
{ top = -1;
stack = new Object[capacity];
System.out.println("Stack is Created....");
}
public boolean isEmpty()
{ if(top == -1)
return true;
else
return false;
}
public int count()
{ return top+1;

2
}
public void push(Object data)
{ if(top == capacity-1)
System.out.println("The stack is already full");
else
{ top++;
stack[top]=data;
System.out.println(data + " inserted successfully into the stack");
}
}
public Object pop()
{ Object removedElement = null;
if(top == -1)
System.out.println("The stack is empty");
else
{ removedElement = stack[top];
top--;
}
return removedElement;
}
public void peek()
{ if(top == -1)
System.out.println("Stack is empty");
else
System.out.println("Element at top of the stack is : "+ stack[top]);
}
public void traverse()
{ if(top==-1)
System.out.println("The stack is empty");
else
{ System.out.println("Elements in the stack are: ");
for(int i=top;i>=0;i--)
System.out.println(stack[i]);
}
}
}
class StackDemo
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
int ch;
StackArray ob = new StackArray();
do
{ System.out.println("1.Creating an empty stack");
System.out.println("2.Checking whether stack is empty or not");
System.out.println("3.Displaying number of elements in the stack");
System.out.println("4.Pushing an element into the stack");
System.out.println("5.Popping an element from the stack");
System.out.println("6.Displaying element at top of the stack");
System.out.println("7.Traversing elements in a stack");
System.out.println("8.Exit");
System.out.print("\nEnter your choice:");

3
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Stack is empty : " + ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the stack are: "
+ ob.count() );
break;
case 4 :
System.out.println("Enter the Element to insert into the
stack:");
int element = sc.nextInt();
ob.push(element);
break;
case 5 :System.out.println("The element popped from stack is : " +
ob.pop());
break;
case 6 :ob.peek();
break;
case 7 :ob.traverse();
break;
}
}while(ch>=1 && ch<=7);
}
}
Output:
F:\>javac StackDemo.java
F:\>java StackDemo
1.Creating an empty stack
2.Checking whether stack is empty or not
3.Displaying number of elements in the stack
4.Pushing an element into the stack
5.Popping an element from the stack
6.Displaying element at top of the stack
7.Traversing elements in a stack
8.Exit
Enter your choice:1
Stack is Created....

1.Creating an empty stack


2.Checking whether stack is empty or not
3.Displaying number of elements in the stack
4.Pushing an element into the stack
5.Popping an element from the stack
6.Displaying element at top of the stack
7.Traversing elements in a stack
8.Exit
Enter your choice:4
Enter the Element to insert into the stack:
10

4
10 inserted successfully into the stack
4. Implementation of a Stack Using Single Linked List Representation
Initially the list is empty, so the top pointer is NULL. The push function creates a new
node by using the data value, and adds it to the top of the existing list. A pop function retrieves
the value of the node pointed to by the top pointer, takes the top point to the next node, and
destroys the node that was pointed by the top. If this strategy is used for creating a stack with the
four data values: 10, 20, 30, and 40, then the stack is created as shown in the following figure:

The elements in the stack are of generic type Object. The elements form a linear structure
in which elements follow one after the other. The Stack ADT supports the following operations.
1. create( ) : Creates an empty stack.
2. isEmpty( ) : returns true if stack is empty, false otherwise
3. count( ) : returns the stack size
4. push(x) : inserts x at top of the stack
5. pop( ) : removes an element at top of the stack
6. peek( ) : Displays an element at top of the stack
7. traverse( ) : output the stack elements from top to bottom

The Stack ADT is translated into a Java interface as follows:


interface StackADT
{ void create( );
boolean isEmpty( );
int count( );
void push(Object data);
Object pop( );
void peek( );
void traverse( );
}
To implement the above said Stack operations let us make the following assumptions:
1. The single linked list implementation of a stack requires a class Node which contains two
data members: info and link fields. The info field may contain any data of type Object.
The link is a reference to the next node in the list.
2. Let size be the integer type identifier which represents number of elements in the list. For
each push and pop operations the value of size should be incremented and decremented
by 1 respectively.
Note: 1. To run the following program first write import java.util.*; statement in the code.

5
2. Before writing the following implementation class write the interface StackADT.
class Node
{ Object info;
Node link;
}
class StackSLL implements StackADT
{ Node top,node,temp,p;
int size=0;
public void create()
{ top = null;
size = 0;
System.out.println("Stack is Created....");
}
public boolean isEmpty()
{ if(size == 0)
return true;
else
return false;
}
public int count()
{ return size;
}
public void push(Object data)
{ node = new Node();
node.info = data;
node.link = top;
top = node;
size++;
System.out.println(data + " inserted successfully into the stack");
}
public Object pop()
{ if(size == 0)
System.out.println("The stack is empty");
else
{ temp = top;
top = top.link;
size--;
}
return temp.info;
}
public void peek()
{ if(size == 0)
System.out.println("Stack is empty");
else
System.out.println("Element at top of the stack is : "+ top.info);
}
public void traverse()
{ if(size==0)
System.out.println("The stack is empty");
else
{ p = top;

6
System.out.println("Elements in the stack are: ");
while ( p!= null)
{
System.out.println(p.info);
p = p.link;
}
}
}
}
class StackDemo1
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
int ch;
StackSLL ob = new StackSLL();
do
{ System.out.println("1.Creating an empty stack");
System.out.println("2.Checking whether stack is empty or not");
System.out.println("3.Displaying number of elements in the stack");
System.out.println("4.Pushing an element into the stack");
System.out.println("5.Popping an element from the stack");
System.out.println("6.Displaying element at top of the stack");
System.out.println("7.Traversing elements in a stack");
System.out.println("8.Exit");
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Stack is empty : " + ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the stack are: "
+ ob.count() );
break;
case 4 :
System.out.println("Enter the Element to insert into the
stack:");
int element = sc.nextInt();
ob.push(element);
break;
case 5 :System.out.println("The element popped from stack is : " +
ob.pop());
break;
case 6 :ob.peek();
break;
case 7 :ob.traverse();
break;
}
}while(ch>=1 && ch<=7);
}
}

7
5. Implementation of a Stack Using Double Linked List Representation
The elements in the stack are of generic type Object. The elements form a linear structure in
which elements follow one after the other. The Stack ADT supports the following operations.
1. create( ) : Creates an empty stack.
2. isEmpty( ) : returns true if stack is empty, false otherwise
3. count( ) : returns the stack size
4. push(x) : inserts x at top of the stack
5. pop( ) : removes an element at top of the stack
6. peek( ) : Displays an element at top of the stack
7. traverse( ) : output the stack elements from top to bottom

The Stack ADT is translated into a Java interface as follows:


interface StackADT
{ void create( );
boolean isEmpty( );
int count( );
void push(Object data);
Object pop( );
void peek( );
void traverse( );
}

To implement the above said Stack operations let us make the following assumptions:
1. The double linked list implementation of a stack requires a class Node which contains three
data members: info, left and right fields. The info field may contain any data of type Object. The
left is a reference to the processor node and the right is a reference to the successor node in the
list.
2. Let size be the integer type identifier which represents number of elements in the list. For each
push and pop operations the value of size should be incremented and decremented by 1
respectively.
Note: 1. To run the following program first write import java.util.*; statement in the code.
2. Before writing the following implementation class write the interface StackADT in the code.

class Node
{ Object info;
Node right, left;
}
class StackDLL implements StackADT
{ Node top,node,temp,p;
int size=0;
public void create()
{ top = null;
size = 0;
System.out.println("Stack is Created....");
}
public boolean isEmpty()
{ if(size == 0)
return true;
else
return false;

8
}
public int count()
{ return size;
}
public void push(Object data)
{ node = new Node();
node.info = data;
node.left = null;
node.right = top;
top = node;
size++;
System.out.println(data + " inserted successfully into the stack");
}
public Object pop()
{ if(size == 0)
System.out.println("The stack is empty");
else
{ temp = top;
top = top.right;
top.left = null;
size--;
}
return temp.info;
}
public void peek()
{ if(size == 0)
System.out.println("Stack is empty");
else
System.out.println("Element at top of the stack is : "+ top.info);
}
public void traverse()
{ if(size==0)
System.out.println("The stack is empty");
else
{ p = top;
System.out.println("Elements in the stack are: ");
while ( p!= null)
{
System.out.println(p.info);
p = p.right;
}
}
}
}
class StackDemo1
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
int ch;
StackDLL ob = new StackDLL();
do
{ System.out.println("1.Creating an empty stack");

9
System.out.println("2.Checking whether stack is empty or not");
System.out.println("3.Displaying number of elements in the stack");
System.out.println("4.Pushing an element into the stack");
System.out.println("5.Popping an element from the stack");
System.out.println("6.Displaying element at top of the stack");
System.out.println("7.Traversing elements in a stack");
System.out.println("8.Exit");
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Stack is empty : " + ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the stack are: "
+ ob.count() );
break;
case 4 :
System.out.println("Enter the Element to insert into the
stack:");
int element = sc.nextInt();
ob.push(element);
break;
case 5 :System.out.println("The element popped from stack is : " +
ob.pop());
break;
case 6 :ob.peek();
break;
case 7 :ob.traverse();
break;
}
}while(ch>=1 && ch<=7);
}
}

6. APPLICATIONS OF STACKS
1. Expression Evaluation:
One of the applications of the stack is in expression evaluation. A complex assignment statement
such as a = b + c*d/e–f may be interpreted in many different ways. Therefore, to give a unique
meaning, the precedence and associativity rules are used. But still it is difficult to evaluate an
expression by computer in its present form, called the infix notation. In infix notation, the binary
operator comes in between the operands. A unary operator comes before the operand. To get it
evaluated, it is first converted to the postfix form, where the operator comes after the operands.
For example, the postfix form for the expression a*(b–c)/d is abc–*d/. A good thing about postfix
expressions is that they do not require any precedence rules or parentheses for unique definition.
So, evaluation of a postfix expression is possible using a stack-based algorithm.
2. Recursion:
Java supports recursion. A method that calls itself is said to be recursion. Recursion
internally uses the linear data structure stack to keep track of each and every method call. The
classic example of recursion is the computation of the factorial of a number. The factorial of a

10
number N is the product of all the whole numbers between 1 and N. For example, 3 factorial is
1×2×3, or 6.
class Factorial
{ int fact(int n)
{ if ( n ==1)
return 1;
else
return ( n* fact(n-1) );
}
}
class Recursion
{ public static void main (String args[])
{ Factorial f =new Factorial();
System.out.println(“Factorial of 3 is “ + f.fact(3));
}
}
The output from this program is shown here:
Factorial of 3 is 6

When we compute the factorial of 3, the first call to fact() will cause a second call to be
made with an argument of 2, this invocation will cause fact() to be called a third time with an
argument of 1. This call will return1, which is then multiplied by 2 (the value of n in the second
invocation). This result (which is 2) is then returned to the original invocation of fact() and
multiply by 3 ( the original value of n). This yields the answer, 6. Internally each and every call
is maintained in a Stack and for every return statement a pop operation is executed by
multiplying return statement with the pop operation and finally the result will be returned to the
initial call statement fact(3).

7. QUEUES
A queue is also a list of elements with insertions permitted at one end—called the rear,
and deletions permitted from the other end—called the front. This means that the removal of
elements from a queue is possible in the same order in which the insertion of elements is made
into the queue. Thus, a queue data structure exhibits the FIFO (first in first out) property. Insert
and delete are the operations that are provided for insertion of elements into the queue and the
removal of elements from the queue, respectively. The following figure shows the effects of
insert and delete operations on the queue.

11
8. Types of Queues:
Queues are broadly classified into four categories, listed below:
1. Linear Queue: Linear Queue is a linear data structure which allows to insert elements at
rear end of the queue and which allows to delete elements from front of the queue. It uses
the principle FIFO (First-In-First-Out).

NOTE: 1. Inserting element into queue is also called as Enqueue operation.


2. Deleting element from queue is also called as Dequeue operation.

2. Circular Queue: Circular Queue is also a queue which used the same principle FIFO
such that elements are inserted at rear and elements are deleted at front of the queue. But
the only difference is that it is assumed that the sub sequent position of the last location of
the queue is the first position. That is, if the size of the queue is n then the subsequent
position of n should be 1.

3. Doubled Ended Queue: Deque is also a linear queue which stands for double ended
queue. In a deque, we can insert and delete elements at both the ends front and rear.

12
Deques are categorized into two types that is input restricted deque and output restricted
deque. In input restricted deque insertion is allowed at only at rear end but deletion is
allowed at both the front and rear ends. In output restricted deque deletion is allowed at
only at front end but deletion is allowed at both the front and rear ends.

4. Priority Queue: Priority Queue is also a linear data structure in which the elements will
be inserted and deleted at any position of the queue depending on some property called
“Priority”. Priority Queues are classified into two types, namely single priority queue
and multiple priority queues. If a single queue has been maintained for different
categories of elements then such type of priority queue is called single priority queue. If
separate queues are maintained for each category of elements then such type of priority
queue is called multiple priority queues.

Implementation of Queues:
Since a queue is also a list, it can be implemented using an array or it can be implemented
using a linked representation.

9. Linear Queue: Linear Queue is a linear data structure which allows to insert elements at
rear end of the queue and which allows to delete elements from front of the queue. It uses the
principle FIFO (First-In-First-Out).

We can perform the following operations on a linear queue:


 Creating an empty queue
 Determine whether the queue is empty or not
 Determine the size of the queue
 Insert an element into queue

13
 Delete an element from the queue
 Displaying element at front of the queue
 Displaying element at rear of the queue
 Traversing (displaying) all the elements present in the queue.

Linear Queue ADT: A linear queue may be specified as an abstract data type (ADT) in which
we provide a specification of the instances as well as of the operations that are to be performed.
The operations we need to perform on a linear queue can be expressed as methods in an interface
as follows:
interface LinearQueueADT
{ void create( );
boolean isEmpty( );
int count( );
void insert(Object data);
Object delete( );
void frontQueue( );
void rearQueue();
void traverse( );
}
Implementation of Linear Queue
Since a linear queue is also a list, it can be implemented using an array or it can be
implemented using a linked representation.

10. Array Implementation of a Linear Queue


The elements in the linear queue are of generic type Object. The elements form a linear
structure in which elements follow one after the other. The LinearQueue ADT supports the
following operations.
1. create( ) : Creates an empty linear queue.
2. isEmpty( ) : returns true if linear queue is empty, false otherwise
3. count( ) : returns the linear queue size
4. insert(x) : inserts x at rear of the linear queue
5. delete( ) : removes an element at front of the linear queue
6. frontQueue( ) : Displays an element at front of the linear queue
7. rearQueue( ) : Displays an element at rear of the linear queue
8. traverse( ) : output the linear queue elements from front to rear

The Linear Queue ADT is translated into a Java interface as follows:


interface LinearQueueADT
{ void create( );
boolean isEmpty( );
int count( );
void insert(Object data);
Object delete( );
void frontQueue( );
void rearQueue();
void traverse( );
}
To implement the above said linear queue operations let us make the following
assumptions:
1. Let queue be an Object array with memory for specified capacity elements.

14
2. Let rear and front be two integer type identifiers such that elements are to be inserted at
rear and elements are to be deleted from front.
3. To delete an element, shift the subsequent elements of front one position left then decrement
rear by 1.
Note: 1. To run the following program first write import java.util.*; statement in the code.
2. Before writing the following implementation class write the interface LinearQueueADT.

class QueueArray implements QueueADT


{ int rear = -1, front = 0;
Object queue[];
int capacity = 10;
public void create()
{ rear = -1;
front = 0;
queue = new Object[capacity];
System.out.println("\nLinear Queue Created Successfully");
}
public boolean isEmpty()
{ if(rear == -1)
return true;
else
return false;
}
public int count()
{ return rear+1;
}
public void insert(Object data)
{ if(rear == capacity-1)
System.out.println("The linear queue is already full");
else
{ rear++;
queue[rear]=data;
System.out.println(data + " inserted successfully into the linear queue");
}
}
public Object delete()
{ Object removedElement = null;
if(rear == -1)
System.out.println("The linear queue is empty");
else
{ removedElement = queue[front];
for(int i=front;i<rear;i++)
queue[i] = queue[i+1];
rear--;
}
return removedElement;
}
public void frontQueue()
{ if(rear == -1)
System.out.println("Linear Queue is empty");
else

15
System.out.println("Element at front of the queue is : "+ queue[front]);
}
public void rearQueue()
{ if(rear == -1)
System.out.println("Linear Queue is empty");
else
System.out.println("Element at rear of the queue is : "+ queue[rear]);
}
public void traverse()
{ if(rear==-1)
System.out.println("The Linear queue is empty");
else
{
System.out.println("Elements in the linear queue are: ");
for(int i=front;i<=rear;i++)
System.out.println(queue[i]);
}
}
}

class QueueDemo
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
int ch;
QueueArray ob = new QueueArray();
do
{ System.out.println("1.Creating an empty linear queue");
System.out.println("2.Checking whether linear queue is empty or not");
System.out.println("3.Displaying number of elements in the linear
queue");
System.out.println("4.Inserting an element into the linear queue");
System.out.println("5.Deleting an element from the linear queue");
System.out.println("6.Displaying element at front of the queue");
System.out.println("7.Displaying element at rear of the queue");
System.out.println("8.Traversing elements in a linear queue");
System.out.println("9.Exit");
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Linear queue is empty : " +
ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the Linear
queue are: " + ob.count() );
break;
case 4 :
System.out.println("Enter the Element to insert into the
linear queue:");
int element = sc.nextInt();

16
ob.insert(element);
break;
case 5 :System.out.println("The element deleted from linear queue
is : " + ob.delete());
break;
case 6 :ob.frontQueue();
break;
case 7: ob.rearQueue();
break;
case 8 :ob.traverse();
break;
}
}while(ch>=1 && ch<=8);
}
}

11. Implementation of a Linear Queue using Single Linked List


Initially, the list is empty, so both the front and rear pointers are NULL. The insert
function creates a new node, puts the new data value in it, appends it to an existing list, and
makes the rear pointer point to it. A delete function checks whether the queue is empty, and if
not, retrieves the data value of the node pointed to by the front, advances the front, and frees the
storage of the node whose data value has been retrieved.
If the above strategy is used for creating a queue with four data values —10, 20, 30, and
40, the queue gets created as shown in the following figure:

The elements in the linear queue are of generic type Object. The elements form a linear
structure in which elements follow one after the other. The LinearQueue ADT supports the
following operations.
1. create( ) : Creates an empty linear queue.
2. isEmpty( ) : returns true if linear queue is empty, false otherwise
3. count( ) : returns the linear queue size
4. insert(x) : inserts x at rear of the linear queue
5. delete( ) : removes an element at front of the linear queue
6. frontQueue( ) : Displays an element at front of the linear queue
7. rearQueue( ) : Displays an element at rear of the linear queue
8. traverse( ) : output the linear queue elements from front to rear

17
The Linear Queue ADT is translated into a Java interface as follows:
interface LinearQueueADT
{ void create( );
boolean isEmpty( );
int count( );
void insert(Object data);
Object delete( );
void frontQueue( );
void rearQueue();
void traverse( );
}
To implement the above said linear queue operations let us make the following
assumptions:
1. The single linked list implementation of a linear queue requires a class Node which
contains two data members: info and link fields. The info field may contain any data of
type Object. The link is a reference to the next node in the list.
2. Let front and rear be two references to the queue such that elements are inserted at rear
and elements are deleted from front.
3. Let size be the integer type identifier which represents number of elements in the queue.
For each insertion and deletion operations the value of size should be incremented and
decremented by 1 respectively.
Note: 1. To run the following program first write import java.util.*; statement in the code.
2. Before writing the following implementation class write the interface LinearQueueADT.

class Node
{ Object info;
Node link;
}
class QueueSLL implements QueueADT
{ Node node, temp, p, rear, front;
int size = 0;
public void create()
{ rear = null;
front = null;
size = 0;
System.out.println("\nLinear Queue Created Successfully");
}
public boolean isEmpty()
{ if(size == 0)
return true;
else
return false;
}
public int count()
{ return size;
}
public void insert(Object data)
{ node = new Node();
node.info = data;
node.link = null;
if (size == 0)

18
{ front = node;
rear = node;
}
else
{ rear.link = node;
rear = node;
}
size++;
System.out.println(data + " inserted successfully into the linear queue");
}
public Object delete()
{ if(size == 0)
System.out.println("The linear queue is empty");
else
{ temp = front;
if( size == 1)
{ front = null;
rear = null;
}
else
{ front = front.link;
}
size--;
}
return temp.info;
}
public void front_queue()
{ if(size == 0)
System.out.println("Linear Queue is empty");
else
System.out.println("Element at front of the queue is : "+ front.info);
}
public void rear_queue()
{ if(size == 0)
System.out.println("Linear Queue is empty");
else
System.out.println("Element at rear of the queue is : "+ rear.info);
}

public void traverse()


{ if(size == 0)
System.out.println("The Linear queue is empty");
else
{ p = front;
System.out.println("Elements in the linear queue are: ");
while ( p!= null)
{
System.out.println(p.info);
p = p.link;
}
}

19
}
}

class QueueDemo1
{
public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
int ch;
QueueSLL ob = new QueueSLL();
do
{ System.out.println("1.Creating an empty linear queue");
System.out.println("2.Checking whether linear queue is empty or not");
System.out.println("3.Displaying number of elements in the linear
queue");
System.out.println("4.Inserting an element into the linear queue");
System.out.println("5.Deleting an element from the linear queue");
System.out.println("6.Displaying element at front of the queue");
System.out.println("7.Displaying element at rear of the queue");
System.out.println("8.Traversing elements in a linear queue");
System.out.println("9.Exit");
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Linear queue is empty : " +
ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the Linear
queue are: " + ob.count() );
break;
case 4 :
System.out.println("Enter the Element to insert into the
linear queue:");
int element = sc.nextInt();
ob.insert(element);
break;
case 5 :System.out.println("The element deleted from linear queue
is : " + ob.delete());
break;
case 6 :ob.front_queue();
break;
case 7: ob.rear_queue();
break;
case 8 :ob.traverse();
break;
}
}while(ch>=1 && ch<=8);
}
}

20
12. Implementation of a Linear Queue using Double Linked List
The elements in the linear queue are of generic type Object. The elements form a linear
structure in which elements follow one after the other. The LinearQueue ADT supports the
following operations.
1. create( ) : Creates an empty linear queue.
2. isEmpty( ) : returns true if linear queue is empty, false otherwise
3. count( ) : returns the linear queue size
4. insert(x) : inserts x at rear of the linear queue
5. delete( ) : removes an element at front of the linear queue
6. frontQueue( ) : Displays an element at front of the linear queue
7. rearQueue( ) : Displays an element at rear of the linear queue
8. traverse( ) : output the linear queue elements from front to rear
The Linear Queue ADT is translated into a Java interface as follows:
interface LinearQueueADT
{ void create( );
boolean isEmpty( );
int count( );
void insert(Object data);
Object delete( );
void frontQueue( );
void rearQueue();
void traverse( );
}
To implement the above said linear queue operations let us make the following
assumptions:
1. The double linked list implementation of a linear queue requires a class Node which
contains three data members: info, left and right fields. The info field may contain any
data of type Object. The left is a reference to the precessor node and right is a reference to
the successor node in the list.
2. Let size be the integer type identifier which represents number of elements in the list. For
each insertion and deletion operations the value of size should be incremented and
decremented by 1 respectively.
Note: 1. To run the following program first write import java.util.*; statement in the code.
2. Before writing the following implementation class write the interface LinearQueueADT.

class Node
{ Object info;
Node left, right;
}
class QueueDLL implements QueueADT
{ Node node, temp, p, rear, front;
int size = 0;
public void create()
{ rear = null;
front = null;
size = 0;
System.out.println("\nLinear Queue Created Successfully");
}
public boolean isEmpty()
{ if(size == 0)
return true;

21
else
return false;
}
public int count()
{ return size;
}
public void insert(Object data)
{ node = new Node();
node.info = data;
node.right = null;
node.left = null;
if (size == 0)
{ front = node;
rear = node;
}
else
{
rear.right = node;
node.left = rear;
rear = node;
}
size++;
System.out.println(data + " inserted successfully into the linear queue");
}
public Object delete()
{ if(size == 0)
System.out.println("The linear queue is empty");
else
{ temp = front;
if( size == 1)
{ front = null;
rear = null;
}
else
{ front = front.right;
front.left = null;
}
size--;
}
return temp.info;
}
public void front_queue()
{ if(size == 0)
System.out.println("Linear Queue is empty");
else
System.out.println("Element at front of the queue is : "+ front.info);
}
public void rear_queue()
{ if(size == 0)
System.out.println("Linear Queue is empty");
else

22
System.out.println("Element at rear of the queue is : "+ rear.info);
}
public void traverse()
{ if(size == 0)
System.out.println("The Linear queue is empty");
else
{ p = front;
System.out.println("Elements in the linear queue are: ");
while ( p!= null)
{
System.out.println(p.info);
p = p.right;
}
}
}
}
class QueueDemo2
{ public static void main(String args[ ])
{ Scanner sc = new Scanner(System.in);
int ch;
QueueDLL ob = new QueueDLL();
do
{ System.out.println("1.Creating an empty linear queue");
System.out.println("2.Checking whether linear queue is empty or not");
System.out.println("3.Displaying number of elements in the linear
queue");
System.out.println("4.Inserting an element into the linear queue");
System.out.println("5.Deleting an element from the linear queue");
System.out.println("6.Displaying element at front of the queue");
System.out.println("7.Displaying element at rear of the queue");
System.out.println("8.Traversing elements in a linear queue");
System.out.println("9.Exit");
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Linear queue is empty : " +
ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the Linear
queue are: " + ob.count() );
break;
case 4 :
System.out.println("Enter the Element to insert into the
linear queue:");
int element = sc.nextInt();
ob.insert(element);
break;
case 5 :System.out.println("The element deleted from linear queue
is : " + ob.delete());

23
break;
case 6 :ob.front_queue();
break;
case 7: ob.rear_queue();
break;
case 8 :ob.traverse();
break;
}
}while(ch>=1 && ch<=8);
}
}

13. Circular Queue: Circular Queue is also a queue which used the same principle FIFO
such that elements are inserted at rear and elements are deleted at front of the queue. But the only
difference is that it is assumed that the sub sequent position of the last location of the queue is the
first position. That is, if the size of the queue is n then the subsequent position of n should be 1.

We can perform the following operations on a circular queue:


 Creating an empty circular queue
 Determine whether the circular queue is empty or not
 Determine the size of the circular queue
 Insert an element into circular queue
 Delete an element from the circular queue
 Displaying element at front of the circular queue
 Displaying element at rear of the circular queue
 Traversing (displaying) all the elements present in the circular queue.

Circular Queue ADT: A circular queue may be specified as an abstract data type (ADT) in
which we provide a specification of the instances as well as of the operations that are to be
performed. The operations we need to perform on a linear queue can be expressed as methods in
an interface as follows:
interface CircularQueueADT
{ void create( );
boolean isEmpty( );
int count( );
void insert(Object data);
Object delete( );
void frontQueue( );
void rearQueue();
void traverse( );
}

Implementation of Circular Queue

24
Since a circular queue is also a list, it can be implemented using an array or it can be
implemented using a linked representation.

14. Array Implementation of a Circular Queue


The elements in the circular queue are of generic type Object. The elements form a linear
structure in which elements follow one after the other. The CircularQueue ADT supports the
following operations.
1. create( ) : Creates an empty circular queue.
2. isEmpty( ) : returns true if circular queue is empty, false otherwise
3. count( ) : returns the circular queue size
4. insert(x) : inserts x at rear of the linear queue
5. delete( ) : removes an element at front of the circular queue
6. frontQueue( ) : Displays an element at front of the circular queue
7. rearQueue( ) : Displays an element at rear of the circular queue
8. traverse( ) : output the circular queue elements from front to rear

The Circular Queue ADT is translated into a Java interface as follows:


interface CircularQueueADT
{ void create( );
boolean isEmpty( );
int count( );
void insert(Object data);
Object delete( );
void frontQueue( );
void rearQueue();
void traverse( );
}
To implement the above said circular queue operations let us make the following
assumptions:
1. Let queue be an Object array with memory for specified capacity elements.
2. Let front and rear be two integer type identifiers such that elements are inserted at rear
and elements are deleted from front.
3. Let size be the integer type identifier which represents number of elements in the circular
queue. For each insertion and deletion operation, the value of size should be incremented
and decremented by 1 respectively.
Note: 1. To run the following program first write import java.util.*; statement in the code.
2. Before writing the following implementation class write the interface CircularQueueADT.

class CQArray implements CircularQueueADT


{ int rear = 0, front = 1, size = 0 ;
Object queue[];
int capacity = 10;
public void create()
{ rear = 0;
front = 1;
size = 0;
queue = new Object[capacity + 1];
System.out.println("\nCircular Queue Created Successfully");
}
public boolean isEmpty()
{ if(size == 0)

25
return true;
else
return false;
}
public int count()
{ return size;
}
public void insert(Object data)
{ if(size == capacity)
System.out.println("The circular queue is already full");
else
{ rear = (rear % count) + 1;
queue[rear]=data;
size++;
System.out.println(data + " inserted successfully into the Circular queue");
}
}
public Object delete()
{ Object removedElement = null;
if(size == 0)
System.out.println("The Circular queue is empty");
else
{ removedElement = queue[front];
front = (front % count) + 1;
size--;
}
return removedElement;
}
public void frontQueue()
{ if(size == 0)
System.out.println("Circular Queue is empty");
else
System.out.println("Element at front of the queue is : "+ queue[front]);
}
public void rearQueue()
{ if(size == 0)
System.out.println("Circular Queue is empty");
else
System.out.println("Element at rear of the queue is : "+ queue[rear]);
}
public void traverse()
{ if( size == 0)
System.out.println("The Circular queue is empty");
else
{
System.out.println("Elements in the linear queue are: ");
if(front <= rear)
{
for(int i=front; i<=rear; i++)
System.out.println(queue[i]);
}

26
else
{
for(int i=front; i<=count; i++)
System.out.println(queue[i]);
for(int i=1; i<=rear; i++)
System.out.println(queue[i]);
}

}
}
}
class CQDemo
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
int ch;
CQArray ob = new CQArray( );
do
{ System.out.println("1.Creating an empty circular queue");
System.out.println("2.Checking whether circular queue is empty or not");
System.out.println("3.Displaying number of elements in the circular
queue");
System.out.println("4.Inserting an element into the circular queue");
System.out.println("5.Deleting an element from the circular queue");
System.out.println("6.Displaying element at front of the circular queue");
System.out.println("7.Displaying element at rear of the circular queue");
System.out.println("8.Traversing elements in a circular queue");
System.out.println("9.Exit");
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Linear queue is empty : " +
ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the Linear
queue are: " + ob.count() );
break;
case 4 :
System.out.println("Enter the Element to insert into the
linear queue:");
int element = sc.nextInt();
ob.insert(element);
break;
case 5 :System.out.println("The element deleted from linear queue
is : " + ob.delete());
break;
case 6 :ob.frontQueue();
break;
case 7: ob.rearQueue();
break;

27
case 8 :ob.traverse();
break;
}
}while(ch>=1 && ch<=8);
}
}

15. Implementation of a Circular Queue using Single Linked List


The elements in the circular queue are of generic type Object. The elements form a linear
structure in which elements follow one after the other. The CircularQueue ADT supports the
following operations.
1. create( ) : Creates an empty circular queue.
2. isEmpty( ) : returns true if circular queue is empty, false otherwise
3. count( ) : returns the circular queue size
4. insert(x) : inserts x at rear of the linear queue
5. delete( ) : removes an element at front of the circular queue
6. frontQueue( ) : Displays an element at front of the circular queue
7. rearQueue( ) : Displays an element at rear of the circular queue
8. traverse( ) : output the circular queue elements from front to rear

The Circular Queue ADT is translated into a Java interface as follows:


interface CircularQueueADT
{ void create( );
boolean isEmpty( );
int count( );
void insert(Object data);
Object delete( );
void frontQueue( );
void rearQueue();
void traverse( );
}
To implement the above said circular queue operations let us make the following
assumptions:
1. The single linked list implementation of a circular queue requires a class Node which
contains two data members: info and link fields. The info field may contain any data of
type Object. The link is a reference to the next node in the list.
2. Let front and rear be two references to the queue such that elements are inserted at rear
and elements are deleted from front.
3. Let size be the integer type identifier which represents number of elements in the queue.
For each insertion and deletion operations the value of size should be incremented and
decremented by 1 respectively.
Note: 1. To run the following program first write import java.util.*; statement in the code.
2. Before writing the following implementation class write the interface CircularQueueADT.

class Node
{ Object info;
Node link;
}
class CQSLL implements CircularQueueADT
{ Node node, temp, p, rear, front;

28
int size = 0;
public void create()
{ rear = null;
front = null;
size = 0;
System.out.println("\nCircular Queue Created Successfully");
}
public boolean isEmpty()
{ if(size == 0)
return true;
else
return false;
}
public int count()
{ return size;
}
public void insert(Object data)
{ node = new Node();
node.info = data;
node.link = null;
if (size == 0)
{ front = node;
rear = node;
}
else
{
rear.link = node;
rear = node;
}
rear.link = front;
size++;
System.out.println(data + " inserted successfully into the Circular queue");
}
public Object delete()
{ if(size == 0)
System.out.println("The Circular queue is empty");
else
{ temp = front;
if( size == 1)
{ front = null;
rear = null;
}
else
{ front = front.link;
}
rear.link = front;
size--;
}
return temp.info;
}
public void front_queue()

29
{ if(size == 0)
System.out.println("Circular Queue is empty");
else
System.out.println("Element at front of the queue is : "+ front.info);
}
public void rear_queue()
{ if(size == 0)
System.out.println("Circular Queue is empty");
else
System.out.println("Element at rear of the queue is : "+ rear.info);
}
public void traverse()
{ if(size == 0)
System.out.println("The Circular queue is empty");
else
{ p = front;
System.out.println("Elements in the Circular queue are: ");
do
{
System.out.println(p.info);
p = p.link;
} while ( p!= front)
}
}
}
class CQDemo1
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
int ch;
CQSLL ob = new CQSLL( );
do
{ System.out.println("1.Creating an empty Circular queue");
System.out.println("2.Checking whether Circular queue is empty or not");
System.out.println("3.Displaying number of elements in the Circular
queue");
System.out.println("4.Inserting an element into the Circular queue");
System.out.println("5.Deleting an element from the Circular queue");
System.out.println("6.Displaying element at front of the queue");
System.out.println("7.Displaying element at rear of the queue");
System.out.println("8.Traversing elements in a Circular queue");
System.out.println("9.Exit");
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Circular queue is empty : " +
ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the Circular
queue are: " + ob.count() );

30
break;
case 4 :
System.out.println("Enter the Element to insert into the
Circular queue:");
int element = sc.nextInt();
ob.insert(element);
break;
case 5 :System.out.println("The element deleted from Circular
queue is : " + ob.delete());
break;
case 6 :ob.front_queue();
break;
case 7: ob.rear_queue();
break;
case 8 :ob.traverse();
break;
}
}while(ch>=1 && ch<=8);
}
}

16. Implementation of a Circular Queue using Double Linked List


The elements in the Circular queue are of generic type Object. The elements form a linear
structure in which elements follow one after the other. The CircularQueue ADT supports the
following operations.
1. create( ) : Creates an empty circular queue.
2. isEmpty( ) : returns true if circular queue is empty, false otherwise
3. count( ) : returns the circular queue size
4. insert(x) : inserts x at rear of the linear queue
5. delete( ) : removes an element at front of the circular queue
6. frontQueue( ) : Displays an element at front of the circular queue
7. rearQueue( ) : Displays an element at rear of the circular queue
8. traverse( ) : output the circular queue elements from front to rear
The Circular Queue ADT is translated into a Java interface as follows:
interface CircularQueueADT
{ void create( );
boolean isEmpty( );
int count( );
void insert(Object data);
Object delete( );
void frontQueue( );
void rearQueue();
void traverse( );
}
To implement the above said circular queue operations let us make the following
assumptions:
1. The double linked list implementation of a circular queue requires a class Node which
contains three data members: info, left and right fields. The info field may contain any
data of type Object. The left is a reference to the processor node and right is a reference
to the successor node in the list.

31
2. Let front and rear be two references to the circular queue such that elements are inserted
at rear and elements are deleted from front.
3. Let size be the integer type identifier which represents number of elements in the queue.
For each insertion and deletion operations the value of size should be incremented and
decremented by 1 respectively.
Note: 1. To run the following program first write import java.util.*; statement in the code.
2. Before writing the following implementation class write the interface CircularQueueADT.

class Node
{ Object info;
Node left, right;
}
class CQDLL implements CircularQueueADT
{ Node node, temp, p, rear, front;
int size = 0;
public void create()
{ rear = null;
front = null;
size = 0;
System.out.println("\nCircular Queue Created Successfully");
}
public boolean isEmpty()
{ if(size == 0)
return true;
else
return false;
}
public int count()
{ return size;
}
public void insert(Object data)
{ node = new Node();
node.info = data;
node.right = null;
node.left = null;
if (size == 0)
{ front = node;
rear = node;
}
else
{
rear.right = node;
node.left = rear;
rear = node;
}
rear.right = front;
front.left = rear;
size++;
System.out.println(data + " inserted successfully into the Circular queue");
}
public Object delete()

32
{ if(size == 0)
System.out.println("The Circular queue is empty");
else
{ temp = front;
if( size == 1)
{ front = null;
rear = null;
}
else
{ front = front.right;
front.left = rear;
rear.right = front;
}
size--;
}
return temp.info;
}
public void front_queue()
{ if(size == 0)
System.out.println("Circular Queue is empty");
else
System.out.println("Element at front of the queue is : "+ front.info);
}
public void rear_queue()
{ if(size == 0)
System.out.println("Circular Queue is empty");
else
System.out.println("Element at rear of the queue is : "+ rear.info);
}
public void traverse()
{ if(size == 0)
System.out.println("The Circular queue is empty");
else
{ p = front;
System.out.println("Elements in the Circular queue are: ");
do
{
System.out.println(p.info);
p = p.right;
} while ( p!= front);
}
}
}
class CQDemo2
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
int ch;
CQDLL ob = new CQDLL();
do
{ System.out.println("1.Creating an empty Circular queue");
System.out.println("2.Checking whether Circular queue is empty or not");

33
System.out.println("3.Displaying number of elements in the Circular
queue");
System.out.println("4.Inserting an element into the Circular queue");
System.out.println("5.Deleting an element from the Circular queue");
System.out.println("6.Displaying element at front of the queue");
System.out.println("7.Displaying element at rear of the queue");
System.out.println("8.Traversing elements in a Circular queue");
System.out.println("9.Exit");
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Circular queue is empty : " +
ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the Circular
queue are: " + ob.count() );
break;
case 4 :
System.out.println("Enter the Element to insert into the
Circular queue:");
int element = sc.nextInt();
ob.insert(element);
break;
case 5 :System.out.println("The element deleted from Circular
queue is : " + ob.delete());
break;
case 6 :ob.front_queue();
break;
case 7: ob.rear_queue();
break;
case 8 :ob.traverse();
break;
}
}while(ch>=1 && ch<=8);
}
}

17. Doubled Ended Queue: Deque is also a linear queue which stands for double ended
queue. In a deque, we can insert and delete elements at both the ends front and rear. Deques are
categorized into two types that is input restricted deque and output restricted deque. In input
restricted deque insertion is allowed at only at rear end but deletion is allowed at both the front
and rear ends. In output restricted deque deletion is allowed at only at front end but deletion is
allowed at both the front and rear ends.

34
We can perform the following operations on a deque:
 Creating an empty deque
 Determine whether the deque is empty or not
 Determine the size of the deque
 Insert an element at front of the deque
 Insert an element at rear of the deque
 Delete an element from front of the deque
 Delete an element from rear of the deque
 Displaying element at front of the deque
 Displaying element at rear of the deque
 Traversing (displaying) all the elements present in the deque.

Deque ADT: A deque may be specified as an abstract data type (ADT) in which we provide a
specification of the instances as well as of the operations that are to be performed. The operations
we need to perform on a deque can be expressed as methods in an interface as follows:
interface DequeADT
{ void create( );
boolean isEmpty( );
int count( );
void frontInsert(Object data);
void rearInsert(Object data);
Object frontDelete( );
Object rearDelete( );
void frontDeque( );
void rearDeque();
void traverse( );
}
Implementation of Deque
Since a deque is also a list, it can be implemented using an array or it can be implemented
using a linked representation.

18. Array Implementation of a Deque


The elements in the deque are of generic type Object. The elements form a linear
structure in which elements follow one after the other. The LinearQueue ADT supports the
following operations.
1. create( ) : Creates an empty deque.
2. isEmpty( ) : returns true if deque is empty, false otherwise
3. count( ) : returns the deque size
4. frontInsert(x) : inserts x at front of the deque
5. rearInsert(x) : inserts x at rear of the deque
6. frontDelete( ) : removes and element at front of the deque
7. rearDelete( ) : removes an element at rear of the deque

35
8. frontDeque( ) : Displays an element at front of the deque
9. rearDeque( ) : Displays an element at rear of the deque
10. traverse( ) : output the deque elements from front to rear

The Deque ADT is translated into a Java interface as follows:


interface DequeADT
{ void create( );
boolean isEmpty( );
int count( );
void frontInsert(Object data);
void rearInsert(Object data);
Object frontDelete( );
Object rearDelete( );
void frontDeque( );
void rearDeque();
void traverse( );
}
To implement the above said deque operations let us make the following assumptions:
1. Let deque be an Object array with memory for specified capacity elements.
2. Let rear and front be two integer type identifiers such that elements are to be inserted at
rear and elements are to be deleted from front.
3. To delete an element, shift the subsequent elements of front one position left then
decrement rear by 1.
Note: 1. To run the following program first write import java.util.*; statement in the code.
2. Before writing the following implementation class write the interface LinearDequeADT.

class DequeArray implements DequeADT


{ int rear = -1, front = 0;
Object queue[];
int capacity = 10;
public void create()
{ rear = -1;
front = 0;
queue = new Object[capacity];
System.out.println("\nDeque Created Successfully");
}
public boolean isEmpty()
{ if(rear == -1)
return true;
else
return false;
}
public int count()
{ return rear+1;
}
public void frontInsert(Object data)
{ if(rear == capacity-1)
System.out.println("The deque is already full");
else
{ for(int i= rear;i>=0;i++)

36
Queue[i+1] = queue[i];
queue[front]=data;
rear++;
System.out.println(data + " inserted successfully into the deque");
}
}
public void rearInsert(Object data)
{ if(rear == capacity-1)
System.out.println("The deque is already full");
else
{ rear++;
queue[rear]=data;
System.out.println(data + " inserted successfully into the deque");
}
}
public Object frontDelete( )
{ Object removedElement = null;
if(rear == -1)
System.out.println("The deque is empty");
else
{ removedElement = queue[front];
for(int i=front;i<rear;i++)
queue[i] = queue[i+1];
rear--;
}
return removedElement;
}
public Object rearDelete( )
{
Object removedElement = null;
if(rear == -1)
System.out.println("The deque is empty");
else
{ removedElement = queue[rear];
rear--;
}
return removedElement;
}
public void frontDeque()
{ if(rear == -1)
System.out.println("Deque is empty");
else
System.out.println("Element at front of the deque is : "+ queue[front]);
}
public void rearDeque()
{ if(rear == -1)
System.out.println("Deque is empty");
else
System.out.println("Element at rear of the deque is : "+ queue[rear]);
}
public void traverse()

37
{ if(rear==-1)
System.out.println("The Deque is empty");
else
{
System.out.println("Elements in the deque are: ");
for(int i=front;i<=rear;i++)
System.out.println(queue[i]);
}
}
}
class DequeDemo
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
int ch;
DequeArray ob = new DequeArray();
do
{ System.out.println("1.Creating an empty deque");
System.out.println("2.Checking whether deque is empty or not");
System.out.println("3.Displaying number of elements in the deque");
System.out.println("4.Inserting an element at front of the deque");
System.out.println("5.Inserting an element at rear of the deque");
System.out.println("6.Deleting an element at front of the deque");
System.out.println("7.Deleting an element at rear of the deque");
System.out.println("8.Displaying element at front of the deque");
System.out.println("9.Displaying element at rear of the deque");
System.out.println("10.Traversing elements in a deque");
System.out.println("11.Exit");
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Deque is empty : " +
ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the Deque are deque are: " + ob.count() );
break;
case 4 :
System.out.println("Enter the Element to insert into the
deque:");
int element = sc.nextInt();
ob.frontInsert(element);
break;
case 5 :
System.out.println("Enter the Element to insert into the
deque:");
element = sc.nextInt();
ob.rearInsert(element);
break;
case 6 :System.out.println("The element deleted from front of the
deque is : " + ob.frontDelete());

38
break;
case 7 :System.out.println("The element deleted from rear of the
deque is : " + ob.rearDelete());
break;
case 8 :ob.frontDeque();
break;
case 9: ob.rearDeque();
break;
case 10 :ob.traverse();
break;
}
}while(ch>=1 && ch<=10);
}
}

19. Implementation of a Deque using Single Linked List


The elements in the deque are of generic type Object. The elements form a linear structure in
which elements follow one after the other. The deque ADT supports the following operations.
1. create( ) : Creates an empty deque.
2. isEmpty( ) : returns true if deque is empty, false otherwise
3. count( ) : returns the deque size
4. frontInsert(x) : inserts x at front of the deque
5. rearInsert(x) : inserts x at rear of the deque
6. frontDelete( ) : removes and element at front of the deque
7. rearDelete( ) : removes an element at rear of the deque
8. frontDeque( ) : Displays an element at front of the deque
9. rearDeque( ) : Displays an element at rear of the deque
10. traverse( ) : output the deque elements from front to rear
The Deque ADT is translated into a Java interface as follows:
interface DequeADT
{ void create( );
boolean isEmpty( );
int count( );
void frontInsert(Object data);
void rearInsert(Object data);
Object frontDelete( );
Object rearDelete( );
void frontDeque( );
void rearDeque();
void traverse( );
}
To implement the above said deque operations let us make the following assumptions:
1. The single linked list implementation of a deque requires a class Node which contains
two data members: info and link fields. The info field may contain any data of type
Object. The link is a reference to the next node in the list.
2. Let front and rear be two references to the queue such that elements are inserted at rear
and elements are deleted from front.
3. Let size be the integer type identifier which represents number of elements in the queue.
For each insertion and deletion operations the value of size should be incremented and
decremented by 1 respectively.
Note: 1. To run the following program first write import java.util.*; statement in the code.

39
2. Before writing the following implementation class write the interface DequeADT.

class Node
{ Object info;
Node link;
}
class DequeSLL implements DequeADT
{ Node node, temp, p, rear, front;
int size = 0;
public void create()
{ rear = null;
front = null;
size = 0;
System.out.println("\nDeque Created Successfully");
}
public boolean isEmpty( )
{ if(size == 0)
return true;
else
return false;
}
public int count( )
{ return size;
}

public void frontInsert(Object data)


{ node = new Node();
node.info = data;
node.link = null;
if (size == 0)
{ front = node;
rear = node;
}
else
{
node.link = front;
front = node;
}
size++;
System.out.println(data + " inserted successfully into the deque");
}
public void rearInsert(Object data)
{ node = new Node();
node.info = data;
node.link = null;
if (size == 0)
{ front = node;
rear = node;
}
else
{ rear.link = node;

40
rear = node;
}
size++;
System.out.println(data + " inserted successfully into the deque");
}
public Object frontDelete()
{ if(size == 0)
System.out.println("The deque is empty");
else
{ temp = front;
if( size == 1)
{ front = null;
rear = null;
}
else
{ front = front.link;
}
size--;
}
return temp.info;
}
public Object rearDelete()
{ if(size == 0)
System.out.println("The deque is empty");
else
{ temp = rear;
if( size == 1)
{ front = null;
rear = null;
}
else
{ p = front;
for(int i=0; i < size-2; i++)
p = p.link;
rear = p;
rear.link = null;
}
size--;
}
return temp.info;
}

public void frontDeque()


{ if(size == 0)
System.out.println("Deque is empty");
else
System.out.println("Element at front of the queue is : "+ front.info);
}
public void rearDeque()
{ if(size == 0)
System.out.println("Deque is empty");

41
else
System.out.println("Element at rear of the queue is : "+ rear.info);
}
public void traverse()
{ if(size == 0)
System.out.println("The Deque is empty");
else
{ p = front;
System.out.println("Elements in the deque are: ");
while ( p!= null)
{
System.out.println(p.info);
p = p.link;
}
}
}
}
class DequeDemo1
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
int ch;
DequeSLL ob = new DequeSLL();
do
{ System.out.println("1.Creating an empty deque");
System.out.println("2.Checking whether deque is empty or not");
System.out.println("3.Displaying number of elements in the deque");
System.out.println("4.Inserting an element at front of the deque");
System.out.println("5.Inserting an element at rear of the deque");
System.out.println("6.Deleting an element at front of the deque");
System.out.println("7.Deleting an element at rear of the deque");
System.out.println("8.Displaying element at front of the deque");
System.out.println("9.Displaying element at rear of the deque");
System.out.println("10.Traversing elements in a deque");
System.out.println("11.Exit");
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Deque is empty : " +
ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the Deque are deque are: " + ob.count() );
break;
case 4 :
System.out.println("Enter the Element to insert into the
deque:");
int element = sc.nextInt();
ob.frontInsert(element);
break;
case 5 :

42
System.out.println("Enter the Element to insert into the
deque:");
element = sc.nextInt();
ob.rearInsert(element);
break;
case 6 :System.out.println("The element deleted from front of the
deque is : " + ob.frontDelete());
break;
case 7 :System.out.println("The element deleted from rear of the
deque is : " + ob.rearDelete());
break;
case 8 :ob.frontDeque();
break;
case 9: ob.rearDeque();
break;
case 10 :ob.traverse();
break;
}
}while(ch>=1 && ch<=10);
}
}

20. Implementation of a Deque using Double Linked List


The elements in the deque are of generic type Object. The elements form a linear
structure in which elements follow one after the other. The Deque ADT supports the following
operations.
1. create( ) : Creates an empty deque.
2. isEmpty( ) : returns true if deque is empty, false otherwise
3. count( ) : returns the deque size
4. frontInsert(x) : inserts x at front of the deque
5. rearInsert(x) : inserts x at rear of the deque
6. frontDelete( ) : removes and element at front of the deque
7. rearDelete( ) : removes an element at rear of the deque
8. frontDeque( ) : Displays an element at front of the deque
9. rearDeque( ) : Displays an element at rear of the deque
10. traverse( ) : output the deque elements from front to rear

The Deque ADT is translated into a Java interface as follows:


interface DequeADT
{ void create( );
boolean isEmpty( );
int count( );
void frontInsert(Object data);
void rearInsert(Object data);
Object frontDelete( );
Object rearDelete( );
void frontDeque( );
void rearDeque();
void traverse( );
}
To implement the above said deque operations let us make the following assumptions:

43
1. The double linked list implementation of a deque requires a class Node which contains
three data members: info, left and right fields. The info field may contain any data of type
Object. The left is a reference to the precessor node and right is a reference to the
successor node in the list.
2. Let size be the integer type identifier which represents number of elements in the list. For
each insertion and deletion operations the value of size should be incremented and
decremented by 1 respectively.
Note: 1. To run the following program first write import java.util.*; statement in the code.
2. Before writing the following implementation class write the interface DequeADT.
class Node
{ Object info;
Node left, right;
}
class DequeDLL implements DequeADT
{ Node node, temp, p, rear, front;
int size = 0;
public void create()
{ rear = null;
front = null;
size = 0;
System.out.println("\nDeque Created Successfully");
}
public boolean isEmpty()
{ if(size == 0)
return true;
else
return false;
}
public int count()
{ return size;
}
public void frontInsert(Object data)
{ node = new Node();
node.info = data;
node.right = null;
node.left = null;
if (size == 0)
{ front = node;
rear = node;
}
else
{
node.right = front;
front.left = node;
front = node;
}
size++;
System.out.println(data + " inserted successfully into the deque");
}
public void rearInsert(Object data)
{ node = new Node();

44
node.info = data;
node.right = null;
node.left = null;
if (size == 0)
{ front = node;
rear = node;
}
else
{
rear.right = node;
node.left = rear;
rear = node;
}
size++;
System.out.println(data + " inserted successfully into the deque");
}
public Object frontDelete()
{ if(size == 0)
System.out.println("The deque is empty");
else
{ temp = front;
if( size == 1)
{ front = null;
rear = null;
}
else
{ front = front.right;
front.left = null;
}
size--;
}
return temp.info;
}
public Object rearDelete()
{ if(size == 0)
System.out.println("The deque is empty");
else
{ temp = rear;
if( size == 1)
{ front = null;
rear = null;
}
else
{ rear = rear.left;
rear.right = null;
}
size--;
}
return temp.info;
}
public void frontDeque()

45
{ if(size == 0)
System.out.println("Deque is empty");
else
System.out.println("Element at front of the queue is : "+ front.info);
}
public void rearDeque()
{ if(size == 0)
System.out.println("Deque is empty");
else
System.out.println("Element at rear of the queue is : "+ rear.info);
}
public void traverse()
{ if(size == 0)
System.out.println("The Deque is empty");
else
{ p = front;
System.out.println("Elements in the deque are: ");
while ( p!= null)
{
System.out.println(p.info);
p = p.right;
}
}
}
}
class DequeDemo2
{ public static void main(String args[])
{ Scanner sc = new Scanner(System.in);
int ch;
DequeDLL ob = new DequeDLL( );
do
{ System.out.println("1.Creating an empty deque");
System.out.println("2.Checking whether deque is empty or not");
System.out.println("3.Displaying number of elements in the deque");
System.out.println("4.Inserting an element at front of the deque");
System.out.println("5.Inserting an element at rear of the deque");
System.out.println("6.Deleting an element at front of the deque");
System.out.println("7.Deleting an element at rear of the deque");
System.out.println("8.Displaying element at front of the deque");
System.out.println("9.Displaying element at rear of the deque");
System.out.println("10.Traversing elements in a deque");
System.out.println("11.Exit");
System.out.print("\nEnter your choice:");
ch = sc.nextInt();
switch(ch)
{ case 1 :ob.create();
break;
case 2 :System.out.println("Deque is empty : " +
ob.isEmpty());
break;
case 3 :System.out.println("Number of elements in the Deque are deque are: " + ob.count() );

46
break;
case 4 :
System.out.println("Enter the Element to insert into the
deque:");
int element = sc.nextInt();
ob.frontInsert(element);
break;
case 5 :
System.out.println("Enter the Element to insert into the
deque:");
element = sc.nextInt();
ob.rearInsert(element);
break;
case 6 :System.out.println("The element deleted from front of the
deque is : " + ob.frontDelete());
break;
case 7 :System.out.println("The element deleted from rear of the
deque is : " + ob.rearDelete());
break;
case 8 :ob.frontDeque();
break;
case 9: ob.rearDeque();
break;
case 10 :ob.traverse();
break;
}
}while(ch>=1 && ch<=10);
}
}

47

Вам также может понравиться