Professional Documents
Culture Documents
GROUP 4
DSA Assignment: Describe how stacks work. Use diagrams. Include the different operations.
No need to use program code and no need to include real life applications.
Group Members:
The two primary operations that are done on stacks are the push and the pop operations.
Push is adding an element on the stack and pop is removing an element from the stack. There is
also a peek operation which makes it possible to see the element on the top of the stack. Stacks
are mainly used to keep track of the return address during function calls. They are also used to
pass data between functions.
Every time an element is added, it goes on the top of the stack and the only data item that
STACK OPERATIONS:
A stack has two major operations, namely push and pop. Push and pop are carried out on
the topmost element, which is the item most recently added to the stack.
PUSH Operation
Push operation refers to inserting an element in the stack. A Stack keeps track of the
location of the data item on the top of it using a pointer variable or index. Whenever an element
needs to be added to the stack, the pointer is first incremented by one to point to the new location
of the element that is now to be added and the element is added to the stack.
Algorithm for PUSH operation
If the stack is full, then print error of overflow and exit the program
If the stack is not full, then increment the pointer and add the element.
POP Operation
Pop operation refers to the deletion of an element from the stack. Since the program
only have access to the element at the top of the stack, there’s only one element that can be
removed. The POP operation can only delete the item that is on the top of the stack.
When an element is popped out from the stack, the data element at the top is removed
If the stack is empty, then print error of underflow and exit the program.
If the stack is not empty, then discard the element at the top and decrement the pointer by
one.
PEEK Operation
Peek operation renders the reading of the element at the top of the stack without
Is Empty Operation
is Empty raises an underflow exception when a program tries to pop an element from an empty
internally maintain the size of the stack which will be updated during pop operations.
Is Full Operation
In the case of bounded capacity stacks, Is Full Operation checks if the stack if filled to
its capacity or not. It raises an overflow exception when trying to PUSH an element to an
Duplicate Operation — Copy the top item’s value into a variable and push it back into the
stack.
Rotate Operation— Move the topmost elements in the stack as specified by a number or move
in a rotating fashion.
The index of the stack is defined as top and the program is allowed to access only the
element at the top index. The stack capacity may be declared during the definition of the
program. The program is set to throw a stack overflow error when trying to exceed this
capacity.
Stack Implementation using Linked Lists
There are two ends of a linked list: head and tail. The top of the stack should be
represented by head. A stack implementation using a linked list is declared without specifying
the length of the stack. An empty stack using this implementation is achieved by pointing the
index to null.