0% found this document useful (0 votes)
19 views13 pages

DS Unit-2

Uploaded by

grojamani
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)
19 views13 pages

DS Unit-2

Uploaded by

grojamani
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/ 13

1

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.

What is stack? Explain its Applications


A Stack is a linear list in which insertions and deletions take place at the
same end. The end through which items are added and deleted is called the
Top End. The other end of the list is called the Bottom End.

 A stack is a last-in-first-out ( LIFO ) structure.


 Insertion operation is referred as ―PUSH‖ and deletion operation is referred as
―POP‖.
 The most accessible element in the stack is the element at the position
―TOP‖.

We can implement the stack ADT either with array or linked list.

 Stack overflow happens when we try to push one more item onto our stack than
it can actually hold.
 Stack underflow happens when we try to pop (remove) an item from the
stack, when nothing is actually there to remove.
 To use a stack efficiently, we need to check the status of stack as well. For
the same purpose, the following functionality is added to stacks –
o peek() − get the top data element of the stack, without
removing it. o isFull() − check if stack is full.
o isEmpty() − check if stack is empty.
Applications of stack:
 Paranthesis matching: Stacks can be used to find out whether the
given equation is balanced or not. It mainly checks for the matching of left
and right parenthesis in an equation.
Example:
(A+B(x+y)+C is not a valid expression because it miss right
paranthesis (A+B(x+y)+C) is a valid expression
 Convert from infix to postfix: Stacks are used to convert an infix to
postfix expression.

Data Structures
2

B.Sc. IV SEMESTER

Infix expression: (a+b)*c


Postfix expression : ab+c*
Prefix expression: *+abc
 To evaluate postfix forms: To evaluate postfix expression, stacks
can be used. While evaluating, operands are stored onto a stack and popped
when an operator occurs.
The postfix expression ―2 3 *6 +‖ get executed as ―12‖
 Recusrsion: Recursive functions are implemented using stacks. The
copies of variables at each level of recursion are stored in stack.
Example
Fact(3)=3*fact(
2)
= 3*2*Fact(1)
= 3*2*1
= 3*2
= 6
 Comuter applications that use undo feature: undo is cancel the
previous action. To rememeber these in LIFO order computer applications
use stack.
 Compilers use stacks in syntax analysis phase to check whether a
particular statement in a program is syntactically correct or not.

 Computers use stack during interrupts and function calls. The


information regarding actual parameters return values, return addresses and
machine status is stored in stack.
 Stacks are used in tree traversal techniques
 Stacks are used in depth first search of a graph.

 Keeping track of function calls: To keep track of function calls,


stack can be used to return to the point where it is called after executing the
function. When a function is called the control transfers to the function and
executes all the statements there. But, to return to the pointer where it is
called a stack is used to remember the address of the calling statement.

Main()
{
fun();
…………
…………
}
fun()
{
………..

Data Structures
3

What is stack ? Explain basic operations of stack data structures.


A Stack is a linear list in which insertions and deletions take place at the
same end. The end through which items are added and deleted is called the
Top End. The other end of the list is called the Bottom End.
Basic Operations
Stack operations may involve initializing the stack, using it and then de-
initializing it. Apart from these, a stack is used for the following two primary
operations.
push( ) − Pushing (storing) an element on the stack.

pop( ) − Removing (accessing) an element from the stack.

When data is PUSHed onto stack, To use a stack efficiently, we need to check
the status of stack as well. For the same purpose, the following functionality
is added to stacks.

peek( ) − get the top data element of the stack, without removing it.

isFull( ) − check if stack is full.

isEmpty( ) − check if stack is empty.

Stack is said to be in Overflow state when it is completely full and is said to


be in Underflow state if it is completely empty.
Push Operation
The process of putting a new data element onto stack is known as a
Push Operation. Push operation involves a series of steps.
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Adds data element to the stack location, where top is pointing.
Step 5 − Returns success.
Algorithm for Push operation
1. Begin procedure push: stack, data

2. if stack is full return null end if

3. top = top + 1

4. stack[top] = data

5. end procedure

Data Structures
4

B.Sc. IV SEMESTER

Pop Operation
Accessing the content while removing it from the stack, is known as a
POP Operation. A POP operation may involve the following steps.
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit.
Step 3 − If the stack is not empty, accesses the data element at which
top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.
Algorithm for Pop operation
1. begin procedure pop: stack
2. if stack is empty return null
3. end if 4. data = stack[top]
5. top = top - 1
6. return data
7. end procedure

Peek( ):
This operation returns the most accessible element from stack.

begin procedure peek

Data Structures
5

B.Sc. IV SEMESTER

return stack[top]

end procedure

isFull( ):
This operation is performed to know whether the stack is full or not. If the
TOP value reaches to MAXSIZE then the stack is full.

begin procedure isfull


if top equals to MAXSIZE
return true
else
return false
endif
end procedure
isEmpty( )
This operation is used to check whether the stack is empty or not
begin procedure isempty.
if top less than 1
return true
else
return false
endif
end procedure

What is stack? Discuss about aray and linked representation of a


stack.
A Stack is a linear list in which insertions and deletions take place at the
same end. The end through which items are added and deleted is called the
Top End. The other end of the list is called the Bottom End.The two basic
operations associated with stacks are:
Push: is the term used to insert an element into a stack.
Pop: is the term used to delete an element from a stack.
A stack may be represented in the memory in various ways. Mainly there
are two ways. They are:
1. Using one dimensional arrays(Static Implementation)
2. Using linked lists(Dynamic Implementation)
Stack Representation using Arrays :
A Stack data structure can be represented using arrays. We perform stack
operations Push and POP by maintaining the top value to point to the
topmost element position of the stack.

Data Structures
6

B.Sc. IV SEMESTER

Abstract Class :

AbstractDataType Stack
{
Instances
Linear list of elements: one end is called the bottom the other is the
top.
Operations
isEmpty() : Return True if stack is empty, return False otherwise.
isFull() : Return True if stack is full, return False otherwise
push(x) : Add element x to the stack
pop(x) : Delete top element from stack and put it in x.
}

Operations:
push(x) : Adding items to the stack is called as Push operation. Here x is
the data item that is being added onto the stack.

Data Structures
7

B.Sc. IV SEMESTER

pop(x) : Deleting items from the stack is called as Pop operation. Here x is
the data item that is being deleted from the stack.
isFull( ) :This function verifies whether the stack is full or not. If stack is full,
it returns true, otherwise return false.
isEmpty(): This function verifies whether the stack is empty or not. If stack
is empty, it returns true, otherwise it returns false.
Algorithm
Stack Operations: Basically, there are two operations performed on
the stack.
Push: Push is the term used to insert the element on the top of stack.
Pop: Pop is the term used to delete the elements from top of the stack.
Algorithm: push(s, top, x):[This algorithm inserts the elements.]
Step: 1 [check for the stack over flow]
If (top > = max-1) then write (―stack over flow‖)
Return;
Step: 2 [increment top by 1]
top top+1;
Step: 3 [insert element]
s[top] x;
Step: 4
[finished]
Return;
Algorithm pop(s, top): [this algorithm deletes the top element from the
stack]
Step: 1 [check for the stack under flow]
if (top= = -1) then write (―stack underflow‖)
Return;
Step: 2 [assign top element to x]
X s[top];
Step: 3 [decrement top by 1]
top top-1;
Step: 4
[finished]
Return;
Algorithm isEmpty(s, top): [this algorithm checks the stack is empty ]
Step: 1 [check for the stack is empty()]
if (top= = -1) then write (―stack Empty‖)
Step: 2.
[finished]
Return;
Algorithm: isFull(s, top):[This algorithm checks the stack is full]
Step: 1 [check for the stack is full]
If (top > = max-1) then write (―stack is Full‖)
Step: 2 [finished]
Return;
Stack Representation using Linked List:
Array representation of stacks is very easy, but it allows only fixed sized
stacks. In several applications, the size of the stack may vary during program
execution. In such cases we may use linked list. A single linked list structure
is
8

Data Structures
9

B.Sc. IV SEMESTER

sufficient to represent to any stack. Here data field is users data and link
field is as usual point to next item.
We can use either single linked or double linked list to represent a stack data
structure. But it is easy to use single linked list to represent a stack data
structure.

Example :

In the linked list representation, first node on the list is the current item that
is the item at the top of the stack and the last node is the node containing
bottom most item. So PUSH operation will perform at the front and POP
operation will perform at front. SIZE of the stack is not important here
because dynamic representation.

The implementation of isFull() method is not necessary because the only way
to know whether we can add an element on to the stack or not is to see
whether enough space exists to create a node of type Node. This check can
be done by invoking new.
Abstraction of Stack:
Data Objects:
Node :-consisting of two parts data and address.
headptr :-always points to the address of first node in the list
next :- used to store next node address
Methods :
isEmpty(): This method verifies that the stack is empty or not. If the
stack is empty it returns true, otherwise it returns false.
push(x): This method adds a data item x onto the stack. If the
operation executed successfully it returns true, otherwise it returns
false.

Data Structures
10

B.Sc. IV SEMESTER

pop(x): This method pops a data item from the stack and places it in x. If
the operation executed successfully it returns true, otherwise it
returns false.
peek(): To access the top element of the stack without removing it.

Algorithms
Algorithm: push_LL (s, top, x):[This algorithm inserts the elements.]
Step: 1[Create new node ]
New=Getnew(Node);
Step: 2 [inserting Values]

New Data=Item
Step3: [palcing reference]

New link=top
Top=new
Step: 4 [top is assigns to stack head]

Stack_Head link=top
Step:5 [Finish]
Return;
Algorithm pop_LL(s, top): [this algorithm deletes the top element from
the
stack]
Step: 1 [check for the stack under flow]

PTR= Stack_Head link;
if (PTR=NULL) then write (―stack underflow‖)
Return;
Step: 2 [assign top element to x]
X PTR[top]; [Display x]
Step: 3 [Assign top to next node]

top = ptr link;
Step: 4[Assign head_ptr to top]
Stack_Head=top;
Step: 5 [finished]
Return;
Algorithm isEmpty(s, top): [this algorithm checks the stack is empty ]
Step: 1 [check for the stack is empty()]
if (top= = null) then write (―stack Empty‖)
Step: 2.[finished]
Return;

Data Structures
11

B.Sc. IV SEMESTER

What is an arithmetic expression? Explain their notations?


An algebraic expression is a legal combination of operators and operands.
Operand may be a variable like x, y, z or a constant like 5, 4, 6 etc. Operator
is a symbol which signifies a mathematical or logical operation between the
operands. Examples of familiar operators include +, -, *, /, ^ etc. An
algebraic expression can be represented using three different notations.
They are infix, postfix and prefix notations:

Infix: It is the form of an arithmetic expression in which we fix (place) the


arithmetic operator in between the two operands.

Example: (A + B) * (C - D)

Prefix: It is the form of an arithmetic notation in which we fix (place) the


arithmetic operator before (pre) its two operands. The prefix notation is called
as polish notation

Example: * + A B – C D

Postfix: It is the form of an arithmetic expression in which we fix (place) the


arithmetic operator after (post) its two operands. The postfix notation is
called as suffix notation and is also referred to reverse polish notation.

Example: A B + C D - *

The three important features of postfix expression are:

 The operands maintain the same order as in the equivalent infix


expression.

 The parentheses are not needed to designate the expression


unambiguously.

 While evaluating the postfix expression the priority of the operators is


no longer relevant.

Data Structures
12

B.Sc. IV SEMESTER

Convert the following infix expression A + B * C – D / E * H into its


equivalent postfix expression.

Convert ((A – (B + C)) * D) ↑ (E + F) infix expression to postfix form:

Data Structures
13

B.Sc. IV SEMESTER

What is a Queue ? Explain its operations?

A queue is a leaner list, which has two ends, where items are inserted at one end
called the rear and deleted at the other end called the front. Another name for a
queue is a ―FIFO‖ or ―First-in-first-out‖ list. Unlike stacks, a queue is open at both its
ends. One end is always used to insert data (enqueue) and the other is used to
remove data (dequeue). We can implement the queue by using Arrays and linked
list.

Arrays

Linked list representation

You might also like