Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

DATA STRUCTURE-LABORATORY

EXPT. No 1. Write a Program to Implement Insertion and Deletion of a Number at Specific Position in One
Dimensional Array.

Aim: To understand the concept of insertion & deletion operation and implement a program for
Insertion and Deletion of a number in one Dimensional Array.

Theory:
Insertion operation is used to insert a new element at specific position into one dimensional array.  In order
to insert a new element into one dimensional array we have to create space for new element. Suppose
there are N elements in an array, and we want to insert a new element between first and second element.
We have to move last N-1 elements down in order to create space for the new element.
Algorithm for Insertion in one Dimensional Array
Let A be a Linear Array (unordered) with N elements and LOC is a positive integer such that LOC<=N.
Following is the algorithm where ITEM is inserted into the LOC position of Array A
Step 1: TEMP = N-1
LOC = LOC-1
Step 2: Repeat Step 3 While TEMP ≥ LOC
Step 3: A[TEMP+1] = A[TEMP]
TEMP=TEMP-1
Step 4: A[LOC] = ITEM
Step 5: N = N+1

Algorithm for Deletion in one Dimensional Array


This operation is used to delete an element from specific position in one dimensional array. 
In order to delete an element from one dimensional array first we have to delete element from specified
position and then shift remaining elements upwards to take vacant space of the deleted element.
Step 1: POS = POS-1
TEMP = POS
Step 2: Return A[POS]
Step 3: Repeat step4 while TEMP ≤ N-1
Step 4: A[TEMP] = A[TEMP+1]
TEMP = TEMP+1
Step 5: N = N-1
Viva Questions:-
1. Define one dimensional array.
2. Explain the term index, value and address in one dimensional array.
3. What are the advantages of one dimensional array?
4. What are the disadvantages of one dimensional array?
5. What is static time compilation?

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE AND ENGG
DATA STRUCTURE-LABORATORY
EXPT. No 2: Write a Program to Implement Addition and Subtraction in a Two Dimensional Array.

Aim: To understand the concept of two-dimensional array and Implement the Addition & Subtraction
operations in a Two Dimensional Array.

Theory:
Algorithm for Matrix Addition
The addition of two matrices A m*n and Bm*n gives a matrix Cm*n. The elements of C are sum of corresponding
elements in A and B which can be shown as:

for i in 1 to m
for j in 1 to n
cij = aij + bij

Key points:
 Addition of matrices is commutative which means A+B = B+A
 Addition of matrices is associative which means A+(B+C) = (A+B)+C
 The order of matrices A, B and A+B is always same
 If order of A and B is different, A+B can’t be computed
 The complexity of addition operation is O(m*n) where m*n is order of matrices

Algorithm for Matrix Subtraction:-


The subtraction of two matrices Am*n and Bm*n gives a matrix Cm*n. The elements of C are difference of
corresponding elements in A and B which can be represented as:

for i in 1 to m
for j in 1 to n
cij = aij-bij
Key points:
 Subtraction of matrices is non-commutative which means A-B ≠ B-A
 Subtraction of matrices is non-associative which means A-(B-C) ≠ (A-B)-C
 The order of matrices A, B and A-B is always same
 If order of A and B is different, A-B can’t be computed
 The complexity of subtraction operation is O(m*n) where m*n is order of matrices

Viva Questions:-
1. Define two dimensional array.

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE AND ENGG
DATA STRUCTURE-LABORATORY
2. Explain row and column major representation of two dimensional array?
3. Differentiate between one dimensional and two dimensional array.
4. What is the running time complexity of Matrix Addition?
5. What is the running time complexity of Matrix Subtraction?

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE AND ENGG
DATA STRUCTURE-LABORATORY
EXPT. No 3. Write a Program to Implement Matrix Multiplication.

Aim: To understand the concept of matrix multiplication with the help of program.
Theory:
Matrix multiplication falls into two general categories:
 Scalar in which a single number is multiplied with every entry of a matrix
 Multiplication of an entire matrix by another entire matrix will refer to second category.
 In the scalar variety, every entry is multiplied by a number, called a scalar.

Matrix C and D below cannot be multiplied together because the number of columns in C does not
equal the number of rows in D. In this case, the multiplication of these two matrices is not defined
Algorithm for matrix multiplication
Matmul(a,b,m,n,p) for(i=1 to
m)
for(j = 1 to p)
c[i][j]=0;
For(k=1 to n)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
exit

Viva Questions:-
1. Consider the two matrices P and Q which are 10 x 20 and 20 x 30 matrices respectively. What is the
number of multiplications required to multiply the two matrices?
2. Consider the brute force implementation in which we find all the possible ways of multiplying the given
set of n matrices. What is the time complexity of this implementation?
3. Which of the following is the recurrence relation for the matrix chain multiplication problem
where mat[i-1] * mat[i] gives the dimension of the ith matrix?
4. What is the time complexity of the above dynamic programming implementation of the matrix
chain problem?

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE AND ENGG
DATA STRUCTURE-LABORATORY
EXPT. No 4. Write a Program to Implement Insertion and Deletion operations in Singly Linked List

Aim: To understand the concept of Singly Link List and implement a program for Insertion & Deletion
operations.
Theory:
Linked list is a collection of Nodes. Each node having two parts. First part represents value of the node
and second part represents an address of the next node. If Next node is not present then it contains NULL
value.

Consider Following Presentation of Linked List:

In above presentation FIRST is a pointer which always contains an address of first node in the linked
list. If linked list is empty then value of FIRST pointer is NULL.
In Linked List nodes are logically adjacent but physically not adjacent to each other. Because address of
first node is 2000, address of second node is 2010 and address of third node is 2002.

Algorithm of Insertion:-
In a singly linked list, the insertion operation can be performed in three ways. They are as follows:-
 Insertion at Beginning of the list
 Insertion at End of the list
 Insertion at Specific location in the list

Insertion at Beginning of the list


Step 1: Create a newNode with given value.
Step 2: Check whether list is Empty (head == NULL)
Step 3: If it is Empty then, set newNode→next = NULL and head = newNode.
Step 4: If it is Not Empty then, set newNode→next = head and head = newNode.

Insertion at End of the list


Step 1: Create a newNode with given value and newNode → next as NULL.
Step 2: Check whether list is Empty (head == NULL).
Step 3: If it is Empty then, set head = newNode.
Step 4: If it is Not Empty then, define a node pointer temp and initialize with head.
Step 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).
Step 6: Set temp → next = newNode.
Inserting At Specific location in the list (After a Node)
Step 1: Create a newNode with given value.
Step 2: Check whether list is Empty (head == NULL)

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE AND ENGG
DATA STRUCTURE-LABORATORY
Step 3: If it is Empty then, set newNode → next = NULL and head = newNode.
Step 4: If it is Not Empty then, define a node pointer temp and initialize with head.
Step 5: Keep moving the temp to its next node until it reaches to the node after which we want to
insert the newNode (until temp1 → data is equal to location, here location is the node value after
which we want to insert the newNode).
Step 6: Every time check whether temp is reached to last node or not. If it is reached to last node then
display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the function.
Otherwise move the temp to next node.
Step 7: Finally, Set 'newNode → next = temp → next' and 'temp → next = newNode'

Algorithm of deletion:
In a singly linked list, the deletion operation can be performed in three ways. They are as follows:-
 Deleting from Beginning of the list
 Deleting from End of the list
 Deleting a Specific Node

Deleting from Beginning of the list


Step 1: Check whether list is Empty (head == NULL)
Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
Step 3: If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4: Check whether list is having only one node (temp → next == NULL)
Step 5: If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions)
Step 6: If it is FALSE then set head = temp → next, and delete temp.

Deleting from End of the list


Step 1: Check whether list is Empty (head == NULL)
Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
Step 3: If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize 'temp1'
with head.
Step 4: Check whether list has only one Node (temp1 → next == NULL)
Step 5: If it is TRUE. Then, set head = NULL and delete temp1. And terminate the function. (Setting
Empty list condition)
Step 6: If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node. Repeat the same until
it reaches to the last node in the list. (until temp1 → next == NULL)
Step 7: Finally, Set temp2 → next = NULL and delete temp1.

Deleting a Specific Node from the list


Step 1: Check whether list is Empty (head == NULL)
Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
Step 3: If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize 'temp1'
with head.
Step 4: Keep moving the temp1 until it reaches to the exact node to be deleted or to the last node. And
every time set 'temp2 = temp1' before moving the 'temp1' to its next node.

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE AND ENGG
DATA STRUCTURE-LABORATORY
Step 5: If it is reached to the last node then display 'Given node not found in the list! Deletion not
possible!!!'. And terminate the function.
Step 6: If it is reached to the exact node which we want to delete, then check whether list is having only
one node or not
Step 7: If list has only one node and that is the node to be deleted, then set head = NULL and delete
temp1 (free(temp1)).
Step 8: If list contains multiple nodes, then check whether temp1 is the first node in the list (temp1 ==
head).
Step 9: If temp1 is the first node then move the head to the next node (head = head → next) and delete
temp1.
Step 10: If temp1 is not first node then check whether it is last node in the list (temp1 → next ==
NULL).
Step 11: If temp1 is last node then set temp2 → next = NULL and delete temp1 (free(temp1)).
Step 12: If temp1 is not first node and not last node then set temp2 → next = temp1 → next and delete
temp1 (free(temp1)).

Viva Questions:-
1. What is singly linked list?
2. What are the advantages of singly linked list?
3. What are the applications of singly linked list?
4. What is the condition for linked list Overflow & Underflow?
5. How linked list is represented in memory?

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE AND ENGG
DATA STRUCTURE-LABORATORY
EXPT. No 5. Write a Program to Implement Stack using Array

Aim: To understand the concept of stack and learn the implementation of stack using array.
Theory:
A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO)
principle. In the pushdown stacks only two operations are allowed: push the item into the stack, and
pop the item out of the stack. A stack is a limited access data structure - elements can be added and
removed from the stack only at the top. Push adds an item to the top of the stack, pop removes the
item from the top. A helpful analogy is to think of a stack of books; you can remove only the top book,
also you can add a new book on the top.
Algorithm for Push Operation
begin procedure push: stack, data if stack is full
return null
end if
top ← top + 1
stack[top] ← data
end procedure

Algorithm for Pop Operation


begin procedure pop: stack
if stack is empty
return null
endif
data ← stack[top]
top ← top - 1 return data
end procedure

Viva Questions:-
1. What is a stack?
2. What are the applications of stack?
3. Explain push algorithm of stack?
4. Explain pop algorithm of stack?
5. Why stack is known as LIFO?

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE DEPARTMENT OF COMPUTER SCIENCE AND ENGG

You might also like