Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

50 Kash Sharma DS

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

JOURNAL

IN THE MAJOR COURSE

DATA STRUCTURE

SUBMITTED BY

MR. KASH SHARMA

SYDS

Roll no. SDDS050A

SEMESTER III

UNDER THE GUIDANCE OF

ASST. PROF. HIRAL SACHALA

ACADEMIC YEAR

2023 - 2024
CERTIFICATE

This is to certify that MR. KASH SHARMA of SECOND year B.SC.DS Div.:
A, Roll No. SDDS050A of Semester III (2023 - 2024) has successfully
completed the Journal for the Major course DATA STRUCTURE as per the
guidelines of KES’ Shroff College of Arts and Commerce, Kandivali(W),
Mumbai-400067.

Teacher In-charge Principal


ASST. PROF. HIRAL SACHALA Dr. L Bhushan.
INDEX

Sr. Practical Practical Name Page No.


No. No.
1. 1 Write a program to store the elements in 1 D array and 4–8
perform the operations like searching, reversing the
elements {menu driven}.
2. 2 Write a program to create a single linked list and display 9 – 11
the node elements in reverse.
3. 3 Write a program to create a double linked list and sort the 12 – 15
elements in the linked list.
4. 4 Write a program to implement the concept of stack with 16 – 18
push, pop, display and exit operations.
5. 5 Write a program to implement the concept of Queue with 19 – 21
insert, delete, display and exit operation.
6. 6 Write a program to implement the bubble sort. 22
7. 7 Write a program to search the element using sequential 23, 24
search.
8. 8 Write a program to implement inorder, preorder and 25 – 28
postorder.
9. 9 Write a program to insert the element into maximum 29 – 31
heap.
10. 10 Write a program to implement the collision technique. 32 – 34
11. 11 Write a program to generate the adjacency matrix. 35, 36
Experiment NO 1

Aim : Write a program to store the elements in 1 D array and perform the operations like
searching and reversing the elements {menu driven}

Solution :

Arrays are represented as a collection of buckets where each bucket stores one element. These
buckets are indexed from ‘0’ to ‘n-1’, where n is the size of that particular array.

Insertion operation at specific location of


indexAlgorithm

Step 1: Start
Step 2: take two inputs from the user(let’s say element and
position)Step 3: if index >= len(arr):
print(“please enter index smaller than”,len(arr))
else:
arr.insert(index, num)
Step 4: print the array
(list)Step 5: Stop

Search OperationAlgorithm
Consider LA is a linear array with N elements and K is a positive integer such that
K<=N. Following is the algorithm to find an element with a value of ITEM using
sequential search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop

Reversing Algorithm :
Input: arr[], size
if length of array is 1, then return arr.
elif length of array is 2, swap first and last number and return arr.
otherwise initialize i=0.
Loop for i in size//2
swap first present and last present numbers.
if first and next numbers indexes are not same, then swap next and last of next
numbers.increment i+=2.
Sorting ascending or descendingAlgorithm:

Input size of array.


Store it in some variable
say nInput array say arr
Use arr.sort(reverse=False) or arr.sort() for ascending order of
array.Use arr.sort(reverse=True) for descending order of array

Program :

S
Output:
Experiment NO 2 : Implement the following for Linked List:

Aim : Write a program to create a single linked list and display the node elements in
reverseorder.

Solution :

A linked list is a linear data structure where every element acts as a node with two parts data
part(which stores the actual data of the element) and the address part(which stores the
address ofthe location of the next node).

Algorithm
1. Create a class Node which has two attributes: data and next. Next is a pointer to the
nextnode in the list.

2. Create another class which has two attributes: head and tail.

3. addNode() will add a new node to the list:

a. Create a new node.

b. It first checks, whether the head is equal to null which means the list is empty.

c. If the list is empty, both head and tail will point to the newly added node.

d. If the list is not empty, the new node will be added to end of the list such that
tail'snext will point to the newly added node. This new node will become the
new tail of the list.

4. reverse() will reverse the order of the nodes present in the list.

a. This method checks whether node next to current is null which implies
that,current is pointing to tail, then it will print the data of the tail node.

b. Recursively call reverse() by considering node next to current node and prints
outall the nodes in reverse order starting from the tail.

5. display() will display the nodes present in the list:

a. Define a node current which will initially point to the head of the list.

b. Traverse through the list till current points to null.

c. Display each node by making current to point to node next to it in each


iteration.
Program :
Output :
Experiment NO 3.

Aim: Write a program to create a double linked list and sort the elements inthe linked list.

Algorithm

1. Define a Node class which represents a node in the list. It will have three properties:
data,previous which will point to the previous node and next which will point to the
next node.
2. Define another class for creating a doubly linked list, and it has two nodes: head and
tail.Initially, head and tail will point to null.

3. addNode() will add node to the list:

a. It first checks whether the head is null, then it will insert the node as the head.

b. Both head and tail will point to a newly added node.

c. Head's previous pointer will point to null and tail's next pointer will point to
null.

d. If the head is not null, the new node will be inserted at the end of the list such
thatnew node's previous pointer will point to tail.
e. The new node will become the new tail. Tail's next pointer will point to null.

4. sortList() will sort nodes of the list in ascending order.

a. Define a node current which will point to head.

b. Define another node index which will point to node next to current.

c. Compare data of current and index node. If the current's data is greater
than theindex's data, then swap the data between them.
d. Current will point to current.next and index will point to index.next.

e. Continue this process till the entire list is sorted.

5. display() will show all the nodes present in the list.

a. Define a new node 'current' that will point to the head.

b. Print current.data till current points to null.

c. Current will point to the next node in the list in each iteration.
Program :
Output :
Experiment NO 4 : Implement the following for Stack:

Aim : Write a program to implement the concept of stack with push, pop, display and
exitoperations .
Solution :

A linked list is a linear data structure where every element acts as a node with two parts data
part(which stores the actual data of the element) and the address part(which stores the
address ofthe location of the next node).

Algorithm

push()

It stacks up a new item. A stack overflow circumstance is when the stack is completely

full.Push :

1. begin
2. if stack is full
3. return
4. endif
5. else
6. increment top
7. stack[top] assign value
8. end else
9. end procedure
Pop:

pop()

It takes something out of the stack. In the opposite sequence from which they were pushed,
thethings are popped. The condition is referred to as an underflow if the stack is empty.

Algorithm for pop():


1. begin
2. if stack is empty
3. return
4. endif
5. else
6. store value of stack[top]
7. decrement top
8. return value
9. end else
10. end procedure

The operations work as follows:

A pointer called TOP is used to keep track of the top element in the stack.

When initializing the stack, we set its value to -1 so that we can check if the stack is empty
bycomparing TOP == -1.

On pushing an element, we increase the value of TOP and place the new element in the
positionpointed to by TOP.

On popping an element, we return the element pointed to by TOP and reduce its

value.Before pushing, we check if the stack is already full

Before popping, we check if the stack is already empty

Working of Stack Data Structure


Program :

Output:
Experiment NO 5 : Implement the following for Queue:

Aim : Write a program to implement the concept of Queue with insert, Delete , Display and
ExitOperation.
Solution :

Queue operations work as follows:

● two pointers FRONT and REAR


● FRONT track the first element of the queue
● REAR track the last element of the queue
● initially, set value of FRONT and REAR to -1
Algorithm :
Enqueue Operation :
● check if the queue is full
● for the first element, set the value of FRONT to 0
● increase the REAR index by 1
● add the new element in the position pointed to by REAR
Dequeue Operation :
● check if the queue is empty
● return the value pointed by FRONT
● increase the FRONT index by 1
● for the last element, reset the values of FRONT and REAR to -1
Program :

Output:
Experiment NO 6 : Implement the following sorting techniques:

Aim : Write a program to implement the bubble sort.


Solution :

Algorithm: BUBBLE_SORT(A, N)
Step 1: Repeat Step 2 For I= 0 to N-1 // to keep track of the number of iterations
Step 2: Repeat For J= 0 to N-I // to compare the elements within the particular iteration
Step 3: IF A[J] > A[J+1] // swap if any element is greater than its adjacent element SWAP
A[J]and A[J+1] [END OF INNER LOOP] [END OF OUTER LOOP]
Step 4: EXIT

# Python program for implementation of Bubble Sort

Output:
Experiment NO 7: Implement the following sorting techniques:

Aim : Write a program to search the element using sequential search .


Solution :

Algorithm:
LinearSearch(array,
key) for each item in
the array
if item ==
value
return its
index
Program :

Output:
Experiment NO 8
Aim : Write a program to implement inorder, preorder and postorder.

Solution :
Algorithm :
Preorder Traversal:
Algorithm Preorder(tree)
Visit the root.
Traverse the left subtree, i.e., call Preorder(left-
>subtree) Traverse the right subtree, i.e., call
Preorder(right->subtree)

Inorder Traversal:
Algorithm Inorder(tree)
Traverse the left subtree, i.e., call Inorder(left-
>subtree)Visit the root.
Traverse the right subtree, i.e., call Inorder(right->subtree)

Postorder Traversal:
Algorithm Postorder(tree)
Traverse the left subtree, i.e., call Postorder(left-
>subtree) Traverse the right subtree, i.e., call
Postorder(right->subtree) Visit the root
Output :
Experiment NO 9
Aim : Write a program to insert the element into maximum heap.

Algorithm :
Step 1 - Insert the newNode as last leaf from left to
right. Step 2 - Compare newNode value with its
Parent node.
Step 3 - If newNode value is greater than its parent, then swap both of them.
Step 4 - Repeat step 2 and step 3 until newNode value is less than its parent node (or)
newNodereaches to root.
Output :
Experiment NO 10
Aim : Write a program to implement the collision technique.

Solution : Collision avoiding and solving using chaining

Algorithm :

chainedHashSearch
(T, k)return T[h(k)]
chainedHashInsert(
T, x)
T[h(x.key)] = x //insert at the
head chainedHashDelete(T, x)
T[h(x.key)] = NIL

Program :
Output :
Experiment NO 11
Aim : Write a program to generate the adjacency matrix.
Program :
Output :

You might also like