DS Unit-2
DS Unit-2
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.
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
Main()
{
fun();
…………
…………
}
fun()
{
………..
Data Structures
3
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
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.
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.
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
Example: (A + B) * (C - D)
Example: * + A B – C D
Example: A B + C D - *
Data Structures
12
B.Sc. IV SEMESTER
Data Structures
13
B.Sc. IV SEMESTER
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