Data Structure Lab Manual_CS_303
Data Structure Lab Manual_CS_303
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
Year/Sem : II/III
OBJECTIVE:
To understand the concept of Array.
ALGORITHM:
Addition:
1. We first define three matrices A, B, C and read their respective row and column numbers in
variable r and c
2. Read matrices A and B.
3. First, start a loop for getting row elements of A and B
Secondly, inside it again start a loop for column of A and B
4. Then, we store their corresponding addition by C[i][j]=A[i][j] + B[i][j] into C[i][j]
Subtraction:
1. We first define three matrices A, B, C and read their respective row and column numbers in
variable r and c
2. Read matrices A and B.
3. First, start a loop for getting row elements of A and B
Secondly, inside it again start a loop for column of A and B
4. Then, we store their corresponding Subtraction by C[i][j]=A[i][j] - B[i][j] into C[i][j]
Multiplication:
1. We first define three matrices A, B, C and read their respective row and column numbers in
variable r and c
2. Read matrices A and B.
3. First, start a loop for getting row elements of A and B
Secondly, inside it again start a loop for column of A and B
4. Then, we store their corresponding Multiplication by C[k][i]=A[k][j] * B[j][i] into C[k][i]
Transpose:
1. C1 [i] [j] = C2 [j] [i]
INPUT SET:
OUTPUT SET:
EXPECTED VIVA QUESTIONS:
Q.1 what is array?
Q.2 explains different types of array?
Q.3 how to perform addition, subtraction, multiplication and division operation in 2-D array?
NAME OF FACULTY:
SIGNATURE:
DATE:
EXPERIMENT NO. 2
Unit/Topic: 1/ Linked List
PROBLEM DEFINITION:
Write a program to insert an element in the beginning and end of singly linked list.
OBJECTIVE:
To understand the concept of linked list.
ALGORITHM:
At the Beginning:
1. Create a newNode with given value.
2. Check whether list is Empty (head == NULL)
3. If it is Empty then, set newNode→next = NULL and head = newNode.
4. If it is Not Empty then, set newNode→next = head and head= newNode.
At the End:
1. Create a newNode with given value and newNode → next as NULL.
2. Check whether list is Empty (head == NULL).
3. If it is Empty then, set head = newNode.
4. If it is Not Empty then, define a node pointer temp and initialize with head.
5. Keep moving the temp to its next node until it reaches to the last node in the list (until temp
→ next is equal to NULL).
6. Set temp → next = newNode.
INPUT SET:
OUTPUT SET:
NAME OF FACULTY:
SIGNATURE:
DATE:
EXPERIMENT NO. 3
Unit/Topic: 1/ Linked List
PROBLEM DEFINITION:
Write a program to insert and delete a node at any position in doubly linked list.
OBJECTIVE:
To understand insertion and deletion operation in doubly linked list.
ALGORITHM:
Insert an element at any position:
1. IF PTR = NULL
a. Write OVERFLOW
Go to Step 15
[END OF IF]
2. SET NEW_NODE = PTR
3. SET PTR = PTR -> NEXT
4. SET NEW_NODE -> DATA = VAL
5. SET TEMP = START
6. SET I = 0
7. REPEAT 8 to 10 until I<="" li="">
8. SET TEMP = TEMP -> NEXT
9. IF TEMP = NULL
10. WRITE "LESS THAN DESIRED NO. OF ELEMENTS"
a. GOTO STEP 15
[END OF IF]
[END OF LOOP]
11. SET NEW_NODE -> NEXT = TEMP -> NEXT
12. SET NEW_NODE -> PREV = TEMP
13. SET TEMP -> NEXT = NEW_NODE
14. SET TEMP -> NEXT -> PREV = NEW_NODE
15. EXIT
INPUT SET:
OUTPUT SET:
NAME OF FACULTY:
SIGNATURE:
DATE:
EXPERIMENT NO. 4
Unit/Topic: 1/ Linked List
PROBLEM DEFINITION:
Write a program to insert an element at the beginning and end of circular linked list.
OBJECTIVE:
To understand the concept of circular linked list.
ALGORITHM:
At Beginning of list:
1. Create a newNode with given value.
2. Check whether list is Empty (head == NULL)
3. If it is Empty then, set head = newNode and newNode→next= head .
4. If it is Not Empty then, define a Node pointer 'temp' and initialize with 'head'
5. Keep moving the 'temp' to its next node until it reaches to the last node (until 'temp →
next == head').
6. Set 'newNode → next =head', 'head = newNode' and 'temp → next = head'.
At End of list:
1. Create a newNode with given value.
2. Check whether list is Empty (head == NULL).
3. If it is Empty then, set head = newNode and newNode → next = head.
4. If it is Not Empty then, define a node pointer temp and initialize with head.
5. Keep moving the temp to its next node until it reaches to the last node in the list (until temp
→ next == head).
6. Set temp → next = newNode and newNode → next = head.
INPUT SET:
OUTPUT SET:
NAME OF FACULTY:
SIGNATURE:
DATE:
EXPERIMENT NO. 5
Unit/Topic: 2/ Stack
PROBLEM DEFINITION:
Write a program to implement stack using array.
OBJECTIVE:
To understand concept of push and pop operation in stack.
ALGORITHM:
Push element
int push()
{
if(top==length-1) "stack overflow";
else
top+1;
stack[top]=data;
}
Pop element
int pop()
{
int y;
if(top==-1) "stack is underflow";
else
y=stack[top];
top-1;
}
INPUT SET:
OUTPUT SET:
NAME OF FACULTY:
SIGNATURE:
DATE:
EXPERIMENT NO.6
Unit/Topic: 2/ Queue
PROBLEM DEFINITION:
Write a program to implement queue using array.
OBJECTIVE:
To understand Enqueue and De-queue in queue.
ALGORITHM:
Insert element
enqueue(data)
if queue is full
return overflow
endif
rear ← rear + 1
queue[rear] ← data
return true
Delete element
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
INPUT SET:
OUTPUT SET:
NAME OF FACULTY:
SIGNATURE:
DATE:
EXPERIMENT NO.7
Unit/Topic: 2/ Circular Queue
PROBLEM DEFINITION:
Write a program to implement circular queue using array.
OBJECTIVE:
To understand working of circular queue.
ALGORITHM:
Insert Circular ( ):
1.If (FRONT == 1 and REAR == N) or (FRONT == REAR + 1) Then
2. Print: Overflow
3. Else
4. If (REAR == 0) Then [Check if QUEUE is empty] (a) Set FRONT = 1 (b) Set REAR = 1
5. Else If (REAR == N) Then [If REAR reaches end if QUEUE]
6. Set REAR = 1
7. Else
8. Set REAR = REAR + 1 [Increment REAR by 1] [End of Step 4 If]
9. Set QUEUE[REAR] = ITEM
10. Print: ITEM inserted [End of Step 1 If]
11. Exit
Delete Circular ( ):
1.If (FRONT == 0) Then [Check for Underflow]
2. Print: Underflow
3. Else
4. ITEM = QUEUE[FRONT]
5. If (FRONT == REAR) Then [If only element is left] (a) Set FRONT = 0 (b) Set REAR = 0
6. Else If (FRONT == N) Then [If FRONT reaches end if QUEUE]
7. Set FRONT = 1
8. Else
9. Set FRONT = FRONT + 1 [Increment FRONT by 1] [End of Step 5 If]
10. Print: ITEM deleted [End of Step 1 If]
11. Exit
INPUT SET:
OUTPUT SET:
NAME OF FACULTY:
SIGNATURE:
DATE:
EXPERIMENT NO.8
Unit/Topic: 5/ Sorting
PROBLEM DEFINITION:
Write program for implement Insertion sort.
OBJECTIVE:
To implement Insertion Sort.
ALGORITHM:
1. insertionSort(array)
2. mark first element as sorted
3. for each unsorted element X
4. 'extract' the element X
5. for j <- lastSortedIndex down to 0
6. if current element j > X
7. move sorted element to the right by 1
8. break loop and insert X here
9. end insertionSort
INPUT SET:
OUTPUT SET:
NAME OF FACULTY:
SIGNATURE:
DATE:
EXPERIMENT NO.9
Unit/Topic: 5/ Sorting
PROBLEM DEFINITION:
Write program for implement Selection sort.
OBJECTIVE:
To implement Selection Sort.
ALGORITHM:
1. selectionSort(array, size)
2. repeat (size - 1) times
3. set the first unsorted element as the minimum
4. for each of the unsorted elements
5. if element < currentMinimum
6. set element as new minimum
7. swap minimum with first unsorted position
8. end selectionSort
INPUT SET:
OUTPUT SET:
NAME OF FACULTY:
SIGNATURE:
DATE:
EXPERIMENT NO.10
Unit/Topic: 3/ Tree
PROBLEM DEFINITION:
Write a program to search an element in binary search.
OBJECTIVE:
To implement Binary search.
ALGORITHM:
1. Compare x with the middle element.
2. If x matches with middle element, we return the mid index.
3. Else if x is greater than the mid element, then x can only lie in right half sub array after the
mid element. So we recur for right half.
4. Else (x is smaller) recur for the left half.
INPUT SET:
OUTPUT SET:
NAME OF FACULTY:
SIGNATURE:
DATE: