Chap-9 Data Structures-I: Linear Lists
Raw Data
Raw facts/values/set of values e.g. 101,”mohan”
Data Item
Single unit of values of certain type e.g. roll nos-101,102,103
Data Type
Named group of data with same properties and common behavior e.g. integer
Data Structure
• Named group of data which are stored in a specific way and can be processed as a single unit. e.g.
record, array, list
• It has well-defined operations, behavior and properties
• Makes managing of data simpler
• Example- Python has lists which can store heterogeneous types of elements but when we implement
as a data structure, its behavior is clearly pre decided e.g. it will store all elements of same type.
Data Structure
Simple Data Compound
Structure Data Structure
Array/Linear
List Linear Non-Linear
Stack Queue Linked list Tree
Types of Data Structure
Simple Data Structure Compound Data Structure
Built from primitive data types Built by combining simple data structures
Example-Arrays, Linear List Example-Stack, Queue ,Tree ,Graph
Types of Compound Data Structure
Linear/Single-Level Non Linear/Multi-level
Elements are stored in sequential order No sequential order is followed for elements
Example-Linked List, Stack, Queue Example-Tree, Graph
Note : In Python, built-in linear data
structures are: lists, tuples, dictionaries
Array/Linear Lists
• form of simple data structure
• named list of finite number of contiguous data elements of similar data types
• whose elements are referenced by 0,1,2……..(size-1) as:
A[0],A[1],A[2]……….
where A is the name of the array
• whose size/length/range → no. of elements
• can be one-dimensional, two-dimensional or multi-dimensional
• If the array has lower bound as L and upper bound as U then
Array size = U – L + 1
Example- if an array has elements numbered as -7,-6,-5,…..0,1,2,3…….15
Then array length/size = 15-(-7)+1 = 15+7+1 = 23
• Arrays are implemented thru List data type in Python (as Linear list) or thru NumPy arrays.
Stack - LIFO List
• Is a data structure (List) where insertions and deletions of the elements take place from the top
of the stack
• Addition of a new element in the Stack --- PUSH operation
• Removal of an element from the Stack -- POP operation
• Inspecting/view the value at the top without removing it – PEEK /INSPECTION operation
• Example : stack of plates/books
Underflow Situation – when deleting an element from empty stack/queue
Overflow Situation - when inserting an element to a full stack/queue (in case of fixed size lists)
Note: In Python, there is no overflow situation as we have variable size lists
Queue - FIFO List
• Is a data structure where insertions take place at the ‘rear’ end of the queue and deletions take
place at the ‘front’ end of the queue.
• Addition of an element from the rear end of the queue -- INSERT operation --- Enqueu-ing
• Removal of an element from the front end of the queue – DELETE operation --- Dequeue-ing
• Inspecting the value at the Queue’s front without removing it - Peek/Inspection
• Two ends of the queue -- Two Access Points -- FRONT and REAR
• Examples:
At the bank
At Fee counter
At Shopping Centre
Linked list
• List of data elements linked to one another
• Element → called as Node
• Each node has two parts: Data /Info Link
Singly Linked list Doubly Linked list
Each node has two parts: Data and Link Each node has three parts: Data and two Links
Data part stores data and Link part stores Data part stores data, first Link part stores the
the address of next node address of previous node and other stores the
address of the next node
Data Link Data
Trees:
• Multilevel data structure
• Whose elements/nodes have hierarchical relationship
• Topmost node→ root of the tree
• Bottommost nodes→ leaves of the tree
• Node points to the node below it
Operations on Data Structures
Insertion Adding a new data element in a data structure
Deletion Deleting a data element from a data structure(searching is done first to delete)
Searching Searching a specified data element in a data structure
Traversal Processing /accessing elements one by one.
Sorting Arranging data elements of a data structure in a specified order(asc or desc)
Merging Forming a new data structure by combining two similar data structures.
'''
WRITE A MENU-DRIVEN PROGRAM ON STACK OPERATIONS
(push,pop,peek,display)
'''
stack=[]
top=None
def Push():
global top
num=int(input("enter the no."))
stack.append(num)
top=len(stack)-1
def Pop():
global top
if len(stack)==0: # if stack==[]
print("underflow! cannot pop from an empty stack")
else:
delval=stack.pop()
print("the del num=",delval)
top=len(stack)-1
def Display():
global top
if len(stack)==0:
print("no numbers in stack")
else:
for i in range(len(stack)-1,-1,-1):
print(stack[i],end="-->")
while True:
print("*****************************")
print("1.Push")
print("2.Pop")
print("3.Display")
print("4.Exit")
print("*************************")
opt=int(input("choose 1/2/3/4="))
if opt==1:
Push()
elif opt==2:
Pop()
elif opt==3:
Display()
else:
break