0% found this document useful (0 votes)
8 views6 pages

Unit-Ii Ds-Ug

This document covers data structures focusing on stacks and queues, detailing their definitions, Abstract Data Types (ADTs), implementations using arrays, and various applications. It explains stack operations such as push, pop, and display, as well as queue operations like insert and delete. Additionally, it discusses different types of queues including simple, circular, priority, and doubly ended queues, along with their applications in CPU scheduling and interrupt handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Unit-Ii Ds-Ug

This document covers data structures focusing on stacks and queues, detailing their definitions, Abstract Data Types (ADTs), implementations using arrays, and various applications. It explains stack operations such as push, pop, and display, as well as queue operations like insert and delete. Additionally, it discusses different types of queues including simple, circular, priority, and doubly ended queues, along with their applications in CPU scheduling and interrupt handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DATA STRUCTURES

UNIT II
Stacks: Definition, ADT, Array and Linked representations,
Implementations and Applications
Queues: Definition, ADT, Array and Linked representations,
Circular Queues, Dequeues, Priority Queues,
Implementations and Applications.

Stack Definition: A stack is a linear list in which insertions and


deletions take place at the same end. This end is called the top, the
other end is called the bottom.

Fig(a) shows a stack which contains A, B, C, D elements. Add a new


element E to the stack of Fig(a).This element will have to be placed
on top of element D as shown in Fig(b). Delete element E, D, C from
the stack of Fig(b).After deleting these elements the stack is as
shown in Fig(c).

Stack ADT: A stack is a linear list in which insertions and deletions


take place at the top end and following operations can be performed
on the stack.
push() – Insert an element at top position of the stack.
pop() – Remove the element from the top position in a non-empty
stack.
isEmpty() – Return true if the stack is empty, otherwise return false.
isFull() – Return true if the stack is full, otherwise return false.
display() – Displays all the elements in the list.

Array Implementation of Stack: When a stack is implemented


using an array, that stack size is fixed and can add only limited
number of elements.
// Java Program to implement the Stack ADT using an array
class StackArray
{
float s[];
int size,top;
StackArray(int max)
{
size=max;
s = new float[size];

Krishnaveni Degree & PG College :: Narasaraopet Page No. : 1


DATA STRUCTURES
UNIT II
top=-1;
}
boolean isEmpty()
{
return(top==-1);
}
boolean isFull()
{
return(top==size-1);
}
void push(float value)
{
s[++top]=value;
}
float pop()
{
return(s[top--]);
}
void display()
{
int i;
if(!isEmpty())
{
System.out.print("The elements in STACK are as follows\nTop-->
");
for(i=top; i>=0; i--)
System.out.print(s[i]+"\n\t");
System.out.print("\n");
}
else
System.out.println("The STACK is empty\n");
}
}

Queue Definition: A queue is a linear list in which insertion is done


at front end and deletion is done at rear end. The front end is called
the front, the rear end is called the rear.

Krishnaveni Degree & PG College :: Narasaraopet Page No. : 2


DATA STRUCTURES
UNIT II

Fig(a) shows a queue which contains A, B, C, D elements. Add a new


element E to the queue of Fig(a).This element will have is added at
the rear end of the queue as shown in Fig(b). Delete element A, B, C
from the queue of Fig(b). Elements will be deleted from the front end
after deleting these elements the queue is as shown in Fig(c).

Queue ADT: A queue is a linear list in which insertion is done at


front end and deletion is done at rear end and following operations
can be performed on the queue.
insert() – Insert an element at top position of the stack.
delete() – Remove the element from the top position in a non-empty
stack.
isEmpty() – Return true if the stack is empty, otherwise return false.
isFull() – Return true if the stack is full, otherwise return false.
display() – Displays all the elements in the list.

Array Implementation of Queue: When a queue is implemented


using an array, that stack size is fixed and can add only limited
number of elements.

// Java Program to implement the Queue operations using an array


class QueueArray
{
float q[];
int size,front,rear;
QueueArray(int max)
{
size=max;
q = new float[size];
front=0;
rear=-1;
}
boolean isFull()
{
return(rear==size-1);
}
boolean isEmpty()
{
return((front==-1)&&(rear==-1));
}
Krishnaveni Degree & PG College :: Narasaraopet Page No. : 3
DATA STRUCTURES
UNIT II
void insert(float value)
{
if(isEmpty())
{
q[++rear]=value;
front=rear;
}
else
q[++rear]=value;
}
float remove()
{
float val;
val=q[front];
if(front==rear)
front=rear=-1;
else
front++;
return(val);
}
void display()
{
int i;
if(!isEmpty())
{
System.out.print("The elements in Queue are as follows\nFront--
>");
for(i=front; i<=rear; i++)
System.out.print(" "+q[i]+" -> ");
System.out.print("Rear\n");
}
else
System.out.print("The Queue is empty\n");
}
}

Applications of Stack: There are many applications of stack.


Commonly used applications are
1. Expression Evaluation
2. Expression Conversion
3. Syntax Parsing
4. Backtracking
5. Parenthesis Checking
Krishnaveni Degree & PG College :: Narasaraopet Page No. : 4
DATA STRUCTURES
UNIT II
6. String Reversal
7. Function Call
Expression Evaluation: An expression can be represented in prefix,
postfix or infix notation. Stack is used to evaluate prefix, postfix and
infix expressions.
Expression Conversion: An expression can be represented in prefix,
postfix or infix notation. Stack can be used to convert one form of
expression to another.
Syntax Parsing: Many compilers use a stack for parsing the syntax of
expressions, program blocks etc. before translating into low level
code.
Backtracking: Suppose we are finding a path for solving maze
problem. We choose a path and after following it we realize that it is
wrong. Now we need to go back to the beginning of the path to start
with new path. This can be done with the help of stack.
Parenthesis Checking: Stack is used to check the proper opening and
closing of parenthesis.
String Reversal: Stack is used to reverse a string. We push the
characters of string one by one into stack and then pop character
from stack.
Function Call: Stack is used to keep information about the active
functions or subroutines.

Applications of Queue: There are many applications of queue.


Commonly used applications are
1. CPU Scheduling
2. Interrupt Handling
CPU Scheduling: CPU Scheduling decides the order in which the
tasks are to be completed. It performs the task in the order in which
they arrive i.e. first come first served. Thus CPU uses queue to
perform task Scheduling.
Interrupt Handling: Interrupt is a disturbance created during the
execution of task. CPU has to handle these interrupts. The interrupts
are handled in the same order as they arrive i.e First come first
served. Thus CPU uses queue to handle interrupts.

Types of Queues: Queue is an important concept of the data


structures and understanding their types is very necessary for
working appropriately with them.
1. Simple Queue
2. Circular Queue
3. Priority Queue
4. Doubly Ended Queue (Dequeue)
Krishnaveni Degree & PG College :: Narasaraopet Page No. : 5
DATA STRUCTURES
UNIT II
Simple queue: Simple queue allows to perform insertion and
deletions operations simply. i.e., insertion occurs at the rear (end) of
the queue and deletions are performed at the front (beginning) of
the queue list.

All nodes are connected to each other in a sequential manner. The


pointer of the first node points to the value of the second and so on.
The first node has no pointer pointing towards it whereas the last
node has no pointer pointing out from it.
Circular Queue:

Image Source

Unlike the simple queues, in a circular queue each node is


connected to the next node in sequence but the last node’s pointer
is also connected to the first node’s address. Hence, the last node
and the first node also gets connected making a circular link overall.

Priority Queue

Image Source

Priority queue makes data retrieval possible only through a pre


determined priority number assigned to the data items.

While the deletion is performed in accordance to priority number


(the data item with highest priority is removed first), insertion is
performed only in the order.

Doubly Ended Queue (Dequeue)


Doubly Ended Queue

Image Source

The doubly ended queue or dequeue allows the insert and delete
operations from both ends (front and rear) of the queue.

Krishnaveni Degree & PG College :: Narasaraopet Page No. : 6

You might also like