Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

E-school Education Hub

Data Structures: Stacks and Queues using Lists

Raw Data

Simple values or set of values.

Data Item

Single unit of values of certain type.

Data Type

It defines a set of values along with well-defined operations stating its input-output
behaviour.

Data Structure

It is the physical implementation that defines a way to store, access, and manipulate the
Data. We can say that it is a named group of data of different types stored in a specific way
and can be processed as Single Unit. A Data Structure has well defined operations,
properties and behaviour.

a. Linear Data Structures:

Data structure where data elements are arranged sequentially or linearly where the
elements are attached to its previous and next adjacent element. In linear data
structure, single level is involved. Therefore, we can traverse all the elements in
single run only. Linear data structures are easy to implement because computer
memory is arranged in a linear way. Its examples are array, stack, queue, linked list,
etc.

b. Non-Linear Data Structures:


E-school Education Hub

Data structures where data elements are not arranged sequentially or linearly are
called non-linear data structures. In a non-linear data structure, single level is not
involved. Therefore, we can’t traverse all the elements in single run only. Non-linear
data structures are not easy to implement in comparison to linear data structure. It
utilizes computer memory efficiently in comparison to a linear data structure. Its
examples are trees and graphs.

Linear Lists or Arrays

Linear List or Arrays refers to a named list of similar data elements. All its elements can be
referenced by a position represented by consecutive numbers from 0 to n-1, where n is the
length of the list.

Stacks

Lists stored in a special way following concept of LIFO (Last In First Out). Insertion of an
element is performed at the end called TOP and deletion of an element also occurs from
same end (TOP).

Queues
E-school Education Hub

Lists stored in a special way following concept of FIFO (First In First Out). Insertion of an
element is performed at the end called REAR and deletion of an element occurs from the
beginning called FRONT.

Linked List

List of elements that are linked to one another. Each element is called a NODE and contains
two parts. The INFO part stores information of the Node and REFERENCE POINTER stores
the reference of next element. (We need not to implement it in python)

Trees

Multilevel Data Structures having a Hierarchal relationship among its Nodes. Topmost node
is Root and bottom nodes are called leaves. It is in shape of an Inverted Tree.
E-school Education Hub

Operations on Data Structures


1. Insertion Adding a new element
2. Deletion Removing an existing element
3. Searching Finding existence of an element
4. Traversal Accessing each and every element
5. Sorting Arrange data in an order (Ascending/Descending)
6. Merging Combining element of two similar Data Structures.
Note: - Programs on the above topics will be discussed in class room

STACKS

A Linear Data Structure following concept of LIFO (Last In First Out). TOP is an important
part of a Stack because Insertion and Deletion, both operations in a Stack, are performed at
this end.
PUSH Operation When a new element is added/ inserted at TOP end of a Stack.
POP Operation When an existing element is deleted from TOP end of a Stack.
PEEK Operation When Stack’s TOP element is referred.
OVERFLOW When a new element is tried to be added to a full Stack. (Normally in
python it happens only when the RAM is full)
UNDERFLOW When an element is tried to be deleted from an empty Stack.

#Program to perform operations on a Stack (On simple list)

def Push(Arr,data):
Arr.append(data)

def Pop(Arr):
if (Arr==[]):
print( "Underflow or Stack is empty")
E-school Education Hub

else:
print ("Deleted element is: ",Arr.pop())

def Disp(Arr):
if (Arr==[]):
print( " Stack is empty")
else:
for i in range(len(Arr)-1,-1,-1):
print(Arr[i])

#main script begins here


Stack=[]
ch=1
while ch!=4:
print("Press 1 to perform Push Operation")
print("Press 2 to perform Pop Operation")
print("Press 3 to display all elements of a Stack")
print("Press 4 to exit")
ch=int(input("Enter your choice"))
if ch==1:
name=input("Enter name to be inserted in Stack")
Push(Stack,name)
elif ch==2:
Pop(Stack)
elif ch==3:
Disp(Stack)

#Program to perform operations on a Stack (On nested list)

def Push(Arr,data):
Arr.append(data)

def Pop(Arr):
if (Arr==[]):
print( "Underflow or Stack is empty")
else:
print ("Deleted element is: ",Arr.pop())

def Disp(Arr):
if (Arr==[]):
print( "Stack is empty")
else:
for i in range(len(Arr)-1,-1,-1):
print(Arr[i])

#main script begins here


Stack=[]
ch=1
E-school Education Hub

while ch!=4:
print("Press 1 to perform Push Operation")
print("Press 2 to perform Pop Operation")
print("Press 3 to display all elements of a Stack")
print("Press 4 to exit")
ch=int(input("Enter your choice"))
if ch==1:
bno=int(input("Enter Book no"))
bname=input("Enter book name")
price=float(input("Enter price"))
record=[bno,bname,price]
Push(Stack,record)
elif ch==2:
Pop(Stack)
elif ch==3:
Disp(Stack)

Implementation of a Stack
1. Reversing a Line
2. Polish String (Helps in Arithmetic Expression Evaluation)

Arithmetic Expression Evaluation

The stack organization is very effective in evaluating arithmetic expressions. Expressions are
usually represented in what is known as Infix notation, in which each operator is written
between two operands (i.e., A+B). With this notation, we must distinguish between (A+B)*C
and A + (B*C) by using either parentheses or some operator-precedence convention. Thus,
the order of operators and operands in an arithmetic expression does not uniquely
determine the order in which the operations are to be performed.

Polish notation (prefix notation)

It refers to the notation in which the operator is placed before its two operands. Here no
parentheses are required, i.e. +AB

Reverse Polish notation (postfix notation)

It refers to the analogous notation in which the operator is placed after its two operands.
Again, no parentheses is required in Reverse Polish notation, i.e. AB+

Stack organized computers are better suited for post-fix notation then the traditional infix
notation. Thus the infix notation must be converted to the post-fix notation.

Note: Examples of Converting Infix to Postfix and Evaluating Postfix Expression will be
discussed in class.

You might also like