You are on page 1of 7

UNIVERSITY OF ZIMBABWE

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:

PRINCE MADZAMBA R216950C HCS

PASSION ZIMANI R216964J HCS

THEMBALETHU NDLOVU R216949D HCS

STEVE MURENJE R217034E HCS

SASHA NDAMBA R217026G HCS

CRAIG MAZORODZE R217022N HCS

PRAISE JAVANGWE R216976K HCS

DELIGHT HUNGWE R216977G HCS

TATENDA SHAVA R216977G HCS

TAKUDZWA CHITONGO R216954T HCS


DEFINITION:

A stack is a linear data structure in which operations are occur in Last-in-First-out(LIFO)


order and operations occur at one end the top of the stack.

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

can be altered is the element that is at the top of the stack.


The state of a stack when it is empty is called the underflow state and the state when its

full, in terms of a bounded stack is called an overflow state.

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

Check if the stack is full or not.

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

from the stack and the pointer is decremented by one.


Algorithm for POP operation

Check if the stack is empty or not.

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.

OTHER COMMON STACK OPERATIONS:

PEEK Operation

Peek operation renders the reading of the element at the top of the stack without

deleting the element.

Is Empty Operation

Check if stack is empty or not and returns True or False

is Empty raises an underflow exception when a program tries to pop an element from an empty

stack. To prevent performing operations on an empty stack, a programmer is supposed to

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

already full stack.

Duplicate Operation — Copy the top item’s value into a variable and push it back into the

stack.

Swap Operation — Swap the two topmost items in the stack.

Rotate Operation— Move the topmost elements in the stack as specified by a number or move

in a rotating fashion.

IMPLEMENTATION OF STACK DATA STRUCTURE:

Stack can be easily implemented using an Array or a Linked List

Stack Implementation using Arrays

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.

You might also like