[go: up one dir, main page]

0% found this document useful (0 votes)
24 views6 pages

Data Structure Manual

Uploaded by

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

Data Structure Manual

Uploaded by

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

Ex.

No:1
Implement simple ADTs as Python classes
Date:

AIM:

ALGORITHM:
1.The data is generally stored in key sequence in a list which has a head structure consisting
of count, pointers and address of compare function needed to compare the data in the list.
2.The data node contains the pointer to a data structure and a self-referential pointer which points to the
next node in the list.
 get() – Return an element from the list at any given position.
 insert() – Insert an element at any position of the list.
 remove() – Remove the first occurrence of any element from a non-empty list.
 removeAt() – Remove the element at a specified location from a non-empty list.
 replace() – Replace an element at any position by another element.
 size() – Return the number of elements in the list.
 isEmpty() – Return true if the list is empty, otherwise return false.
 isFull() – Return true if the list is full, otherwise return false.

PROGRAM:
class Student:
def __init__(self, first, last, age, major):
self.first = first
self.last = last
self.age = age
self.major = major
self.courses = []
def profile(self):
print(f"Student name {self.first + ' ' + self.last}")
print(f"Student age: {self.age}")
print(f"Major: {self.major}")
def enrol(self, course):
self.courses.append(course)
print(f"enrolled {self.first} in {course}")
def show_courses(self):
print(f"{self.first + '' + self.last} is taking the following courses")
for course in self.courses:
print(course)
s = Student('Sally' , 'Harris', 20, 'Biology')
s.enrol('Biochemistry I')
s.enrol('Literature')
s.enrol('Mathematics')
s.show_courses()
Ex.No:2
Implement Recursive Algorithms in Python
Date:

RESULT :

AIM:
ALGORITHM:
i) Fibonacci series
1.Input the number of values we want to generate the Fibonacci sequence and initialize a=0,
b=1, sum=0, and count=1.
2.Start a while loop using the condition count<=n and print the sum every time the condition
works.
3.Increment the count variable, swap 'a' and 'b,' and store the addition of a and b in the sum.
ii)Factorial of a number
1.Get a positive integer input (n) from the user.
2.Iterate from 1 to n using a for loop (for loop is used to increment the number up to the given
input).
3.Using the below formula, calculate the factorial of a number f = f*i.
4.Print the output i.e, the calculated factorial.

PROGRAM:
i) Fibonacci series
class Fibonacci:
def __init__(self, n):
self.n = n
self.i = 0

def __iter__(self):
return self

def __next__(self):
if self.i >= self.n:
raise StopIteration('Exhausted!!!')
fib_i = Fibonacci.fib(self.i)
self.i += 1
return fib_i
def fib(n):
if n < 2:
return n
else:
return Fibonacci.fib(n-1) + Fibonacci.fib(n-2)
f = Fibonacci(5)
for i in range(5):
print(next(f), end=' ')

ii)Factorial of a number
class Factorial:
def __init__(self, n):
self.n = n
self.i = 0

def __iter__(self):
return self

def __next__(self):
if self.i >= self.n:
raise StopIteration('Exhausted!!!')
factr = Factorial.fact(self.n)
self.i += 1
return factr
def fact(n):
if n <= 1:
return n
else:
return n * Factorial.fact(n-1)
f = Factorial(4)
print(next(f), end=' ')

OUTPUT:

RESULT:
Ex.No:3 Implement List ADT using Python arrays
Date:

AIM:

ALGORITHM:

1. Allocate a new array B with larger capacity (A commonly used rule for the new array is
to have twice the capacity of the existing array)
2. Set B[i]=A[i], for i=0 to n-1 where n denotes the current no of items.
3. Set A=B that is, we hence forth use B as the array of supporting list.
4. Insert new element in the new array.

PROGRAM:

import array
arr = array.array('i', [1, 2, 3])
print ("The new created array is : ",end=" ")
for i in range (0, 3):
print (arr[i], end=" ")
print("\r")
arr.append(4);
print("The appended array is : ", end="")
for i in range (0, 4):
print (arr[i], end=" ")
arr.insert(2, 5)
print("\r")
print ("The array after insertion is : ", end="")
for i in range (0, 5):
print (arr[i], end=" ")
print ("The popped element is : ",end="")
print (arr.pop(2));
print ("The array after popping is : ",end="")
for i in range (0,4):
print (arr[i],end=" ")
print("\r")
arr.remove(1)
print ("The array after removing is : ",end="")
for i in range (0,3):
print (arr[i],end=" ")
print ("The index of 1st occurrence of 2 is : ",end="")
print (arr.index(2))
arr.reverse()
print ("The array after reversing is : ",end="")
for i in range (0,2):
print (arr[i],end=" ")

RESULT:

You might also like