DS Lab Manual (Jntu)
DS Lab Manual (Jntu)
DS Lab Manual (Jntu)
Laboratory Manual
For
It is my great pleasure to present this laboratory manual for Second year engineering
students for the subject of Data Structures (Using C).
As a student, many of you may be wondering with some of the questions in your
mind regarding the subject and exactly what has been tried is to answer
through this manual.
As you may be aware that MGM has already been awarded with ISO 9001:2000
certification and it is our endure to technically equip our students taking the
advantage of the procedural aspects of ISO 9001:2000 Certification.
Faculty members are also advised that covering these aspects in initial stage
itself, will greatly relieve them in future as much of the load will be taken care
by the enthusiasm energies of the students once they are conceptually clear.
Dr. S. D. Deshmukh
Principal
LABORATORY MANUAL CONTENTS
This manual is intended for the Second year students of Computer Science and
Engineering in the subject of Data Structures (Using C). This manual typically
contains practical/lab sessions related Data Structures implemented in C covering
various aspects related the subject to enhanced understanding.
Students are advised to thoroughly go through this manual rather than only topics
mentioned in the syllabus as practical aspects are the key to understanding and
conceptual visualization of theoretical aspects covered in the books.
II. Preparing graduates for higher education and research in computer science and engineering
enabling them to develop systems for society development.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to
the professional engineering practice.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader
in diverse teams, and in multidisciplinary settings.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
LAB INDEX
6. Design, develop and execute a program in C to read a sparse matrix of integer values
and make a transpose of it. Use the triple <row, column, value> to represent an element
in sparse matrix.
7. Design, develop and execute a program in C to implement singly linked list where
each node consist of integers. The program should support following functions.
a. Create a singly linked list
b. Insert a new node
c. Delete a node if it is found, otherwise display appropriate message
d. Display the nodes of singly linked list
8. Design, develop and execute a program in C to implement doubly linked list where
each node consist of integers. The program should support following functions.
a. Create a doubly linked list
b. Insert a new node
c. Delete a node if it is found, otherwise display appropriate message
d. Display the nodes of doubly linked list
9. Using array representation for a polynomial, design, develop and execute a program
in C to add two polynomials and then print the resulting polynomial.
10. Write a program in C to construct binary tree and binary tree traversal
11. Design, develop and execute a program in C to create a max heap of integers by
accepting one element at a time and by inserting it immediately in to heap. Use the
array representation of heap. Display the array at the end of insertion phase.
DOs and DON’Ts in Laboratory:
1. Make entry in the Log Book as soon as you enter the Laboratory.
2. All the students should sit according to their roll numbers starting from their left to
right.
3. All the students are supposed to enter the terminal number in the log book.
1. Submission related to whatever lab work has been completed should be done during the
next lab session.
2. The immediate arrangements for printouts related to submission on the day of practical
assignments.
3. Students should be taught for taking the printouts under the observation of lab teacher.
4. The promptness of submission should be encouraged by way of marking and evaluation
patterns that will benefit the sincere students.
1. Lab Exercise
• Write a Program to read in one dimensional , calculate the sum and display it
Output
Enter number of students 3
Enter rollno , name and percentage of 3 students
1 Ajay 70
2 Rahul 80
3 Yogesh 90
1 Ajay 70
2 Rahul 80
3 Yogesh 90
Conclusion
Declaration:--
Type Stack = array [1.. stacksize] of integer;
Stacklimit = 0..stacksize;
Algorithm: --
Data Structure: An Array with TOP as the pointer.
Start
Initialize the array of 10 elements and name it as stack
Initialize other variables like top in the beginning of the program
Provide the choice to the users for the different operations on stack like
Push(insert),Pop(delete),Display and Exit
If the choice= push then call the function push()
If the choice= pop then call the function pop()
If the choice= display then call the function display()
If the choice= exit then exit from the program end
Function push()
Check for the overflow condition of the stack
if (TOP>= SIZE) then Print “Stack is full”
Function pop()
Function display()
Note: Ask the students to list the drawbacks of implementing the Stack using array.
The above program could be repeated using structures and linked lists
Output
The stack is 11 22 33
Conclusion
All the stack operations are successfully performed using the switch case
3. Lab Exercises:
Declaration:--
Type queue = array [1.. n] of integer;
Var p: queue
Algorithm:--
Data Structure: Q is the array representation of queue structure; two pointers FRONT
and REAR of the queue Q are known.
start
Initialize the array of 10 elements and name it as queue
Initialize other variables like rear and front in the beginning of the program
Give the choice to the users for the different operations on Queue like Insert,
Delete,Display and Exit
Function insert()
Function delete()
Check for the underflow( empty) condition of the queue If not empty ,Output the
element to be deleted from the queue Increment the value of front.
Function display()
Note: Ask the students to list the drawbacks of implementing the Queue using array.
The above program could be repeated using structures and linked lists
Output
The queue is 22 33 44
Conclusion
Declaration:--
Type queue = array [1.. n] of integer;
Var p: queue
Algorithm:--
Data Structure: CQ is the array representation of circular queue structure; two pointers
FRONT and REAR of the queue CQ are known.
start
Initialize the array of 10 elements and name it as queue
Initialize other variables like rear and front in the beginning of the program
Provide the choice to the users for the different operations on Queue like Insert,
Delete, Display and Exit
Function insert()
Check for the overflow condition of the Queue
If(FRONT=(REAR+1)%N) then Print “Queue is full”
If not overflow , increment the value of rear
REAR = (REAR+1) % N
Get the element to be inserted into the queue from the user Q[REAR] = ITEM
Function delete()
FRONT = (FRONT+1) % N
Function display()
Note: Ask the students to list the drawbacks of implementing the Queue using array.
The above program could be repeated using structures and linked lists
Output
14 15 20
Conclusion
All the operations on the circular queue are performed successfully
5. Lab Exercises:
Objective- Student should be able to develop the program for evaluation of the postfix
expression.
Theory:
Infix Expression : Any expression in the standard form like "2*3-4/5" is an Infix(Inorder)
expression.
Postfix Evaluation : In normal algebra we use the infix notation like a+b*c. The
corresponding postfix notation is abc*+. The algorithm for the conversion is as follows
Algorithm:--
Start
Declare an array of characters to take an postfix expression Get the postfix
expression from the user.
• Calculate the result with operand1 ,operand2 and the operator Push the
result into the stack.
• Repeat this step till all the characters are scanned.
After all characters are scanned, we will have only one element in the stack.
Pop the stack for final result.
End.
Example:--
Let us see how the above algorithm will be implemented using an example.
Initially the Stack is empty. Now, the first three characters scanned are 1,2 and 3, which are
operands. Thus they will be pushed into the stack in that order.
Expression
Stack
Next character scanned is "*", which is an operator. Thus, we pop the top two elements
from the stack and perform the "*" operation with the two operands. The second
operand will be the first element that is popped.
Expression
Stack
The value of the expression(2*3) that has been evaluated(6) is pushed into the
stack.
Expression
Stack
Next character scanned is "+", which is an operator. Thus, we pop the top two elements
from the stack and perform the "+" operation with the two operands. The second
operand will be the first element that is popped.
Expression
Stack
The value of the expression(1+6) that has been evaluated(7) is pushed into the
stack.
Expression
Stack
Expression
Stack
Next character scanned is "-", which is an operator. Thus, we pop the top two elements from
the stack and perform the "-" operation with the two operands. The second operand will be
the first element that is popped.
Expression
Stack
The value of the expression(7-4) that has been evaluated(3) is pushed into the
stack.
Expression
Stack
Now, since all the characters are scanned, the remaining element in the stack (there will
be only one element in the stack) will be returned.
End result :
Output
Ans= 14
Conclusion
AIM:-- Design, develop and execute a program in C to read a sparse matrix of integer
values and make a transpose of it. Use the triple <row, column, value> to represent an
element in sparse matrix.
Objective- Student should be able to develop the program for sparse matrix and its
transpose.
Theory:--
Matrices with a relatively high proportion of zero entries are called Sparse
Matrices. In mathematics a matrix contains m rows and n columns of elements as
illustrated in figure (a). in this figure, the elements are numbers. The first matrix have
5 rows and 3 columns; the second has six rows and six columns. In general, we write
mxn to designate a matrix with m rows and n columns. The total number of elements
in such a matrix is mn. If m = n, the matrix is square and takes m x n x sizeof
(datatype of matrix) bytes to store in memory. But if the matrix is sparse matrix then
storing zero value elements causes inefficient use of memory.
Therefore to use memory efficiently a sparse matrix containing only non zero elements
can be represented by using the triplet <row, column, value >
{
int col;
int row;
int value;
} term;
term A[MAX_TERMS];
Operations on Sparse Matrix:
a. Create
b. Transpose
c. Multiplication
Transpose of Sparse Matrix:
Figure (I) shows a sparse matrix represented by array ‘a’ and it’s transpose of a
matrix which is represented in figure (a). thus a[0].row contains the number of rows;
a[0].col contains number of columns; and a[0].value contains total number of nonzero
entries. Positions 1 through 8 store the triples representing the non-zero entries. The
row index is in the field row; the column index is in the field col; the value index is in the
field value. The triples are ordered by row and within rows by columns
Transpose is
[0] 3 3 4
[1] 0 0 1
[2] 2 0 6
[3] 2 1 8
[4] 1 2 5
Conclusion
Successful conversion of given matrix into the sparse matrix and its transpose.
7. Lab Exercises:
Traversing a list
Insertion of a node into a list.
Deletion of a node from the list.
Searching for an element in a list.
Merging two linked list into a larger list.
Linked list can be developed using the user defined data types. i.e Structure struct
list
{
int data; struct
list * next;
};
Node Structure:
Fig: Insertion
Fig: Deletion
Algorithm:--
Start
Initialize the structure of the node
Initialize other variables like rear and front in the beginning of the program
Give the choice to the users for the different operations on Queue like Insert, Delete,
Display and Exit
INSERT-Singly List
new = GETNODE(NODE)
If (new = NULL) then
a. Print “Memory is insufficient : Insertion is not possible” b.
Exit.
Else
• Ask the user where to insert the item at beginning, at end or besides some node
and get the data for new node as x
• If the node is to be inserted besides some node,find the node. Lets call it as key
ptr= start
While(ptr.data<>key)and(ptr.next<> NULL) do
o ptr = ptr1.next
End while
End if
• If the node is to be inserted at the beginning
new.next = start
new.data= x
start= new
• If the node is to be inserted at the end
ptr= start
While (ptr.next<> NULL) do
ptr = ptr1.next
• End while
• If (ptr.next = NULL) then
new.next = NULL
new.data= x
ptr.link= new
• End if
DELETE-Singly List
Ask the user which item to delete .Find the item. The item may be at beginning, at end
or between some nodes
DISPLAY-Singly List
ptr=start
While (ptr.next<> NULL) do
ptr = ptr1.next
print ptr.data
Endwhile
Output:
2. Insert in Beginning
3. Insert in Middle
5. Count
6. Display
7. Quit
Enter more(y/n): y
Enter more(y/n): n
2. Insert in Beginning
3. Insert in Middle
4. Remove from the List
5. Count
6. Display
7. Quit
2. Insert in Beginning
3. Insert in Middle
5. Count
6. Display
7. Quit
2. Insert in Beginning
3. Insert in Middle
5. Count
6. Display
7. Quit
2. Insert in Beginning
3. Insert in Middle
5. Count
6. Display
7. Quit
2. Insert in Beginning
3. Insert in Middle
5. Count
6. Display
7. Quit
11 14 13
Conclusion
AIM - Design, develop and execute a program in C to implement doubly linked list
where each node consist of integers. The program should support following functions.
Objective- Student should be able to develop the program for creation of doubly linked
list and operations on it
Theory :--
In computer science, a doubly-linked list is a linked data structure that consists
of a set of data records, each having two special link fields that contain references to
the previous and to the next record in the sequence.
{
int data;
struct list * next, * prev;
};
Node Structure:
3
Operations on Linked List:--
Output
2. Insert in Beginning
3. Insert in Middle
5. Count
6. Display
7. Quit
Enter more(y/n): y
Enter more(y/n): n
2. Insert in Beginning
3. Insert in Middle
5. Count
6. Display
7. Quit
2. Insert in Beginning
3. Insert in Middle
5. Count
6. Display
7. Quit
2. Insert in Beginning
3. Insert in Middle
5. Count
6. Display
7. Quit
2. Insert in Beginning
3. Insert in Middle
5. Count
6. Display
7. Quit
2. Insert in Beginning
3. Insert in Middle
5. Count
6. Display
7. Quit
11 14 13
Conclusion
AIM:-- Using array representation for a polynomial, design, develop and execute a
program in C to add two polynomials and then print the resulting polynomial.
Objective- Student should be able to develop the program for operations on two
polynomials
Theory:--
Assume that there are two polynomials A and B. Te polynomial storing the
addition result is stored in one large array C and i, j and k represents the pointers to
the polynomials and arrays respectively. Following algorithm carry out the addition of
• Else
if exponent position i > exponent position j in B then
Copy coeff at i’th position from A to coeff at k’th position in C
Copy the exponent pointed by i into exponent field at position k in C
Increment i, k
else
Copy coeff at j’th position from B to coeff at k’th position in C
Copy the exponent pointed by j into exponent field at position k in C
Increment j, k
While i < n1
(a) Copy coeff at i'th position of the coeff field into coeff at k’th position in C
(b) Copy exponent pointed by i into exponent field at k’th position in C
(c) i and k are incremented by to point to the next position in arrays A and C
While j < n2
(d) Copy coeff at j'th position of the coeff field into coeff at k’th position in C
(e) Copy exponent pointed by j into exponent field at k’th position in C
(f) j and k are incremented by to point to the next position in arrays B and C
Continue??? (Y/N) :Y
Continue??? (Y/N) :
First polynomial is ::
5x^3 + 3x^2
Continue??? (Y/N) :
Second polynomial is ::
7x^3
Conclusion
Thus the addition of two polynomial equations has been implemented successfully in C
program.
10. Lab Exercises:
AIM:-- Write a program in C to construct binary tree and binary tree traversal
Objective- Student should be able to develop the program for creation of binary search
tree and traversal operations on it
Theory:--
A binary tree is made of nodes, where each node contains a "left" pointer, a
"right" pointer, and a data element. The "root" pointer points to the topmost node in the
tree. The left and right pointers recursively point to smaller "subtrees" on either side. A
null pointer represents a binary tree with no elements -- the empty tree. The formal
recursive definition is: a binary tree is either empty (represented by a null pointer), or
is made of a single node, where the left and right pointers (recursive definition ahead)
each point to a binary tree.
Tree Structure:
Else
If (root.right=NULL) then root.right=s
Else insert(root.right,s)
Algorithm preorder
Let T be an ordered binary tree with root R
If T has only R then R is the preorder traversal
Else
• Let T1, T2 be the left and right subtrees at R
• Visit R
• Traverse T1 in preorder
• Traverse T2 in preorder
Algorithm postorder
Let T be an ordered binary tree with root R
If T has only R then R is the inorder traversal
Else
• Let T1, T2 be the left and right subtrees at R
• Traverse T1 in inorder
• Visit R
• Traverse T2 in inorder
Algorithm inorder
Let T be an ordered binary tree with root R
If T has only R then R is the postorder traversal
Else
• Let T1, T2 be the left and right subtrees at R
• Traverse T1 in postorder
• Traverse T2 in postorder
• Visit R
Output
10 40 30
10 30 40
Conclusion
Thus the expression tree has been implemented successfully in C program.
11. Lab Exercises:
AIM:-- Design, develop and execute a program in C to create a max heap of integers by
accepting one element at a time and by inserting it immediately in to heap. Use the
array representation of heap. Display the array at the end of insertion phase.
Objective- Student should be able to develop the program for creation of max heap tree
Theory:--
There are two type of heap MAX heap and MIN heap. MAX heap is a complete
binary tree in which root node value is greater than its left child value and right child
value. Similarly MIN heap is a complete binary tree in which root node value is less
than its left child value and right child value.
Operations on heap:
Suppose H is a heap with N elements, and suppose an ITEM of information is given, and
then ITEM can be inserted into heap H as follows:
• First adjoin ITEM at the end of H so that H is still a complete tree, but not
necessarily a heap.
• Then let ITEM rise to its “appropriate place” in H so that H is finally heap.
8 4
6 5
After Insertion of node 7 above tree will be:
8 7
6 5 4
Algorithm:
Return
b) Deletion in MAX heap
A heap H with N elements is stored in array TREE. This procedure assigns the
root TREE[1] of H to variable ITEM and then reheaps the remaining elements. The
variable LAST saves the value of the original last node of H. The pointers PTR, LEFT,
RIGHT gives the location of LAST and its left and right children as LAST sinks in the
tree.
8 4
6 5
6 4
Algorithm:
Output
97 88 95 66 55 95 48 66 35 48 55 62 77 25 38 18 40 30 26 24
97 88 95 66 70 95 48 66 35 55 55 62 77 25 38 18 40 30 26 24 48
95 88 95 66 70 77 48 66 35 55 55 62 48 25 38 18 40 30 26 24
Conclusion
Teacher should oral exams of the students with full preparation. Normally, the
objective questions with guess are to be avoided. To make it meaningful, the questions
should be such that depth of the students in the subject is tested Oral examinations
are to be conducted in co-cordial environment amongst the teachers taking the
examination. Teachers taking such examinations should not have ill thoughts about
each other and courtesies should be offered to each other in case of difference of
opinion, which should be critically suppressed in front of the students.
What is the difference between linear and non linear data structure
Definition of Stack
Definition of Queue
Types of Queue
Application of Queue.
Disadvantage of queue
Define BST
Basic honesty in the evaluation and marking system is absolutely essential and
in the process impartial nature of the evaluator is required in the examination system
to become popular amongst the students. It is a wrong approach or concept to award
the students by way of easy marking to get cheap popularity among the students to
which they do not deserve. It is a primary responsibility of the teacher that right
students who are really putting up lot of hard work with right kind of intelligence are
correctly awarded.
The marking patterns should be justifiable to the students without any ambiguity and
teacher should see that `students are faced with unjust circumstances.
The assessment is done according to the directives of the Principal/ Vice-Principal/ Dean
Academics.