Chapter-6 STACKSds
Chapter-6 STACKSds
STACKS
The linear lists and linear arrays allow inserting and deleting elements at any place in the list i.e.
at the beginning, at the end, or in the middle. There are certain frequent situations when one
wants to restrict insertions and deletions so that they can take place only at the beginning or at
the end of the list, not in the middle. One of the data structures that are useful in such situations
is a stack.
STACK
A stack is a linear list of elements in which an element may be inserted or deleted only at one
end, called the top of the stack. This means, in particular, that elements are removed from a stack
in the reverse order of that in which they were inserted into the stack.
Stacks are also called last in – first out (LIFO) lists. Other names used for stacks are “piles” and
“push-down lists”.
Examples
Three every day examples of stack: a stack of coins, stack of books and computer stack are
shown in the figure below.
1) Arrays
2) Single-linked lists
A Stack contains an ordered list of elements and an array is also used to store ordered list of
elements. Hence it would be very easy to manage a stack using an array. However, the problem
with an array is that we are required to declare the size of the array before using it in a program.
Therefore the size of the stack would be fixed.
When a stack is implemented using arrays, it suffers from the basic limitation of an array that is,
its size cannot be increased or decreased once it is declared. As a result, one ends up reserving
either too much space or too less space for an array and in turn for a stack. This problem can be
overcome if we implement a stack using linked list.
In the case of a linked stack, we shall Push and Pop nodes from one end of a linked list. Each
node in the linked list contains the data and a pointer that gives location of the next node in the
Operations on stacks
Push
Pop
Peep
Change
1) Push - Adds an item at the top of the stack.
3) Peek - returns the value of the Ith element from the top of the stack.
4) Change - changes the value of the Ith element from the top of the stack to the value contained
in ITEM.
Pop
Peek
Peek operation returns the value of the I th element from the top of the stack.
// This function returns the value of the I th element from the top of the stack.
If (TOP < 0)
return 0;
else
I = STACK[TOP];
return I;
Change
// This function changes the value of the Ith element from the top of the stack to the value
// contained in ITEM.
1) If TOP <= 0
return 0;
else
return(STACK[TOP]) ß ITEM;
Complexity:
Time Complexity: Worst case: O(n)
Space Complexity: Worst case: O(n)
int data;
};
int count;
NODE *top;
};
Create stack
Push
Pop
Stack top
Empty stack
Full stack
Stack count
Destroy
1) Create stack
CreateStack allocates memory for the stack structure and allocates a stack head node, initializes
the top pointer to null, and zeros the count field.
NODE *stack;
2) If (stack)
head àcount = 0;
stackàtop = NULL;
return stack;
2) Push
Pop stack sends the data in the node at the top of the stack back to the calling algorithm. It then
deletes and recycles the node. After the count is adjusted by subtracting one, the function returns
to the caller.
The stack top function returns the data at the top of the stack without deleting the top node. It
allows the user to see what will be deleted when the stack is popped.
// This algorithm returns the data from the top of the stack without changing the stack.
else
return 0;
} // End of stacktop
5) Empty stack
} // end of emptystack
6) Full stack
Full stack operation determines if a stack is full. Returns the value true if the stack is full.
NODE *temp;
free ( temp);
return 0;
else
return 1;
7) Stack count
return(stack àcount);
8) Destroy
Destroy stack operation deletes the nodes in a stack including the stack structure and returns a
NULL pointer.
// This function deletes all the nodes from the stack including its structure.
NODE *temp;
1) if (stack)
free (stackàtopàdata);
free (temp);
free (stack);
} // End of if
return 0;
} // End of destroyStack
1) Towers of Hanoi
The Towers of Hanoi is a mathematical game or puzzle. It consists of three pegs, and a number
of disks of different sizes which can slide onto any peg. The puzzle starts with the disks neatly
stacked in order of size on one peg, smallest at the top, thus making a conical shape.
The objective of the game is to move the entire stack to another peg, obeying the following rules:
The solution to the Towers of Hanoi problem for n>1 disks may be reduced to the following
sub problems:
// This algorithm gives a recursive solution to the Towers of Hanoi problem for N disks.
1) If N=1, then
3) Write: BEGEND
5) Return
Complexity:
Time Complexity: Worst case: O(2n)
Space Complexity: Worst case: O(2n)
Convert the given infix expression A into postfix expression P using stacks.
Suppose P is an arithmetic expression in postfix notation. The algorithm uses a STACK to hold
operands, evaluates P.
using stacks.
Example: