50 Kash Sharma DS
50 Kash Sharma DS
50 Kash Sharma DS
DATA STRUCTURE
SUBMITTED BY
SYDS
SEMESTER III
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.
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.
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:
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.
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.
a. Define a node current which will initially point to the head of the list.
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.
a. It first checks whether the head is null, then it will insert the node as the head.
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.
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.
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.
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
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 :
Output:
Experiment NO 6 : Implement the following sorting techniques:
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
Output:
Experiment NO 7: Implement the following sorting techniques:
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.
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 :