Data Structures Unit 3 Part 1
Data Structures Unit 3 Part 1
Data Structures Unit 3 Part 1
We can implement the stack ADT either with array or linked list.
F Stack overflow happens when we try to push one more item onto our stack than
it can actually hold.
F Stack underflow happens when we try to pop (remove) an item from the stack,
when nothing is actually there to remove.
F 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:
F 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
F Convert from infix to postfix: Stacks are used to convert an infix to
postfix expression.
Infix expression: (a+b)*c
Postfix expression : ab+c*
Prefix expression: *+abc
F 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”
F 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
F Comuter applications that use undo feature: undo is cancel the
previous action. To rememeber these in LIFO order computer
applications use stack.
F Compilers use stacks in syntax analysis phase to check whether a
particular statement in a program is syntactically correct or not.
F Computers use stack during interrupts and function calls. The
information regarding actual parameters return values, return addresses
and machine status is stored in stack.
F Stacks are used in tree traversal techniques
F Stacks are used in depth first search of a graph.
F 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()
{
………..
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.
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.
3. top = top + 1
4. stack[top] = data
5. end procedure
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.
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.
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.
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
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.
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;