DATA-STRUCTURES Note
DATA-STRUCTURES Note
• The logical or mathematical model of a particular organization of data is called data structure. It is a
way of storing, accessing, manipulating data.
Data Structures
|
|-Linear
| |-Arrays
| |-Linked List
| |-Stacks
| |-Queues
|
|-Non-Linear
|- Trees
|- Graphs
|- Tables
|- Sets
10
20
30
40
50
>>>
• Inserting Element in a list: There are two ways to insert an element in a list.
- If the array is not sorted
- If the array is sorted
• If the array is not sorted: In this case, enter the element at any position using insert() function or add
the element in the last of the array using append() function.
Example:
Output:
[15, 8, 25, 88, 45, 13, 19]
• If the array is sorted: In this case, import bisect module and use the functions bisect() and insort().
bisect(): identifies the correct index for the element and returns the index. insort(): Inserts the element
in the list in its correct order.
• Deletion of an element from a List: To delete an element from a list we can use remove() or pop()
method.
Example:
Output:
• Searching in a List: There are two types of searching techniques we can use to search an element in a
list. These are:
- Linear Search
-Binary Search
Program:
Output:
Binary search can work for only sorted arrays whereas linear search can work for both sorted as well as
unsorted arrays.
Output:
Linear Search
- Access of elements sequentially.
- Elements may or may not be in sorted order.
- Takes more time to search an element.
- Easy
- Efficient for small array.
Binary Search
- Access of elements randomly.
- Elements must be in sorted order i.e. ascending or descending order
- Takes less time to search an element.
- Tricky
- Efficient for larger array
• Sorting: To arrange the elements in ascending or descending order. There are many sorting
techniques.
Program:
Output:
• Stack: It is a linear data structure. Stack is a list of elements in which an element may be inserted or
deleted only at one end, called the TOP of the stack. It follows the principle Last in First out (LIFO).
S= []
from os import system
#menu display
def menu():
ch = 0
while(ch<1 or ch>4):
#print("\n" 100)
anyvar = system("cls")
print("\n\n\n\n\n")
print ("\t\t\t1: PUSH")
print ("\t\t\t2: POP")
print ("\t\t\t3: DISPLAY")
print("\t\t\t4: EXIT")
ch = int(input("\n\t\tEnter a choise (1- 4):"))
return ch
def push():
#code to push an item
item = int(input("\t\t\tEnter an item to push: "))
S.append(item)
print("\t\t\t ITEM", item," PUSHESD IN THE STACK")
def display():
#code to display stack
if (S== []):
print ("\t\t\tEMPTY STACK")
else:
print ("\t\t\t",)
for i in S:
print( i, '', end="")
import sys
ch = 0
while(ch != 4):
ch=menu()
if(ch == 1):
push()
elif(ch == 2):
pop()
elif(ch == 3):
display()
elif(ch == 4):
print ("\t\t\t ABORTING PROGRAM.")
sys.exit()
anyvar = input("\n\t\t\tPress any key to continue... \n")
• QUEUE: Queue is a linear data structure. Queue is a list of elements in which insertion takes place at
one end called REAR and deletion takes place only at the other end, called the FRONT. It follows the
principle First In First Out (FIFO).
Q = []
from os import system
#menu display
def menu():
ch = 0
while(ch<1 or ch>4):
#print ("\n"*100)
anyvar = system("cls")
print ("\n\n\n\n\n")
print ("\t\t\t1: INSERT")
print ("\t\t\t2: DELETE")
print ("\t\t\t3: DISPLAY")
print ("\t\t\t4: EXIT")
ch = int(input("\n\t\t\tEnter a choise (1-4): "))
return ch
def insert():
#code to insert an item
item = int(input("\t\t\tEnter an item to insert: "))
Q.append(item)
print("\t\t\tITEM", item," INSERTED IN THE QUEUE")
def delete():
#code to delete from queue
if (Q == []):
print ("\t\t\tNO ITEM TO DELETE")
else:
item = Q.pop(0)
print ("\t\t\t ITEM", item," DELETED FROM THE QUEUE")
def display():
#code to display stack
if (Q == [] ):
print ("\t\t EMPTY QUEUE")
else:
print ("\t\t\t",)
for i in Q:
print (i, '', end = " ")
Answer = Data Structure means organization of data. A data structure has well defined operations or
behavior.
Answer = QUEUE
Answer = STACK
Answer = Yes
Answer = Lists
Answer = Graphs
Answer = PUSH
Answer = POP
Answer = len( )
Answer = 0
Answer = Data Structure provides information regarding organization of data whereas Data Type
provides information regarding the domain of values and operations that can be perform on data.
Answer =
Stack: - A stack is a linear list also known as LIFO list with the special property that items can be added or
removed from only one end called the top.
Queue: - A queue is a linear list also known as FIFO list with the special property that items can be added
at one end and removed from the other.
Answer = A list is a mutable sequence of data elements indexed by their position. A list is represented
using [ ]. E.g. L= [10, 20, 30]
Answer = Traversing means accessing or visiting or processing each element of any data structure.
Q16. Name the methods used for inserting and deleting elements from a list.
Answer = Various methods for inserting elements in a list are - insert(), append(), extend() and methods
used for deleting items from a list are – pop() , remove(), clear()
Answer = Reversing a string, compilers uses stack to store previous state of program, undo mechanism
in text editors and backtracking.
Answer = Sharing of resources, CPU uses queue, Airport authorities uses queue for runways and many
computer algorithms uses queue.
Answer =
Answer =
Q21. Write a program to implement a stack for the students(studentno, name). Just implement Push.
stk = []
top = -1
def PUSH(stk, student):
stk.append(student)
top = len(stk) - 1
PUSH(stk,data)
Answer = 55
Answer =
pop() will delete the last element of a list whereas pop(0) will delete element at index zero of a list.
Thankyou!!!!!
For Solution of ‘Sumita Arora’ visit on Path Walla
For ‘Sumita Arora Type C’ solution (Programing Question) Visit our
YouTube Channel Portal Express