Xii Cs Unit I Part7 Data Structure 2020 21
Xii Cs Unit I Part7 Data Structure 2020 21
CLASS-XII 2020-21
DISTRIBUTION OF MARKS:
UNIT UNIT NAME MARKS
II Computer Networks 10
TOTAL 70
It is a collection of items and each item has its own index value. Index of
first item is 0 and the last item is n-1. Here n is number of items in a list.
Indexing of list
Creating a list:
Example:
e.g.
list =[3,5,9]
print(list[0]) 3
print(list[1]) 5
print(list[2]) 9
print('a pause') a pause
print(list[-1]) 9
print(list[-2]) 5
print(list[-3]) 3
list =[3,5,9]
for i in range(0, len(list)):
print(list[i])
Output
3
5
9
Slicing of a List:
list =['I','N','D','I','A']
print(list[0:3])
print(list[3:])
print(list[:])
Output
['I', 'N', 'D']
['I', 'A']
['I', 'N', 'D', 'I', 'A']
Updating Lists:
Output
('Value available at index 2 : ', 1997)
('New value available at index 2 : ', 2001)
('New value available at index 3 : ', 2002)
Add Item to a List
list=[1,2]
print('list before append', list)
list.append(3)
print('list after append', list)
Output
('list before append', [1, 2])
('list after append', [1, 2, 3])
list = [1,2]
list2 = [3,4]
list3 = list + list2
print(list3)
OUTPUT
[1,2,3,4]
list=[1,2,3]
print('list before delete', list)
del list [1]
print('list after delete', list)
Output
('list before delete', [1, 2, 3])
('list after delete', [1, 3])
Function Description
list.append() Add an Item at end of a list
list.extend() Add multiple Items at end of a list
list.insert() insert an Item at a defined index
list.remove() remove an Item from a list
del list[index] Delete an Item from a list
list.clear() empty all the list
list.pop() Remove an Item at a defined index
list.index() Return index of first matched item
list.sort() Sort the items of a list in ascending or descending order
list.reverse() Reverse the items of a list
len(list) Return total length of the list.
max(list) Return item with maximum value in the list.
min(list) Return item with min value in the list.
list(seq) Converts a tuple, string, set, dictionary into list.
Stack:
A stack is a linear data structure in which all the insertion and deletion
of data / values are done at one end only.
Applications of Stack:
Expression Evaluation
Expression Conversion
Parenthesis Checking
String Reversal
Function Call
Using list as Stack:
To add an item to the top of the list, i.e., to push an item, we use
append() function and to pop out an element we use pop() function.
Example:
class Stack:
def init (self):
self.items = []
def is_empty(self):
return self.items == []
def push(self, data):
self.items.append(data)
def pop(self):
return self.items.pop()
s = Stack()
while True:
print('Press 1 for push')
print('Press 2 for pop')
print('Press 3 for quit')
do = int(input('What would you like to do'))
if do == 1:
n=int(input("enter a number to push"))
s.push(n)
elif do == 2:
if s.is_empty():
print('Stack is empty.')
else:
print('Popped value: ', s.pop())
elif operation == 3:
break
Queue:
Applications of Queue
Synchronization
Scheduling
Searching
Interrupt handling
class Queue:
def init (self):
self.items = []
def isEmpty(self):
return self.items == []
def enqueue(self, item):
self.items.insert(0,item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
q = Queue()
while True:
print('Press 1 for insert')
print('Press 2 for delete')
print('Press 3 for quit')
do = int(input('What would you like to do'))
if do == 1:
n=int(input("enter a number to push"))
q.enqueue(n)
elif do == 2:
if q.isEmpty():
print('Queue is empty.')
else:
print('Deleted value: ', q.dequeue())
elif operation == 3:
break