DS Lab Manual (Jntu)

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

Jawaharlal Nehru Engineering College

Laboratory Manual

DATA STRUCTURES USING C

For

Second year Students (CSE)

16, Nov 2005 – Rev 00 – Comp Sc – ISO 9000 Tech Document


Author JNEC, Aurangabad
FOREWORD

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.

Good Luck for your Enjoyable Laboratory Sessions

Dr. V .B. Musande Mr. Y. R. Tayade

HOD,CSE Asst. Prof., CSE Dept.


MGM’s

Jawaharlal Nehru Engineering College, Aurangabad

Department of Computer Science and Engineering

Vision of CSE Department:


To develop computer engineers with necessary analytical ability and human values who can
creatively design, implement a wide spectrum of computer systems for welfare of the society.

Mission of the CSE Department:


I. Preparing graduates to work on multidisciplinary platforms associated with their
professional position both independently and in a team environment.

II. Preparing graduates for higher education and research in computer science and engineering
enabling them to develop systems for society development.

Programme Educational Objectives:

Graduates will be able to


I. To analyze, design and provide optimal solution for Computer Science & Engineering and
multidisciplinary problems.
II. To pursue higher studies and research by applying knowledge of mathematics and
fundamentals of computer science.
III. To exhibit professionalism, communication skills and adapt to current trends by engaging
in lifelong learning.
Programme Outcomes (POs):

Engineering Graduates will be able to:

1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals, and an engineering specialization to the solution of complex engineering
problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.

3. Design/development of solutions: Design solutions for complex engineering problems and


design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.

4. Conduct investigations of complex problems: Use research-based knowledge and research


methods including design of experiments, analysis and interpretation of data, and synthesis of
the information to provide valid conclusions.

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.

7. Environment and sustainability: Understand the impact of the professional engineering


solutions in societal and environmental contexts, and demonstrate the knowledge of, and need
for sustainable development.

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.

10. Communication: Communicate effectively on complex engineering activities with the


engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.

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

1. Prerequisite for Data Structures implementation

2. Write a C program for implementation of stack using array

3. Write a C program for implementation of queue using array

4. Write a C program for implementation of circular queue using array

5. Design, develop and execute a program in C to evaluate a valid postfix expression


using stack. Assume that the postfix expression is read as a single line consisting of
non-negative single digit operands and binary arithmetic operators. The operators
are +(add), -(subtract), *(multiply), /(divide)

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.

4. Do not change the terminal on which you are working.


5. All the students are expected to get at least the algorithm of the program/concept to be
implement.

6. Strictly follow the instructions given by the teacher/Lab Instructor.

Instruction for Laboratory Teachers

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

Exercise No 1: ( 2 Hours) – 1 Practical

AIM - Introduction to concepts of Array and Structures in C


Objective- Student should be able to develop the programs using arrays and structures.

• Write a Program to read in one dimensional , calculate the sum and display it

 Initialize the array of 10 elements


 Read the elements using a loop
 Add all the elements
 Display the contents of the array and the sum
• Write a Program for addition of two matrices using two dimensional array
• Write a Program to demonstrate the concept of structure and array of structures

• Write a Program to demonstrate passing the structure and array of structures to


a function.

Output
Enter number of students 3
Enter rollno , name and percentage of 3 students

1 Ajay 70
2 Rahul 80
3 Yogesh 90

You entered the student record

1 Ajay 70
2 Rahul 80
3 Yogesh 90

Conclusion

Studied and successfully demonstrated the use arrays and structures.


2. Lab Exercises:

Exercise No 2: ( 2 Hours) – 1 Practical

AIM – Write a C program for implementation of stack using array


Objective- Student should be able to develop a program from the logic of stack
Theory:--
A stack is an ordered collection of homogeneous data element where the
insertion and deletion operation take place at one end only. An n-element array can be
used to implement a stack with capacity n. The stack top is indicated by the index top
and it points to the element that was entered into the stack.

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”

 If not overflow , increment the value of top


 Get the element to be inserted onto the stack from the user
 Assign it as the topmost value ,stack[top]

Function pop()

 Check for the underflow( empty) condition of the stack


if TOP<0 Print “Stack is Empty”
 If not empty ,Output the element to be deleted from the stack  Decrement the
value of top.

Function display()

 Display all the elements of the stack

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

Enter the operation to be performed: 1) push 2) pop 3) display 4) exit

Enter the number to be Add: 11

Enter the operation to be performed: 1) push 2) pop 3) display 4) exit

Enter the number to be Add: 22

Enter the operation to be performed: 1) push 2) pop 3) display 4) exit

Enter the number to be Add: 33


Enter the operation to be performed: 1) push 2) pop 3) display 4) exit

Enter the number to be Add: 44

Enter the operation to be performed: 1) push 2) pop 3) display 4) exit

Enter the number to be Add: 44

Enter the operation to be performed: 1) push 2) pop 3) display 4) exit

The stack is 11 22 33

Enter the operation to be performed: 1) push 2) pop 3) display 4) exit

Conclusion

All the stack operations are successfully performed using the switch case
3. Lab Exercises:

Exercise No 3: ( 2 Hours) – 1 Practical

AIM – Write a C program for implementation of queue using array


Objective- Student should be able to develop the program for queue using arrays.
Theory:--
A Queue is an ordered collection of homogeneous data elements where the insertion
and deletion operation take place at two extreme ends.

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

 If the choice= insert operation call the function qinsert()


 If the choice= delete operation call the function qdelete()
 If the choice= display operation call the function qdisplay()
 If the choice= exit then exit from the program  end

Function insert()

 Check for the overflow condition of the Queue


If(REAR = N) then Print “Queue is full”
 If not overflow , increment the value of rear
REAR = REAR+1
 Get the element to be inserted into the queue from the user Q[REAR] = ITEM 
Assign it as the last value ,queue[rear]

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()

 Display the elements of the queue

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

Enter the operation to be performed: 1)Enqueue 2)Dequeue 3)Display 4)Exit

Enter the number to be added 11

Enter the operation to be performed: 1)Enqueue 2)Dequeue 3)Display 4)Exit

Enter the number to be added 22

Enter the operation to be performed: 1)Enqueue 2)Dequeue 3)Display 4)Exit

Enter the number to be added 33

Enter the operation to be performed: 1)Enqueue 2)Dequeue 3)Display 4)Exit

Enter the number to be added 44


Enter the operation to be performed: 1)Enqueue 2)Dequeue 3)Display 4)Exit

The number to be deleted is 11

Enter the operation to be performed: 1)Enqueue 2)Dequeue 3)Display 4)Exit

The queue is 22 33 44

Conclusion

All the queue operations are successfully performed


4. Lab Exercises:

Exercise No 4: ( 2 Hours) – 1 Practical

AIM – Write a C program for implementation of circular queue using array


Objective- Student should be able to develop the program for circular queue
Theory:--
A Queue is an ordered collection of homogeneous data elements where the
insertion and deletion operation take place at two extreme ends. And it supports
reinsertion as well as re-deletion at same location.

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

 If the choice= insert operation call the function cqinsert()


 If the choice= delete operation call the function cqdelete()
 If the choice= display operation call the function cqdisplay()
 If the choice= exit then exit from the program  end

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

 Assign it as the last value ,queue[rear]

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.

FRONT = (FRONT+1) % N

Function display()

 Display the elements of the queue

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

1) Insert 2) Delete 3) Display

Enter choice for circular queue 1

Enter any item 14

1) Insert 2) Delete 3) Display

Enter choice for circular queue 1

Enter any item 15

1) Insert 2) Delete 3) Display

Enter choice for circular queue 1

Enter any item 20

1) Insert 2) Delete 3) Display


Enter choice for circular queue 3

14 15 20

Conclusion
All the operations on the circular queue are performed successfully
5. Lab Exercises:

Exercise No 5: ( 2 Hours) – 1 Practical

AIM:-- Design, develop and execute a program in C to evaluate a valid postfix


expression using stack. Assume that the postfix expression is read as a single line
consisting of non-negative single digit operands and binary arithmetic operators. The
operators are +(add), -(subtract), *(multiply), /(divide)

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 Expression : The Postfix(Postorder) form of the above expression is "23*45/-".

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.

 Scan the Postfix string from left to right.


 Initialize an empty stack.
 If the scanned character is an operand, add it to the stack.
 If the scanned character is an operator, there will be at least two operands in the
stack.
• If the scanned character is an Operator, then we Pop the stack twice to get
our perand1 and operand2.

• 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.

Postfix String : 123*+4-

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

Next character scanned is "4", which is added to 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(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 :

• Postfix String : 123*+4-


• Result : 3

Output

Enter the Infix Expression: 2+3*4

The Postfix Expression is: 234*+


Do you want to evaluate the Result of Postfix Expression?(Y/N):

Ans= 14
Conclusion

Successful evaluation of the postfix expression is done.


6. Lab Exercises:

Exercise No 6: ( 2 Hours) – 1 Practical

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 >

Figure (a) Two matrices


Structure used for Sparse Matrix:
#define MAX_TERMS 101 /* maximum number of terms +1*/
typedef struct

{
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

row col value row col value


a[0] 6 6 8 b[0] 6 6 8
[1] 0 0 15 [1] 0 0 15
[2] 0 3 22 Transpose [2] 0 4 91

[3] 0 5 -15 [3] 1 1 11


[4] 1 1 11 [4] 2 1 3
[5] 1 2 3 [5] 2 5 28
[6] 2 3 -6 [6] 3 0 22
[7] 4 0 91 [7] 3 2 -6
[8] 5 2 28 [8] 5 0 -15
Figure (I) Sparse matrix and it’s transpose stored as a triples
Algorithm:

 for each row i take element <i, j, value> and store


it in element <j, i, value> of the transpose.

 Difficulty: where to put <j, i, value>?


(0, 0, 15) ====> (0, 0, 15)
(0, 3, 22) ====> (3, 0, 22)

(0, 5, -15) ====> (5, 0, -15)


(1, 1, 11) ====> (1, 1, 11)
Move elements down very often.

 For all elements in column j, place element <i,


j, value> in element <j, i, value>

Function for transpose of sparse matrix:

void transpose (term a[], term b[])


/* b is set to the transpose of a */
{
int n, i, j, currentb;
n = a[0].value; /* total number of elements */
b[0].row = a[0].col; /* rows in b = columns in a */
b[0].col = a[0].row; /*columns in b = rows in a */
b[0].value = n;

if (n > 0) {/*non zero matrix */


currentb = 1;

for (i = 0; i < a[0].col; i++)


/* transpose by columns in a */
for( j = 1; j <= n; j++)
/* find elements from the current column */
if (a[j].col == i) {

/* element is in current column, add it to b */


b[currentb].row = a[j].col;
b[currentb].col = a[j].row;
b[currentb].value = a[j].value;
}
}
}
Output
Enter the matrix elements of size 3 by 3
106008050
Given matrix is
1 0 6
0 0 8
0 5 0
Sparse Matrix is
[0] 3 3 4
[1] 0 0 1
[2] 0 2 6
[3] 1 2 8
[4] 2 1 5

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:

Exercise No 7: (2 Hours) – 2 Practical

AIM :-- Program for implementing Singly Linked List


Objective- Student should be able to develop the program for linked list creation and
operations on it
Theory :--
Linked list is a chain of structures or records called nodes. Each node has at
least two members, one of which points to the next item or node in the list! These are
defined as Single Linked Lists because they only point to the next item, and not the
previous. Those that do point to both are called Doubly Linked Lists. Unlike arrays
there is no upper limit on the amount of memory reserved. A singly linked list is the
simplest of linked lists. Each node of a singly linked list has data elements and a single
link (pointer) that points to the next node of the list (or) NULL if it is the last node of
the list. Addition/ Deletion of a node to the singly linked list involve the creation/
deletion of the node and adjusting the node pointers accordingly. Some operations
include

 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:

Operations on Linked List:

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

 If the choice = insert operation call the function insert()


 If the choice = delete operation call the function delete()
 If the choice = display operation call the function display()
 If the choice = exit then exit from the program
 end

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

 If (ptr.next = NULL) then o Print “Key is not


available in the list” o Exit

 Else o new.next = ptr.next o new.data= x o


ptr.next= new

 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

 If the node is to be deleted is besides some node. Lets call it as key


• ptr= start
• While(ptr.data<>key)and(ptr.next<> NULL) do
 ptr = ptr1.NEXT
• Endwhile
• If (ptr.next = NULL) then
 Print “Key is not available in the list”
 Exit
Else
 prevnode.next = ptr.next
 free(ptr)
Endif
 If the node to be deleted is at the beginning
• ptr = start
• temp=ptr.next
• start=temp
• free(ptr)
 If the node to be deleted is at the end
• ptr= start
• While (ptr.next<> NULL) do
 ptr = ptr1.next
• Endwhile
• If (ptr.next = NULL) then
 prevnode.next = NULL
free(ptr)
 Endif

DISPLAY-Singly List
 ptr=start
 While (ptr.next<> NULL) do
ptr = ptr1.next
print ptr.data
 Endwhile

Output:

Singly Linked List:

1. Create or Append List

2. Insert in Beginning

3. Insert in Middle

4. Remove from the List

5. Count

6. Display

7. Quit

Enter your Choice: 1

Enter any number: 12

Enter more(y/n): y

Enter any number: 13

Enter more(y/n): n

Singly Linked List:

1. Create or Append List

2. Insert in Beginning

3. Insert in Middle
4. Remove from the List

5. Count

6. Display

7. Quit

Enter your Choice: 2

Enter the Data to be inserted at Beginning: 11

Singly Linked List:

1. Create or Append List

2. Insert in Beginning

3. Insert in Middle

4. Remove from the List

5. Count

6. Display

7. Quit

Enter your Choice: 3

Enter the Position to be Inserted: 2

Enter the Data: 14

Singly Linked List:

1. Create or Append List

2. Insert in Beginning

3. Insert in Middle

4. Remove from the List

5. Count

6. Display

7. Quit

Enter your Choice: 4

Enter the Data to be Deleted: 12


Singly Linked List:

1. Create or Append List

2. Insert in Beginning

3. Insert in Middle

4. Remove from the List

5. Count

6. Display

7. Quit

Enter your Choice: 5

The Number of Nodes: 3 28

Singly Linked List:

1. Create or Append List

2. Insert in Beginning

3. Insert in Middle

4. Remove from the List

5. Count

6. Display

7. Quit

Enter your Choice: 6

11 14 13

Conclusion

All the Singly Linked List operations are performed.


8. Lab Exercises:

Exercise No 8: (2 Hours) – 2 Practical

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.

 Create a doubly linked list


 Insert a new node
 Delete a node if it is found, otherwise display appropriate message  Display the
nodes of doubly linked list

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.

Structure Declaration: struct


list

{
int data;
struct list * next, * prev;
};

Node Structure:

prev data next

3
Operations on Linked List:--

a) Insertion in doubly linked list:

b) Deletion in doubly linked list:

Output

Doubly Linked List:

1. Create or Append List

2. Insert in Beginning

3. Insert in Middle

4. Remove from the List

5. Count
6. Display

7. Quit

Enter your Choice: 1

Enter any number: 12

Enter more(y/n): y

Enter any number: 13

Enter more(y/n): n

Doubly Linked List:

1. Create or Append List

2. Insert in Beginning

3. Insert in Middle

4. Remove from the List

5. Count

6. Display

7. Quit

Enter your Choice: 2

Enter the Data to be inserted at Beginning: 11

Doubly Linked List:

1. Create or Append List

2. Insert in Beginning

3. Insert in Middle

4. Remove from the List

5. Count

6. Display

7. Quit

Enter your Choice: 3

Enter the Position to be Inserted: 2


Enter the Data: 14

Doubly Linked List:

1. Create or Append List

2. Insert in Beginning

3. Insert in Middle

4. Remove from the List

5. Count

6. Display

7. Quit

Enter your Choice: 4

Enter the Data to be Deleted: 12

Doubly Linked List:

1. Create or Append List

2. Insert in Beginning

3. Insert in Middle

4. Remove from the List

5. Count

6. Display

7. Quit

Enter your Choice: 5

The Number of Nodes: 3 28

Doubly Linked List:

1. Create or Append List

2. Insert in Beginning

3. Insert in Middle

4. Remove from the List

5. Count
6. Display

7. Quit

Enter your Choice: 6

11 14 13

Conclusion

All the Doubly Linked List operations are performed.


9. Lab Exercises:

Exercise No 9: ( 2 Hours) – 1 Practical

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

these two polynomials

Consider two polynomials

A(X) = -7X5 + 4X2 + X

B(X) = 3X4 + 5X3 + 6X2 + 10X - 14

A(X) + B(X) = -7X5 + 3X4 + 5X3 + 10X2 + 11X - 14

Polynomial Addition using array representation


Algorithm:

 Set i to point to first term in A


 Set j to point to first term in B
 Set k to point to first term in C
 Read n1=no. of terms in A and n2= no. of terms in B  While i < n1 and j < n2

• If exponent at i’th position of A = exponent at j’th position of B then C at


C at k’th position = coeff of A at i’th position + coeff of B at j’th position
Increment i, j, k to point to next position in A, B, C

• 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

 Display C [complete array as the addition of two polynomials A


and B]
 Exit
Output

Enter the first polynomial::

Enter the coefficient :5

Enter the power :3

Continue??? (Y/N) :Y

Enter the coefficient :3

Enter the power :2

Continue??? (Y/N) :

First polynomial is ::

5x^3 + 3x^2

Enter the second polynomial::

Enter the coefficient :7

Enter the power :3

Continue??? (Y/N) :

Second polynomial is ::

7x^3

Addition of the two polynomials::12x^3 + 3x^2

Conclusion

Thus the addition of two polynomial equations has been implemented successfully in C
program.
10. Lab Exercises:

Exercise No 10: (2 Hours) – 2 Practical

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:

Structure declaration of a node: struct


node
{
int data;
struct node* left;
struct node* right;
}

Algorithm for Creation of Binary tree

 Initialize root= NULL


 Create node as
• s= new node
• s.data=x
• s.left=NULL
• s.right=NULL

 If root =NULL then root=s i.e the node is first node


 If leaf nodes are to be inserted then call function insert(root,s)

Insert function( root, s)


 If root.data<s.data then
If (root.left=NULL) then root.left=s
Else insert(root.left,s)

 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

Enter the root node:30


1.Insertion 2.Preorder 3.Inorder 4.Postorder 5.Quit

Enter your choice 1

Enter the element to insert 10

10 is inserted at the left

1.Insertion 2.Preorder 3.Inorder 4.Postorder 5.Quit

Enter your choice 1

Enter the element to insert 40

40 is inserted at the right

1.Insertion 2.Preorder 3.Inorder 4.Postorder 5.Quit

Enter your choice 2

The preorder elements are 30 10 40

1.Insertion 2.Preorder 3.Inorder 4.Postorder 5.Quit

Enter your choice 4

The postorder elements are

10 40 30

1.Insertion 2.Preorder 3.Inorder 4.Postorder 5.Quit

Enter your choice 3

The inorder elements are

10 30 40

1.Insertion 2.Preorder 3.Inorder 4.Postorder 5.Quit

Enter your choice 5

Conclusion
Thus the expression tree has been implemented successfully in C program.
11. Lab Exercises:

Exercise No 11: ( 2 Hours) – 1 Practical

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:

a) Insertion in MAX heap


b) Deletion in MAX heap

a) Insertion in MAX 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:

INSHEAP (TREE, N, ITEM)


A heap H with N elements is stored in array TREE, and an ITEM of information is
given. This procedure inserts ITEM as a new element of H. PTR gives the location of
ITEM, and PAR denotes the location of parent of ITEM.

 [Add new node H and initialize PTR]

 Set N := N+1 and PTR := N.

 [Find location to insert ITEM.]


 Repeat Step 3 to 6 while PTR > 1
 Set PAR := (PTR/2). Location of parent node
 If ITEM <= TREE[PAR], then:
 Set TREE[PTR] := ITEM, and Return.
[End of if structure]
 Set TREE[PTR] := TREE[PAR]. [Moves the node down.]
 Set PTR := PAR. [Updates PTR.]
[End of step two loop]
 [Assign ITEM as the root of H.]

 Set TREE[1] := ITEM

 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

After Deletion of 9 above tree will be

6 4

Algorithm:

DELHEAP (TREE, N, TIEM)


 Set ITEM := TREE[1]. [Removes the root of H.]
 Set LAST := TREE[N] and N := N-1. [Removes the last node of H.]
 Set PTR := 1, LEFT := 2 and RIGHT := 3 [Initialize pointers]
 Repeat step 5 to 7 while RIGHT <=N:
 If LAST >= TREE[LEFT] and LAST >= TREE[LIGHT], then
Set TREE[PTR] := LAST and Return
[End of If structure.]
 If TREE[RIGHT] <= TREE[LEFT] then:
Set TREE[PTR] := TREE[LEFT] and PR := LEFT Else:

Set TREE[PTR] := TREE[RIGHT] and PR := RIGHT


[End of If structure.]
 Set LEFT := 2*PTR and RIGHT := LEFT + 1
[End of step 4 loop.]
If LEFT = N and if LASST < TREE[LEFT], then: Set PTR := LEFT
 Set TREE[PTR] := LAST
 Return

Output

Array representation of heap:

97 88 95 66 55 95 48 66 35 48 55 62 77 25 38 18 40 30 26 24

Enter item to be inserted into heap 70

Array representation of heap:

97 88 95 66 70 95 48 66 35 55 55 62 77 25 38 18 40 30 26 24 48

After deletion of root

95 88 95 66 70 77 48 66 35 55 55 62 48 25 38 18 40 30 26 24

Conclusion

Successfully created the max-heap tree from given elements.


Conduction of Viva-Voce Examinations:

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.

 Define data structure and it’s types

 What is the difference between linear and non linear data structure

 How to declare the pointer.

 How to declare the structure.

 What is mean by array

 How polynomial is represented using array

 How polynomial addition takes place by using array

 How polynomial addition takes place by using structure

 What is the difference between structure and array

 What is the mean by dynamic array

 What is the difference between static variable and dynamic variable

 What is the difference between static array and dynamic array

 How the memory is utilized in structure and union

 Define two dimensional array

 Define multi dimensional array

 What is performance analysis


 Define algorithm

 What is the criteria for algorithm specification


 Define space and time complexity

 What is performance measurement

 Definition of Stack

 What are the different operations on the stack.

 Applications of the stack.

 What is dynamic stack

 Define PUSH operation

 Define POP operation.

 What is the drawback of stack

 Define multiple stack

 Definition of Queue

 How to use queue in program

 Explain the operations on the Queue

 Types of Queue

 Application of Queue.

 Disadvantage of queue

 Define circular queue

 What is dynamic circular queue

 What is the difference between stack and queue

 Difference between simple queue and circular queue

 How to insert node in the linked list.

 Define the Header.


 How to delete the node at the last position in the linked list.

 How polynomial is represented using singly linked list

 How polynomial is represented using circular linked list

 How polynomial is represented using doubly linked list

 How polynomial addition takes place by using linked list

 Define BINARY tree

 Define BST

 What is the difference between binary tree and BST

 What is AVL tree

 Define Red-Black tree

 What is selection tree

 Difference between winner tree and looser tree

 Define Splay tree

 What is heap and it’s types

 Difference between Min heap and Max heap

 Tree representation methods

 How to represent binary tree in memory location

 Advantages and Disadvantages of binary tree.

 Operations on binary tree.

 Traversals of the binary tree


Evaluation and marking system:

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.

You might also like