Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
91 views4 pages

Chapter 9 (Data Structure - I)

Uploaded by

paxecam470
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views4 pages

Chapter 9 (Data Structure - I)

Uploaded by

paxecam470
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Chapter – 9

Data Structures – I: Linear List


DATA STRUCTURE – A data structure is a named group of data of different data types which is stored in a specific
way and can be processed as a single unit. A data structure has well defined operations, behavior and properties.
TYPE OF DATA STRUCTURE – The data structure can be classified into following two types.
1. Simple Data Structures – These data structures are normally built from primitive data types like integers,
reals, characters, Boolean.
Example – Array or Linear List
2. Compound Data Structures – Simple data structure can be combined in various way form more complex
structures called compound data structures. Compound data structures are classified into following two
types.
a. Linear Data Structure – These data structures are single level data structure.
Example – Stack, Queue, Linked List
b. Non Linear Data Structure – These data structures are multilevel level data structure.
Example – Tree, Graph
LINEAR LIST – Linear Lists or Arrays are one of the simplest data structures and are very easy to traverse, search,
sort etc. A linear is a sequence of n>=0 elements, E1, E2 ………. En where each element Ei has the same data type T.
Searching in a Linear List – There are two types of searching in Linear List.
1. Linear Search
2. Binary Search
Linear Search – In Linear Search, each element of the array/ linear list is compared with the given item to be
searched for, one by one.
def linearsearch(a,s,size):
found=0
for i in range(size):
if a[i]==s:
print("The element is found at position",i+1)
found=1
break;
if found==0:
print("The element is not found in list")
a=[]
size=int(input("Enter the size of the list"))
for i in range(size):
val=int(input("enter a number"))
a.append(val)
s=int(input("Enter the number for search"))
linearsearch(a,s,size)

Binary Search – Binary search can work for only sorted arrays. It works faster than linear search.
def binarysearch(a,s,size):
f=0
first=0
last=size-1
mid=0

while first<=last and f==0:


mid=(first+last)//2
if a[mid]==s:
f=1
pos=mid+1
if a[mid]>s:
last=mid-1
if a[mid]<s:
first=mid+1
if f==1:
print("no is found at position",pos)
else:
print("no is not found")
#_____main program______
size=int(input("Enter the size of the list"))
a=[]
for i in range(size):
val=int(input("enter a number"))
a.append(val)
print("You entered list as =",a)
a.sort()
print("After sorting the List is =",a)
s=int(input("Enter the number for search"))
binarysearch(a,s,size)
Insertion in Linear list –
def insert(record,n,pos):
i=n-1
record.append(None)
while i>=0 and record[i]>pos:
record[i+1]=record[i]
i=i-1
record[i+1]=pos
print("The inserted list:",record)
record=[]
n=int(input("How many element you want to enter in list"))
for i in range(n):
val=int(input("Enter the element:"))
record.append(val)
pos=int(input("Enter the element which you want to insert"))
print("The original list is:",record)
insert(record,n,pos)
Deletion in Linear list –
def deletion(a,s,size):
f=0
first=0
last=size-1
mid=0
while first<=last and f==0:
mid=(first+last)//2
if a[mid]==s:
f=1
pos=mid
if a[mid]>s:
last=mid-1
if a[mid]<s:
first=mid+1
if f==1:
return pos
else:
return -1
#_____main program______
size=int(input("Enter the size of the list"))
a=[]
for i in range(size):
val=int(input("enter a number"))
a.append(val)
print("You entered list as =",a)
a.sort()
print("After sorting the List is =",a)
s=int(input("Enter the deleted element:"))
x=deletion(a,s,size)
if x==-1:
print("Element not found")
else:
del a[x]
print("list after deletion:",a)

Bubble Sort in Linear List –


def bubble(a,size):
for i in range(size):
for j in range(size-i-1):
if a[j]>a[j+1]:
c=a[j]
a[j]=a[j+1]
a[j+1]=c
print("Sorted elements are:",a)
#_____main program______
size=int(input("Enter the size of the list"))
a=[]
for i in range(size):
val=int(input("enter a number"))
a.append(val)
print("You entered list as:",a)
x=bubble(a,size)

Insertion Sort in Linear List –


def insertion(a,size):
for i in range(1,size):
t=a[i]
j=i-1
while j>=0 and t<a[j]:
a[j+1]=a[j]
j=j-1
else:
a[j+1]=t
print("Sorted elements are:",a)
#_____main program______
size=int(input("Enter the size of the list"))
a=[]
for i in range(size):
val=int(input("enter a number"))
a.append(val)
print("You entered list as:",a)
x=insertion(a,size)

List Comprehension – A list comprehension is short-hand for a loop that creates a list. A list comprehension is
usually shorter, more readable and more efficient.
Advantages of List Comprehension – List comprehension are considered more Pythonic as they truly represent
Python coding style and also offer these advantages:
1. Code reduction
2. Faster code processing
Examples –
Question1. Write equivalent list comprehension for the following code:
A=[ ]
for a in range(1,6):
A.append(a*2)
Solution – A = [a*2 for a in range(1,6)]
Question2. Write equivalent list comprehension for the following code:
A=[]
for a in range(1,50):
if a%7==0:
A.append(a)
Solution – A = [a for a in range(1,50) if a%7==0]

Question3. What will be the output of the following code?


A = [num if num<5 else num*2 for num in range(2,9)]
Solution – [2,3,4,10,12,14,16]

Question4. Given an input list val below, produce a list namely mul3, using a list comprehension having the
numbers from val that are multiple of 3.
val = [31,15,42,12,5,39,21,61,25]
Solution – mul3 = [a for a in val if a%3==0]

Question5. Consider the following code. What will the list Res be storing?
Res=["Ev" if i%2==0 else "Od" for i in range(10,20)]
print(Res)
Solution – ['Ev', 'Od', 'Ev', 'Od', 'Ev', 'Od', 'Ev', 'Od', 'Ev', 'Od']

Question6. What will be the output of the following code?


[x+y for x in 'ball' for y in 'boy']
Solution – ['bb', 'bo', 'by', 'ab', 'ao', 'ay', 'lb', 'lo', 'ly', 'lb', 'lo', 'ly']

Question7. Write equivalent list comprehension for the following code:


Result = [ ]
for x in [10,5,2]:
for y in [2,3,4]:
Result.append(x**y)
print(Result)
Solution – Result = [x**y for x in[10,5,2] for y in [2,3,4]]
Output – [100, 1000, 10000, 25, 125, 625, 4, 8, 16]

Question8. What will be the output produced by following code?


text=['h','e','l','l','o']
print(text)
vowels="aeiou"
newtext=[ ]
newtext=[x.upper() for x in text if x not in vowels]
print(newtext)
Solution – ['h', 'e', 'l', 'l', 'o']
['H', 'L', 'L']

Question9. What will be the output produced by following code?


ages=[11,14,15,17,13,18,25]
print(ages)
e=[x for x in ages if x in range(14,18)]
print(e)
Solution – [14, 15, 17]

You might also like