0% found this document useful (0 votes)
12 views21 pages

UNIT-III DSUC-UG

This document covers data structures focusing on stacks and queues, detailing their definitions, abstract data types, operations, and implementations using arrays and linked lists. It explains stack operations such as push, pop, and display, along with applications like expression evaluation and recursion. Additionally, it discusses queue operations, types, and their implementations, highlighting simple, circular, and priority queues.
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)
12 views21 pages

UNIT-III DSUC-UG

This document covers data structures focusing on stacks and queues, detailing their definitions, abstract data types, operations, and implementations using arrays and linked lists. It explains stack operations such as push, pop, and display, along with applications like expression evaluation and recursion. Additionally, it discusses queue operations, types, and their implementations, highlighting simple, circular, and priority queues.
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/ 21

DATA STRUCTURES UNIT: III

USING
Satcks:C Introduction to stacks, Stack as an Abstract Data
Type, Representation of stacks through Arrays,
Representation of Stacks through Linked Lists, Applications
of Stacks, 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

Introduction to Stacks: 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 as an Abstract Data Type: A stack is a linear list in


which insertions and deletions take place at the top end. An
abstract data type (ADT) is the specification of a data type
within some language, independent of an implementation. The
Stack ADT contains the following operations that are 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.

Insert an element at top position of the stack:


Algorithm: push(int
value) Step 1: If Top
=Size-1
Print “Stack is
Full” Else
{
Top=Top+1
;
S[Top]=valu
e;
DATA STRUCTURES UNIT: III
USING C }
DATA STRUCTURES UNIT: III
USING
ProcedureC to push data 1, 2,3 into the stack:
Initially the stack is empty i.e Top=-1 and Size=2.

Read Data i.e 1 and push the data into the stack. Check the top
value before pushing the data into the stack. If top is equal to
Size-1 Print ”Stack is Full”. If top is not equal to Size-1 then
increment the top value and insert data into the stack at top value.

Read Data i.e 2 and push the data into the stack. Check the top
value before pushing the data into the stack. If top is equal to
Size-1 Print ―Stack is Full‖. If top is not equal to Size-1 then
increment the top value and insert data into the stack at top value.

Read Data i.e 3 and push the data into the stack. Check the top
value before pushing the data into the stack. As top is equal to
Size-1 Print ―Stack is Full‖ and data 3 is not pushed onto the stack.

Remove an element from top position of the stack:


Algorithm: pop()
Step 1:
If(Top==-1)
Print ”Stack is
Empty” Else
{
Num=S[To
p]
Top=Top-1
Return
Num
}
Procedure to remove data 2,1 from the stack:
Initially the stack contains two elements i.e Top=1.
DATA STRUCTURES UNIT: III
USING C

Remove Data i.e 2 from the stack. Check the top value before
deleting the data from the stack. If top is equal to -1 Print ―Stack is
Empty‖. If top is not equal to - 1 then get the value at the top into

num and then decrement the top value.


Remove Data i.e 1 from the stack. Check the top value before
deleting the data from the stack. If top is equal to -1 Print ―Stack is
Empty‖. If top is not equal to - 1 then get the value at the top into
num and then decrement the top value.

Remove Data i.e. 3 from the stack. Check the top value before deleting
the data from the stack. As top is equal to -1 Print ―Stack is Empty‖.

Displaying the elements from the stack:


Algorithm:
display() Step 1:
If(Top==-1)
Print ”Stack is
Empty” Else
{
Print "The elements in STACK are as follows\nTop--
> " I=Top
While (I>=0)
{
print
S[i])
I=I-1
}
DATA STRUCTURES UNIT: III
USING C }
DATA STRUCTURES UNIT: III
USING C to display the elements from the stack:
Procedure
Initially the stack contains two elements i.e Top=1.

Check the top value before displaying the data from the stack. If top is
equal to
-1 Print ”Stack is Empty”. If top is not equal to -1 then initialize I with
top values and print the value at the I position and then decrement
the I value.
The elements in STACK are as
follows Top-->2
If I is greater than or equal to 0 then print the value at the I position
and then decrement the I value.
The elements in STACK are as
follows Top-->2
1
If I is not greater than or equal to 0 then print
”End”
The elements in STACK are as follows
Top-->2
1
End
Checking whether the stack is empty:
Algorithm:
isempty() Step 1:
If(Top==-1)
Return
1 Else
Return 0
Checking whether the stack is full:
Algorithm: isfull()
Step 1: If(Top==Size-1)
Return
1 Else
Return 0

Representation of stacks through Arrays: When a stack is


implemented using an array, that stack size is fixed and can add
only limited number of elements.
DATA STRUCTURES UNIT: III
USING C

Representation of stacks through Linked Lists: When a stack is


implemented using a linked list, required number of elements can be
added without any restriction.

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
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.
DATA STRUCTURES UNIT: III
USING&
Stacks C Recursion: Stack ADT is used to implement recursive
functions. A
DATA STRUCTURES UNIT: III
USING C
recursive function is defined as a function that calls itself to solve a
smaller version of its task until a final call is made which does not
require a call to itself. Since a recursive function repeatedly calls itself, it
makes use of the system stack to temporarily store the return address
and local variables of the calling function.
To understand how recursive functions are implemented using stacks
consider the recursive function for calculating factorial of a number.
To calculate n!, we multiply the number n with factorial of the number
that is 1 less than that number.
That is n! = n X
(n–1)! Algorithm
FactRec(N) Step 1:
If N<=1
Return
1 Else
Return N *
FactRec(N-1) To find the
value of 5!
5! = 5 X 4!
Where as 4!= 4 X 3!
Where as 3!= 3 X 2!
Where as 2!= 2 X 1!
Where as 1!= 1

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.

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 as an Abstract data Type: 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 Rear position of the Queue.
delete() – Remove the element from the Front position in a non-
empty Queue. isempty() – Return true if the Queue is empty,
otherwise return false.
isfull() – Return true if the Queue is full, otherwise return false.
DATA STRUCTURES UNIT: III
USING C– Displays all the elements in the list.
display()

Insert an element at Rear position of the Queue:


Algorithm: insert(int
value) Step 1: If Rear
=Size-1
Print ”Queue is Full”
Else
{
Rear=Rear+
1;
Q[Rear]=val
ue;
If(Front=0)
Front=Rear
}
Procedure to insert data 1, 2,3 into the queue:
Initially the Queue is empty i.e Front=Rear=-1 and Size=2.

Read Data i.e 1 and insert the data into the queue. Check the Rear
value before inserting the data into the queue. If Rear is equal to Size-
1 Print ”Queue is Full”. If Rear is not equal to Size-1 then increment
the Rear value and insert data into the queue at rear. Check the Front
value after inserting the data into the queue. If Front is equal to zero
then assign the Rear value to the front.

Read Data i.e 1 and insert the data into the queue. Check the Rear
value before inserting the data into the queue. If Rear is equal to Size-
1 Print “Queue is Full”. If Rear is not equal to Size-1 then increment
the Rear value and insert data into the queue at rear. Check the Front
value after inserting the data into the queue. If Front is equal to zero
then assign the Rear value to the front.

Read Data i.e 3 and insert the data into the queue. Check the Rear
value before inserting the data into the queue. As Rear is equal to
DATA STRUCTURES UNIT: III
USING
Size-1 C ”Queue is Full” and data 3 is not pushed onto the queue.
Print
DATA STRUCTURES UNIT: III
USING C
Delete an element from Front position of the queue:
Algorithm:
delete() Step 1:
If(Front==-1)
Print ”Queue is
Empty” Else
{
Num=Q[Fro
nt] If
(Front=Rear
)
Rear=Front
=-1 Else
Front=Front
+1 Return
Num
}
Procedure to delete data 1,2 from the queue:
Initially the queue contains two elements i.e Front=0 and
Rear=1.

Remove Data i.e 1 from the queue. Check the Front value before
deleting the data from the Queue. If Front is equal to -1 Print ―Queue
is Empty‖. If Front is not equal to -1 then get the value at the Front into
num and then decrement the Front value. If Front value equal to -1
then assign the Front value to Rear.

Remove Data i.e 2 from the queue. Check the Front value before
deleting the data from the Queue. If Front is equal to -1 Print ―Queue
is Empty‖. If Front is not equal to -1 then get the value at the Front into
num and then decrement the Front value. If Front value equal to -1 then
DATA STRUCTURES UNIT: III
USING
assign theCFront value to Rear.
Remove Data i.e. 3 from the Queue. Check the Front value before
deleting the
DATA STRUCTURES UNIT: III
USING
data from Cthe Queue. As Front is equal to -1, Print ”Queue is Empty”.

Displaying the elements from the Queue:


Algorithm:
display() Step 1:
If(Front==-1)
Print ”Queue is
Empty” Else
{
Print "The elements in Queue are as follows\
nFront--> " I=Front
While (I<=Rear)
{
print
Q[I])
I=I+1
}
}
Procedure to display the elements from the stack:
Initially the queue contains two elements i.e Front=0 and
Rear=1.

Check the Front value before displaying the data from the stack.If Front
is equal to -1, Print ”Queue is Empty”. If Front is not equal to -1, then
initialize I with Front values and print the value at the I position and
then increment the I value.
The elements in Queue are as
follows Front-->1
If I is less than or equal to Rear then print the value at the I position and
then increment the I value.
The elements in Queue are as
follows Front-->1-->2
If I is less than or equal to Rear then print
”Rear”
The elements in Queue are as follows
Front-->1-->2-->Rear

Checking whether the Queue is empty:


Algorithm:
isempty() Step 1:
If(Front==-1)
DATA STRUCTURES UNIT: III
USING CReturn
1 Else
DATA STRUCTURES UNIT: III
USING C Return 0
Checking whether the Queue is full:
Algorithm: isfull()
Step 1: If(Rear==Size-1)
Return
1 Else
Return 0

Representation of Queues using Arrays: When a queue is


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

Representation of Queues using Linked Lists: When a queue is


implemented using linked lists, required number of elements can be
added into the queue without any restriction.

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)
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
DATA STRUCTURES UNIT: III
USING
first nodeCpoints 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: 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. Circular queues
overcome the problem of unutilized space in linear queue
implemented using array. In the array implementation there is a
possibility that the queue is reported full even though slots of queue
are empty.

Suppose an array x of n elements is used to implement a circular


queue. If we go on adding elements to the queue we may reach
x[n-1]. We cannot add any more elements to the queue since the
end of the array has been reached. Instead of reporting the queue
is full, if some elements in the queue have been deleted then there
might be empty slots at the beginning of the queue. In such case
these slots would be filled by new elements added to the queue. In
short, just because we have reached the end of the array, the
queuewould not be reported as full. The queue would be reported
full only when all the slots in the array are occupied.

Doubly Ended Queue (Deque): The doubly ended queue or deque


allows the insert and delete operations from both ends of the queue.

That is items can be added or deleted from the front or rear end, but
no changes can be made elsewhere in the list.
There are two types of deque. They are
1. Input Restricted Deque
2. Output Restricted Deque
DATA STRUCTURES UNIT: III
USINGRestricted
Input C Deque: Input Restricted Deque is a deque which
allows the insertions at one end of the queue but delete operations
can be done at both the ends of the queue.

Output Restricted Deque: Output Restricted Deque is a deque which


allows the deletions at one end of the queue but insertions operations
can be done at both the ends of the queue.

Priority Queue: A Priority queue is a collection of elements where


each element is assigned a priority and the order in which elements are
deleted and processed is determined form the following rules.
1. An element of higher priority is processed before any element
of lower priority.
2. Two elements with the same priority are processes according
to the order in which they are added to the queue.
An example of a priority queue can be q timesharing system i.e.
programs of high priority are processed first, and programs with the
same priority form a standard queue.

One way to represent priority queue in memory is by means of one-


way list.
1. Each node in the list contains three items of information- an
information field INFO, a priority number PRNO and the link
NEXT.
2. Node A will precede Node B in the list when A has higher priority
than B.
3. When Node A has same priority as B then the nodes are added
DATA STRUCTURES UNIT: III
USING C
in the
DATA STRUCTURES UNIT: III
USING C arrived.
order

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.
DATASTRUCTURES USING C
UNIT-I

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy