0% found this document useful (0 votes)
27 views

DSA LAB MANUAL (2)

Uploaded by

tahair0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

DSA LAB MANUAL (2)

Uploaded by

tahair0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 38

LABORATORY MANUAL

DATA STRUCTURES LABORATORY (21CS32)


VISVESVARAYA TECHNOLOGICAL UNIVERSITY

JNANA SANGAMA, BELGAVI-590018, KARNATAKA

Prepared By:
Mrs.Shivalingamma.
Assistant Professor
Dept. of CSE

Department of Computer Science and Engineering

Shetty Institute Of Technology, Kalaburagi


(An ISO 9001:2015 Certified Institution)
(Affiliated to Visvesvaraya Technological University Belgaum and approved by AICTE, New
Delhi)

Dept. Of C.S.E. SIT-2022 Page 1


Data structures laboratory 21CS32

DATA STRUCTURES AND APPLICATIONS


Course Code: 21CS32 CIE Marks 50
Teaching Hours/Week (L:T:P: S) 3:0:2:0 SEE Marks 50
Total Hours of Pedagogy 40 T + 20 P Total Marks 100
Credits 04 Exam Hours 03
Course Objectives:

CLO 1. Explain the fundamentals of data structures and their applications essential for implementing solutions
to problems.
CLO 2. Illustrate representation of data structures: Stack, Queues, Linked Lists, Trees and Graphs.
CLO 3. Design and Develop Solutions to problems using Arrays, Structures, Stack, Queues, Linked Lists. CLO
4. Explore usage of Trees and Graph for application development.
CLO 5. Apply the Hashing techniques in mapping key value pairs.
Teaching-Learning Process (General Instructions)

These are sample Strategies, which teachers can use to accelerate the attainment of the various course outcomes.
1. Lecturer method (L) need not to be only traditional lecture method, but alternative effective
teaching methods could be adopted to attain the outcomes.
2. Use of Video/Animation to explain functioning of various concepts.
3. Encourage collaborative (Group Learning) Learning in the class.
4. Ask at least three HOT (Higher order Thinking) questions in the class, which promotes critical
thinking.
5. Adopt Problem Based Learning (PBL), which fosters students’ Analytical skills, develop design
thinking skills such as the ability to design, evaluate, generalize, and analyze information rather than
simply recall it.
6. Introduce Topics in manifold representations.
7. Show the different ways to solve the same problem and encourage the students to come up with their
own creative ways to solve them.
8. Discuss how every concept can be applied to the real world - and when that's possible, it helps
improve the students' understanding.

Module-1
Introduction: Data Structures, Classifications (Primitive & Non-Primitive), Data structure operations
(Traversing, inserting, deleting, searching, and sorting). Review of Arrays. Structures: Array of structures Self-
Referential Structures.
Dynamic Memory Allocation Functions. Representation of Linear Arrays in Memory, dynamically allocated
arrays and Multidimensional Arrays.
Demonstration of representation of Polynomials and Sparse Matrices with arrays.

Textbook 1: Chapter 1: 1.2, Chapter 2: 2.2 - 2.7, Text Textbook 2: Chapter 1: 1.1 - 1.4,
Chapter 3: 3.1 - 3.3, 3.5, 3.7, Chapter 4: 4.1 - 4.9, 4.14 Textbook 3: Chapter 1: 1.3
Laboratory Component:

1. Design, Develop and Implement a menu driven Program in C for the following Array Operations
a. Creating an Array of N Integer Elements
b. Display of Array Elements with Suitable Headings
c. Exit.
Support the program with functions for each of the above operations.

2. Design, Develop and Implement a menu driven Program in C for the following Array operations
a. Inserting an Element (ELEM) at a given valid Position (POS)
b. Deleting an Element at a given valid Position POS)
c. Display of Array Elements

Dept Of C.S.E.SIT Page 3


Data structures laboratory 21CS32

d. Exit.
Support the program with functions for each of the above operations.

Teaching-Learning Process Problem based learning (Implementation of different programs to


illustrate application of arrays and structures.
https://www.youtube.com/watch?v=3Xo6P_V-qns&t=201s

https://ds2-iiith.vlabs.ac.in/exp/selection-sort/index.html
https://ds1-iiith.vlabs.ac.in/data-structures- 1/List%20of
%20experiments.html

Module-2
Stacks: Definition, Stack Operations, Array Representation of Stacks, Stacks using Dynamic
Arrays. Different representation of expression. Stack Applications: Infix to postfix conversion, Infix to
prefix conversion, evaluation of postfix expression, recursion.

Queues: Definition, Array Representation of Queues, Queue Operations, Circular Queues, Queues and Circular
queues using Dynamic arrays, Dequeues, Priority Queues.

Textbook 1: Chapter 3: 3.1 -3.4, 3.6 Textbook 2: Chapter 6: 6.1 -6.4, 6.5, 6.7-6.13
Laboratory Component:

1. Design, Develop and Implement a menu driven Program in C for the following operations on
STACK of Integers (Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate Overflow and Underflow situations on Stack
d. Display the status of Stack
e. Exit
Support the program with appropriate functions for each of the above operations
2. Design, Develop and Implement a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^
b. Solving Tower of Hanoi problem with n disks

Teaching-Learning Process Active Learning, Problem based learning


https://nptel.ac.in/courses/106/102/106102064/ https://ds1-
iiith.vlabs.ac.in/exp/stacks-queues/index.html

Module-3
Linked Lists: Definition, classification of linked lists. Representation of different types of linked lists in
Memory, Traversing, Insertion, Deletion, Searching, Sorting, and Concatenation Operations on Singly linked
list, Doubly Linked lists, Circular linked lists, and header linked lists. Linked Stacks and Queues.
Applications of Linked lists – Polynomials, Sparse matrix representation. Programming Examples.

Textbook 1: Chapter 4: 4.1 – 4.4, 4.5.2, 4.7, 4.8, Textbook 2: Chapter 5: 5.1 – 5.9
Laboratory Component:

1. Singly Linked List (SLL) of Integer Data


a. Create a SLL stack of N integer.
b. Display of SLL
c. Linear search. Create a SLL queue of N Students Data Concatenation of two SLL of integers.
2. Design, Develop and Implement a menu driven Program in C for the following operationson Doubly
Linked List (DLL) of Professor Data with the fields: ID, Name, Branch, Area of specialization
a. Create a DLL stack of N Professor’s Data.

Dept Of C.S.E.SIT Page 4


Data structures laboratory 21CS32

b. Create a DLL queue of N Professor’s Data


Display the status of DLL and count the number of nodes in it.

Teaching-Learning Process MOOC, Active Learning, Problem solving based on linked lists.
https://nptel.ac.in/courses/106/102/106102064/
https://ds1-iiith.vlabs.ac.in/exp/linked-list/basics/overview.html
https://ds1-iiith.vlabs.ac.in/List%20of%20experiments.html
https://ds1-iiith.vlabs.ac.in/exp/linked-list/basics/overview.html
https://ds1-iiith.vlabs.ac.in/List%20of%20experiments.html

Module-4
Trees 1: Terminologies, Binary Trees, Properties of Binary trees, Array and linked Representation of
Binary Trees, Binary Tree Traversals - Inorder, postorder, preorder;
Threaded binary trees, Binary Search Trees – Definition, Insertion, Deletion, Traversal, and Searching
operation on Binary search tree. Application of Trees-Evaluation of Expression.

Textbook 1: Chapter 5: 5.1 –5.5, 5.7; Textbook 2: Chapter 7: 7.1 – 7.9


Laboratory Component:

1. Given an array of elements, construct a complete binary tree from this array in level order fashion. That
is, elements from left in the array will be filled in the tree level wise starting from level 0. Ex: Input :
arr[] = {1, 2, 3, 4, 5, 6}
Output : Root of the following tree
1
/\2
3
/ \ /\
4 5 6
2. Design, Develop and Implement a menu driven Program in C for the following operations on
Binary Search Tree (BST) of Integers
a. Create a BST of N Integers
b. Traverse the BST in Inorder, Preorder and Post Order

Teaching-Learning Process Problem based learning


http://www.nptelvideos.in/2012/11/data-structures-and-
algorithms.html
https://ds1-iiith.vlabs.ac.in/exp/tree-traversal/index.html
https://ds1-iiith.vlabs.ac.in/exp/tree-traversal/depth-first-
traversal/dft-practice.html

Module-5
Trees 2: AVL tree, Red-black tree, Splay tree, B-tree.

Graphs: Definitions, Terminologies, Matrix and Adjacency List Representation of Graphs, Traversal methods:
Breadth First Search and Depth FirstSearch.

Hashing: Hash Table organizations, Hashing Functions, Static and Dynamic Hashing.

Textbook 1: Chapter 10:10.2, 10.3, 10.4, Textbook 2:7.10 – 7.12, 7.15 Chapter 11: 11.2, Textbook 1:
Chapter 6 : 6.1–6.2, Chapter 8 : 8.1-8.3, Textbook 2: 8.1 – 8.3, 8.5, 8.7

Textbook 3: Chapter 15:15.1, 15.2,15.3, 15.4,15.5 and 15.7

Dept Of C.S.E.SIT Page 5


Data structures laboratory 21CS32

Laboratory Component:

1. Design, Develop and implement a program in C for the following operations on Graph (G) of cities
a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a diagraph using DFS/BFS
method.

2. Design and develop a program in C that uses Hash Function H:K->L as H(K)=K mod m(reminder
method) and implement hashing technique to map a given key K to the address space L. Resolve the
collision (if any) using linear probing.

Teaching-Learning Process NPTL, MOOC etc. courses on trees and graphs.


http://www.nptelvideos.in/2012/11/data-structures-and-
algorithms.html
Course Outcomes (Course Skill Set)
At the end of the course the student will be able to:
CO 1. Identify different data structures and their applications. CO
2. Apply stack and queues in solving problems.
CO 3. Demonstrate applications of linked list.
CO 4. Explore the applications of trees and graphs to model and solve the real-world problem. CO
5. Make use of Hashing techniques and resolve collisions during mapping of key value pairs
Assessment Details (both CIE and SEE)
The weightage of Continuous Internal Evaluation (CIE) is 50% and for Semester End Exam (SEE) is 50%. The
minimum passing mark for the CIE is 40% of the maximum marks (20 marks). A student shall be deemed to
have satisfied the academic requirements and earned the credits allotted to each subject/ course if the student
secures not less than 35% (18 Marks out of 50) in the semester-end examination (SEE), and a minimum of 40%
(40 marks out of 100) in the sum total of the CIE (Continuous Internal Evaluation) and SEE (Semester End
Examination) taken together
Continuous Internal Evaluation:
Three Unit Tests each of 20 Marks (duration 01 hour)
1. First test at the end of 5th week of the semester
2. Second test at the end of the 10th week of the semester
3. Third test at the end of the 15th week of the semester
Two assignments each of 10 Marks
4. First assignment at the end of 4th week of the semester
5. Second assignment at the end of 9th week of the semester

Practical Sessions need to be assessed by appropriate rubrics and viva-voce method. This will contribute to 20
marks.
Note: Minimum of 80% of the laboratory components have to be covered.

 Rubrics for each Experiment taken average for all Lab components – 15 Marks.
 Viva-Voce– 5 Marks (more emphasized on demonstration topics)

The sum of three tests, two assignments, and practical sessions will be out of 100 marks and will be
scaled down to 50 marks
(to have a less stressed CIE, the portion of the syllabus should not be common /repeated for any of the methods
of the CIE. Each method of CIE should have a different syllabus portion of the course).
CIE methods /question paper has to be designed to attain the different levels of Bloom’s taxonomy as per
the outcome defined for the course.

Semester End Examination:


Theory SEE will be conducted by University as per the scheduled timetable, with common question

Dept Of C.S.E.SIT Page 6


Data structures laboratory 21CS32

papers for the subject (duration 03 hours)


1. The question paper will have ten questions. Each question is set for 20 marks.
2. There will be 2 questions from each module. Each of the two questions under a module (with a
maximum of 3 sub-questions), should have a mix of topics under that module.

The students have to answer 5 full questions, selecting one full question from each module
Suggested Learning Resources:
Textbooks:
1. Ellis Horowitz and Sartaj Sahni, Fundamentals of Data Structures in C, 2nd Ed, Universities Press,
2014.
2. Seymour Lipschutz, Data Structures Schaum's Outlines, Revised 1st Ed, McGraw Hill, 2014.
3. Reema Thareja, Data Structures using C, 3rd Ed, Oxford press, 2012.
Reference Books:
1. Gilberg and Forouzan, Data Structures: A Pseudo-code approach with C, 2nd Ed, Cengage
Learning,2014.
2. Jean-Paul Tremblay & Paul G. Sorenson, An Introduction to Data Structures with Applications,2nd Ed,
McGraw Hill, 2013
3. A M Tenenbaum, Data Structures using C, PHI, 1989
4. Robert Kruse, Data Structures and Program Design in C, 2nd Ed, PHI, 1996.
Weblinks and Video Lectures (e-Resources):
1. http://elearning.vtu.ac.in/econtent/courses/video/CSE/06CS35.html 2.
https://nptel.ac.in/courses/106/105/106105171/
3. http://www.nptelvideos.in/2012/11/data-structures-and-algorithms.html
Activity Based Learning (Suggested Activities in Class)/ Practical Based learning
 Real world problem solving using group discussion.
 Back/Forward stacks on browsers.
 Undo/Redo stacks in Excel or Word.
 Linked list representation of real-world queues -Music player, image viewer

Dept Of C.S.E.SIT Page 7


Data structures laboratory 21CS32

1.Design, Develop and Implement a menu driven Program in C for the following Array Operations
a. Creating an Array of N Integer Elements
b. Display of Array Elements with Suitable Headings
c. Exit.
Support the program with functions for each of the above operations.

2. Design, Develop and Implement a menu driven Program in C for the following Array operations
a. Inserting an Element (ELEM) at a given valid Position (POS)
b. Deleting an Element at a given valid Position POS)
c. Display of Array Elements

#include<stdio.h>
#include<stdlib.h>
int a[20];
int n,val,i,pos;
/*Function Prototype*/
void create();
void display();
void insert();
void delete();
int main()
{
int choice;
while(choice)
{
printf("\n\n--------MENU-----------\n");
printf("1.CREATE\n");
printf("2.DISPLAY\n");
printf("3.INSERT\n");
printf("4.DELETE\n");
printf("5.EXIT\n");
printf("-----------------------");
printf("\nENTER YOUR CHOICE:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: create();
break;
case 2:
display();
break;
case 3:
insert();
break;
case 4:
delete();
break;
case 5:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}
return 0;
}
//creating an array

Dept Of C.S.E.SIT Page 8


Data structures laboratory 21CS32

void create()
{
printf("\nEnter the size of the array elements:\t");
scanf("%d",&n);
printf("\nEnter the elements for the array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
//displaying an array elements
void display()
{
int i;
printf("\nThe array elements are:\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
//inserting an element into an array
void insert()
{
printf("\nEnter the position for the new element:\t");
scanf("%d",&pos);
printf("\nEnter the element to be inserted :\t");
scanf("%d",&val);
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=val;
n=n+1;
}
//deleting an array element
void delete()
{
printf("\nEnter the position of the element to be deleted:\t");
scanf("%d",&pos);
val=a[pos];
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
printf("\nThe deleted element is =%d",val);
}

Output

Dept Of C.S.E.SIT Page 9


Data structures laboratory 21CS32

--------MENU-----------
1.CREATE
2.DISPLAY
3.INSERT
4.DELETE
5.EXIT
-----------------------
ENTER YOUR CHOICE: 1
Enter the size of the array elements: 3
Enter the elements for the array:
10 25 30

ENTER YOUR CHOICE: 2


The array elements are:
10 25 30

ENTER YOUR CHOICE: 3


Enter the position for the new element: 1
Enter the element to be inserted : 20

ENTER YOUR CHOICE: 2


The array elements are:
10 20 25 30

ENTER YOUR CHOICE: 4


Enter the position of the element to be deleted: 3
The deleted element is =30
enter your choice: 5

Dept Of C.S.E.SIT Page 10


Data structures laboratory 21CS32

3. Design, Develop and Implement a menu driven Program in C for the following operations on
STACK of Integers (Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate Overflow and Underflow situations on Stack
d. Display the status of Stack
e. Exit
Support the program with appropriate functions for each of the above operations

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define max_size 5
int stack[max_size],top=-1;
void push();
void pop();
void display();
void pali();
int main()
{
int choice;
while(choice)
{
//printf("\n");
printf("\n\n--------STACK OPERATIONS-----------\n");
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Palindrome\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
pali();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("\n Invalid choice:\n");
break;
}
}
return 0;

Dept Of C.S.E.SIT Page 11


Data structures laboratory 21CS32

}
void push() //Inserting element into the stack
{
int item,n;
if(top==(max_size-1))
{
printf("\n Stack Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
void pop() //deleting an element from the stack
{
int item;
if(top==-1)
{
printf("Stack Underflow:");
}
else
{
item=stack[top];
top=top-1;
printf("\nThe poped element: %d\t",item);
}
}
void pali()
{
int digit,j,k,len=top+1,flag=0,ind=0;
int num[len],rev[len],i=0;
while(top!=-1)
{
digit= stack[top];
num[i]=digit;
top--;
i++;
}
for(j=0;j<len;j++)
{
printf("Numbers= %d\n",num[j]);
}
printf("reverse operation : \n");
for(k=len-1;k>=0;k--)
{
rev[k]=num[ind];
ind++;
}
printf("reverse array : ");
for(k=0;k<len;k++)
{
printf("%d\n",rev[k]);

Dept Of C.S.E.SIT Page 12


Data structures laboratory 21CS32

}
printf("check for palindrome :\n");
int length = 0;
for(i=0;i<len;i++)
{
if(num[i]==rev[i])
{
length = length+1;
}
}
if(length==len)
{
printf("It is palindrome number\n");
}
else
{
printf("It is not a palindrome number\n");
}
top = len-1;
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is Empty:");
}
else
{
printf("\nThe stack elements are:\n" );
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
}
}

Dept Of C.S.E.SIT Page 13


Data structures laboratory 21CS32

Output

--------STACK OPERATIONS-----------
1.Push
2.Pop
3.Palindrome
4.Display
5.Exit
-----------------------
Enter your choice: 1
Enter the element to be inserted: 1

Enter your choice: 1


Enter the element to be inserted: 2

Enter your choice: 1


Enter the element to be inserted: 1

Enter your choice: 1


Enter the element to be inserted: 5

Enter your choice: 2


The poped element: 5

Enter your choice: 4


The stack elements are:
1
2
1
Enter your choice: 3
Numbers= 1
Numbers= 2
Numbers= 1
reverse operation :
reverse array : 1
2
1
check for palindrome :
It is palindrome number
Enter your choice: 5

Dept Of C.S.E.SIT Page 14


Data structures laboratory 21CS32

4. Design, Develop and Implement a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^
b. Solving Tower of Hanoi problem with n disks

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define MAX 50
int stack[MAX];
char post[MAX];
int top=-1;
void pushstack(int tmp);
void calculator(char c);
void main()
{
int i;
printf("Insert a postfix notation :: ");
//gets(post);
scanf("%s",post);
for(i=0;i<strlen(post);i++)
{
if(post[i]>='0' && post[i]<='9')
{
pushstack(i);
}
if(post[i]=='+' || post[i]=='-' || post[i]=='*' || post[i]=='/' || post[i]=='^')
{
calculator(post[i]);
}
}
printf("\n\nResult :: %d",stack[top]);
}
void pushstack(int tmp)
{
top++;
stack[top]=(int)(post[tmp]-48);
}
void calculator(char c)
{
int a,b,ans;
a=stack[top];
stack[top]='\0';
top--;
b=stack[top];
stack[top]='\0';
top--;
switch(c)
{
case '+':
ans=b+a;
break;
case '-':
ans=b-a;
break;

Dept Of C.S.E.SIT Page 15


Data structures laboratory 21CS32

case '*':
ans=b*a;
break;
case '/':
ans=b/a;
break;
case '^':
ans=pow(b,a);
break;
default:
ans=0;
}
top++;
stack[top]=ans;
}
// Towers of Hanoi
#include <stdio.h>
void towers(int, char, char, char);
int main()
{
int num;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}

Dept Of C.S.E.SIT Page 16


Data structures laboratory 21CS32

Output

Insert a postfix notation :: 22^32*+

Result :: 10

Enter the number of disks : 3

The sequence of moves involved in the Tower of Hanoi are :

Move disk 1 from peg A to peg C


Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C

Dept Of C.S.E.SIT Page 17


Data structures laboratory 21CS32

5. Singly Linked List (SLL) of Integer Data


a. Create a SLL stack of N integer.
b. Display of SLL
c. Linear search. Create a SLL queue of N Students Data Concatenation of two SLL of integer.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int count=0;
struct node
{
int sem,phno;
char name[20],branch[10],usn[20];
struct node *next;
}*first=NULL,*last=NULL,*temp=NULL, *temp1;
void create()
{
int sem,phno;
char name[20],branch[10],usn[20];
temp=(struct node*)malloc(sizeof(struct node));
printf("\n Enter usn,name, branch, sem, phno of student : ");
scanf("%s %s %s %d %d", usn, name,branch, &sem,&phno);
strcpy(temp->usn,usn);
strcpy(temp->name,name);
strcpy(temp->branch,branch);
temp->sem = sem;
temp->phno = phno;
temp->next=NULL;
count++;
}
void insert_atfirst()
{
if (first == NULL)
{
create();
first = temp;
last = first;
}
else
{
create();
temp->next = first;
first = temp;
}
}
void insert_atlast()
{
if(first==NULL)
{
create();
first = temp;
last = first;
}
else
{
create();

Dept Of C.S.E.SIT Page 18


Data structures laboratory 21CS32

last->next = temp;
last = temp;
}
}
void display()
{
temp1=first;
if(temp1 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : \n");
while (temp1!= NULL)
{
printf("%s %s %s %d %d\n", temp1->usn, temp1->name,temp1->branch,temp1->sem,temp1->phno );
temp1 = temp1->next;
}
printf(" No of students = %d ", count);
}
int deleteend()
{
struct node *temp;
temp=first;
if(temp->next==NULL)
{
free(temp);
first=NULL;
}
else
{
while(temp->next!=last)
temp=temp->next;
printf("%s %s %s %d %d\n", last->usn, last->name,last->branch,
last->sem, last->phno );
free(last);
temp->next=NULL;
last=temp;
}
count--;
return 0;
}
int deletefront()
{
struct node *temp;
temp=first;
if(temp->next==NULL)
{
free(temp);
first=NULL;
return 0;
}
else
{
first=temp->next;

Dept Of C.S.E.SIT Page 19


Data structures laboratory 21CS32

printf("%s %s %s %d %d", temp->usn, temp->name,temp->branch,temp->sem, temp->phno );


free(temp);
}
count--;
return 0;
}
void main()
{
int ch,n,i;
first=NULL;
temp = temp1 = NULL;
printf("-----------------MENU----------------------\n");
printf("\n 1 – create a SLL of n emp");
printf("\n 2 - Display from beginning");
printf("\n 3 - Insert at end");
printf("\n 4 - delete at end");
printf("\n 5 - Insert at beg");
printf("\n 6 - delete at beg");
printf("\n 7 - exit\n");
printf("-------------------------------------------\n");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\n Enter no of students : ");
scanf("%d", &n);
for(i=0;i<n;i++)
insert_atfirst();
break;
case 2:
display();
break;
case 3:
insert_atlast();
break;
case 4:
deleteend();
break;
case 5:
insert_atfirst();
break;
case 6:
deletefront();
break;
case 7:
exit(0);
default: printf("wrong choice\n");
}
}}
Output
---------------MENU----------------------
1 – create a SLL of n emp

Dept Of C.S.E.SIT Page 20


Data structures laboratory 21CS32

2 - Display from beginning


3 - Insert at end
4 - delete at end
5 - Insert at beg
6 - delete at beg
7 - exit
------------------------------------------------
Enter choice : 1
Enter no of students : 2
Enter usn,name, branch, sem, phno of student : 007 vijay CSE 3 121
Enter usn,name, branch, sem, phno of student : 100 yashas CSE 3 911

Enter choice : 2
Linked list elements from begining :
100 yashas CSE 3 911
007 vijay CSE 3 121
No of students = 2

Enter choice : 3
Enter usn,name, branch, sem, phno of student : 001 raj CSE 3 111

Enter choice : 2
Linked list elements from begining :
100 yashas CSE 3 911
007 vijay CSE 3 121
001 raj CSE 3 111
No of students = 3

Enter choice : 4
001 raj CSE 3 111

Enter choice : 2
Linked list elements from begining :
100 yashas CSE 3 911
007 vijay CSE 3 121
No of students = 2
Enter choice : 5
Enter usn,name, branch, sem, phno of student : 003 harsh cse 3 111

Enter choice : 2
Linked list elements from begining :
003 harsh cse 3 111
100 yashas CSE 3 911
007 vijay CSE 3 121
No of students = 3

Enter choice : 6
003 harsh cse 3 111
Enter choice : 2
Linked list elements from begining :
100 yashas CSE 3 911
007 vijay CSE 3 121
No of students = 2

Enter choice : 7

Dept Of C.S.E.SIT Page 21


Data structures laboratory 21CS32

6..Design, Develop and Implement a menu driven Program in C for the following operations on Doubly Linked List
(DLL) of Professor Data with the fields: ID, Name, Branch, Area of specialization
c. Create a DLL stack of N Professor’s Data.
d. Create a DLL queue of N Professor’s Data
Display the status of DLL and count the number of nodes in it.
#include<string.h>
int count=0;
struct node
{
struct node *prev;
int ssn,phno;
float sal;
char name[20],dept[10],desg[20];
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;
void create()
{
int ssn,phno;
float sal;
char name[20],dept[10],desg[20];
temp =(struct node *)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\n Enter ssn,name,department, designation, salary and phno of employee : ");
scanf("%d %s %s %s %f %d", &ssn, name,dept,desg,&sal, &phno);
temp->ssn = ssn;
strcpy(temp->name,name);
strcpy(temp->dept,dept);
strcpy(temp->desg,desg);
temp->sal = sal;
temp->phno = phno;
count++;
}
void insertbeg()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
void insertend()
{
if(h==NULL)
{
create();
h = temp;
temp1 = h;

Dept Of C.S.E.SIT Page 22


Data structures laboratory 21CS32

}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
void displaybeg()
{
temp2 =h;
if(temp2 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : \n");
while (temp2!= NULL)
{
printf("%d %s %s %s %f %d\n", temp2->ssn, temp2->name,temp2->dept,temp2->desg,temp2->sal,
temp2->phno );
temp2 = temp2->next;
}
printf(" No of employees = %d ", count);
}
int deleteend()
{
struct node *temp;
temp=h;
if(temp->next==NULL)
{
free(temp);
h=NULL;
return 0;
}
else
{
temp2=temp1->prev;
temp2->next=NULL;
printf("%d %s %s %s %f %d\n", temp1->ssn, temp1->name,temp1->dept,
temp1->desg,temp1->sal, temp1->phno );
free(temp1);
}
count--;
return 0;
}
int deletebeg()
{
struct node *temp;
temp=h;
if(temp->next==NULL)
{
free(temp);
h=NULL;

Dept Of C.S.E.SIT Page 23


Data structures laboratory 21CS32

}
else
{
h=h->next;
printf("%d %s %s %s %f %d", temp->ssn, temp->name,temp->dept,
temp->desg,temp->sal, temp->phno );
free(temp);
}
count--;
return 0;
}
void main()
{
int ch,n,i;
h=NULL;
temp = temp1 = NULL;
printf("-----------------MENU--------------------\n");
printf("\n 1 – create a DLL of n emp");
printf("\n 2 - Display from beginning");
printf("\n 3 - Insert at end");
printf("\n 4 - delete at end");
printf("\n 5 - Insert at beg");
printf("\n 6 - delete at beg");
printf("\n 7 - exit\n");
printf("------------------------------------------\n");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\n Enter no of employees : ");
scanf("%d", &n);
for(i=0;i<n;i++)
insertend();
break;
case 2:
displaybeg();
break;
case 3:
insertend();
break;
case 4:
deleteend();
break;
case 5:
insertbeg();
break;
case 6:
deletebeg();
break;
case 7:
exit(0);
default: printf("wrong choice\n"); }}}

Dept Of C.S.E.SIT Page 24


Data structures laboratory 21CS32

Output

-----------------MENU--------------------
1 –Create a DLL of n emp
2 - Display from beginning
3 - Insert at end
4 - Delete at end
5 - Insert at beg
6 - Delete at beg
7 - exit
------------------------------------------
Enter choice : 1
Enter no of employees : 2
Enter ssn,name,department, designation, salary and phno of employee :
1 RAJ SALES MANAGER 15000 911
Enter ssn,name,department, designation, salary and phno of employee :
2 RAVI HR ASST 10000 123

Enter choice : 2
Linked list elements from begining :
1 RAJ SALES MANAGER 15000.000000 911
2 RAVI HR ASST 10000.000000 123
No of employees = 2

Enter choice : 3
Enter ssn,name,department, designation, salary and phno of employee :
3 RAM MARKET MANAGER 50000 111

Enter choice : 2
Linked list elements from begining :
1 RAJ SALES MANAGER 15000.000000 911
2 RAVI HR ASST 10000.000000 123
3 RAM MARKET MANAGER 50000.000000 111
No of employees = 3

Enter choice : 4
3 RAM MARKET MANAGER 50000.000000 111

Enter choice : 2
Linked list elements from begining :
1 RAJ SALES MANAGER 15000.000000 911
2 RAVI HR ASST 10000.000000 123
No of employees = 2

Enter choice : 5
Enter ssn,name,department, designation, salary and phno of employee :
0 ALEX EXE TRAINEE 2000 133

Enter choice : 2
Linked list elements from begining :
0 ALEX EXE TRAINEE 2000.000000 133
1 RAJ SALES MANAGER 15000.000000 911
2 RAVI HR ASST 10000.000000 123
No of employees = 3

Dept Of C.S.E.SIT Page 25


Data structures laboratory 21CS32

Enter choice : 6
0 ALEX EXE TRAINEE 2000.000000 133

Enter choice : 2
Linked list elements from begining :
1 RAJ SALES MANAGER 15000.000000 911
2 RAVI HR ASST 10000.000000 123
No of employees = 2

Enter choice : 7
Exit

Dept Of C.S.E.SIT Page 26


Data structures laboratory 21CS32

7 .Given an array of elements, construct a complete binary tree from this array in level order fashion. That is, elements
from left in the array will be filled in the tree level wise starting from level 0. Ex: Input :
arr[] = {1, 2, 3, 4, 5, 6}
Output : Root of the following tree 1
1
/\
23
/ \ /\
4 5 6

8. Design, Develop and Implement a menu driven Program in C for the following operations on Binary Search
Tree (BST) of Integers
a. Create a BST of N Integers
b. Traverse the BST in Inorder, Preorder and Post Order

# include <stdio.h>
# include <stdlib.h>
int flag=0;
typedef struct BST
{
int data;
struct BST *lchild, *rchild;
}
node;
void insert(node *, node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *, int, node **);
void main()
{
int choice;
int ans =1;
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;
printf("\nProgram For Binary Search Tree ");
do
{
printf("\n1.Create");
printf("\n2.Search");
printf("\n3.Recursive Traversals");
printf("\n4.Exit");
printf("\nEnter your choice :");
scanf("%d", &choice);
switch (choice)
{
case 1:
do{
new_node = get_node();
printf("\nEnter The Element ");
scanf("%d", &new_node->data);
if (root == NULL) /* Tree is not Created */
root = new_node;

Dept. of CSE, SIT-2022 Page 27


Data structures laboratory 21CS32

else
insert(root, new_node);
printf("\nWant To enter More Elements?(1/0)");
scanf("%d",&ans);
}
while (ans);
break;
case 2:
printf("\nEnter Element to be searched :");
scanf("%d", &key);
tmp = search(root, key, &parent);
if(flag==1)
{
printf("\nParent of node %d is %d", tmp->data, parent->data);
}
else
{
printf("\n The %d Element is not Present",key);
}
flag=0;
break;
case 3:
if (root == NULL)
printf("Tree Is Not Created");
else
{
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Postorder display : ");
postorder(root);
}
break;
}
}
while (choice != 4);
}
/* Get new Node */
node *get_node()
{
node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
/* This function is for creating a binary search tree */
void insert(node *root, node *new_node)
{
if (new_node->data < root->data)
{
if (root->lchild == NULL)
root->lchild = new_node;
else
insert(root->lchild, new_node);

Dept. of CSE, SIT-2022 Page 28


Data structures laboratory 21CS32

}
if (new_node->data > root->data)
{
if (root->rchild == NULL)
root->rchild = new_node;
else
insert(root->rchild, new_node);
}
}
/* This function is for searching the node from binary Search Tree */
node *search(node *root, int key, node **parent)
{
node *temp;
temp = root;
while (temp != NULL)
{
if (temp->data == key)
{
printf("\nThe %d Element is Present", temp->data);
flag=1;
return temp;
}
*parent = temp;
if (temp->data > key)
temp = temp->lchild;
else
temp = temp->rchild;
}
return NULL;
}
/* This function displays the tree in inorder fashion */
void inorder(node *temp)
{
if (temp != NULL)
{
inorder(temp->lchild);
printf("%d\t", temp->data);
inorder(temp->rchild);
} }
/* This function displays the tree in preorder fashion */
void preorder(node *temp)
{
if (temp != NULL)
{
printf("%d\t", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
/* This function displays the tree in postorder fashion */
void postorder(node *temp)
{
if (temp != NULL)
{
postorder(temp->lchild);
postorder(temp->rchild);

Dept. of CSE, SIT-2022 Page 29


Data structures laboratory 21CS32

printf("%d\t", temp->data);
}
}
Output

Program For Binary Search Tree


1.Create
2.Search
3.Recursive Traversals
4.Exit
Enter your choice :1
Enter The Element 15
Want To enter More Elements?(1/0)1
Enter The Element 25
Want To enter More Elements?(1/0)1
Enter The Element 35
Want To enter More Elements?(1/0)1
Enter The Element 45
Want To enter More Elements?(1/0)1
Enter The Element 5
Want To enter More Elements?(1/0)1
Enter The Element 7
Want To enter More Elements?(1/0)0

Enter your choice :2


Enter Element to be searched :7
The 7 Element is Present
Parent of node 7 is 5
1.Create
2.Search
3.Recursive Traversals
4.Exit

Enter your choice :2


Enter Element to be searched :88
The 88 Element is not Present

Enter your choice :3


The Inorder display : 5 7 15 25 35 45
The Preorder display : 15 5 7 25 35 45
The Postorder display : 7 5 45 35 25 15

Dept. of CSE, SIT-2022 Page 30


Data structures laboratory 21CS32

9. Design, Develop and implement a program in C for the following operations on Graph (G) of cities
• Create a Graph of N cities using Adjacency Matrix.
• Print all the nodes reachable from a given starting node in a diagraph using DFS/BFS method.

#include <stdio.h>
#include <stdlib.h>
int a[20][20],q[20],visited[20],reach[10],n,i,j,f=0,r=-1,count=0;
void bfs(int v)
{
for(i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r)
{
visited[q[f]]=1;
bfs(q[f++]);
}
}
void dfs(int v)
{
int i;
reach[v]=1;
for(i=1;i<=n;i++)
{
if(a[v][i] && !reach[i])
{
printf("\n %d->%d",v,i);
count++;
dfs(i);
}
}
}
void main()
{
int v, choice;
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
q[i]=0;
visited[i]=0;
}
for(i=1;i<=n-1;i++)
reach[i]=0;
printf("\n Enter graph data in matrix form:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("1.BFS\n 2.DFS\n 3.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);

Dept. Of C.S.E.,MSEC-2019 Page 31


Data structures laboratory 21CS32

if((v<1)||(v>n))
{
printf("\n Bfs is not possible");
}
else
{
printf("\n The nodes which are reachable from %d:\n",v);
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
}
break;
case 2:dfs(1);
if(count==n-1)
printf("\n Graph is connected");
else
printf("\n Graph is not connected");
break;
case 3: exit(0);
}
}

Output

Enter the number of vertices:5


Enter graph data in matrix form:
01010
10101
01010
10100
01000
1.BFS
2.DFS
3.Exit
2
1->2
2->3
3->4
2->5
Graph is connectedcsdept

Enter the number of vertices: 5


Enter graph data in matrix form:
01010
10100
01010
10100
00000
1.BFS
2.DFS
3.Exit

Dept. Of C.S.E.,MSEC-2019 Page 32


Data structures laboratory 21CS32

2
1->2
2->3
3->4
Graph is not connected
Enter the number of vertices:5

Enter graph data in matrix form:


01100
00010
00000
00100
00100
1.BFS
2.DFS
3.Exit
1
Enter the starting vertex:1
The nodes which are reachable from 1:
234
Enter graph data in matrix form:
01100
00010
00000
00100
00100
1.BFS
2.DFS
3.Exit
1
Enter the starting vertex:0
BFS is not possible

Dept. Of C.S.E.,MSEC-2019 Page 33


Data structures laboratory 21CS32

10. Design and develop a program in C that uses Hash Function H:K->L as H(K)=K mod m(reminder method) and
implement hashing technique to map a given key K to the address space L. Resolve the collision (if any) using linear
probing.

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int create(int);
void linear_prob(int[], int, int);
void display (int[]);
void main()
{
int a[MAX],num,key,i;
int ans=1;
printf(" collision handling by linear probing : \n");
for (i=0;i<MAX;i++)
{
a[i] = -1;
}
do
{
printf("\n Enter the data");
scanf("%4d", &num);
key=create(num);
linear_prob(a,key,num);
printf("\n Do you wish to continue ? (1/0) ");
scanf("%d",&ans);
}while(ans);
display(a);
}
int create(int num)
{
int key;
key=num%100;
return key;
}
void linear_prob(int a[MAX], int key, int num)
{
int flag, i, count=0;
flag=0;
if(a[key]== -1)
{
a[key] = num;
}
else
{
printf("\nCollision Detected...!!!\n");
i=0;
while(i<MAX)
{
if (a[i]!=-1)
count++;
i++;
} printf("Collision avoided successfully using LINEAR PROBING\n");

Dept. Of C.S.E.,MSEC-2019 Page 34


Data structures laboratory 21CS32

if(count == MAX)
{
printf("\n Hash table is full");
display(a);
exit(1);
}
for(i=key+1; i<MAX; i++)
if(a[i] == -1)
{
a[i] = num;
flag =1;
break;
}
//for(i=0;i<key;i++)
i=0;
while((i<key) && (flag==0))
{
if(a[i] == -1)
{
a[i] = num;
flag=1;
break;
}
i++;
}
}
}
void display(int a[MAX])
{
int i,choice;
printf("1.Display ALL\n 2.Filtered Display\n");
scanf("%d",&choice);
if(choice==1)
{
printf("\n the hash table is\n");
for(i=0; i<MAX; i++)
printf("\n %d %d ", i, a[i]);
}
else
{
printf("\n the hash table is\n");
for(i=0; i<MAX; i++)
if(a[i]!=-1)
{
printf("\n %d %d ", i, a[i]);
continue;
}
}
}

Dept. Of C.S.E.,MSEC-2019 Page 35


Data structures laboratory 21CS32

Output
collision handling by linear probing :
Enter the data1234
Do you wish to continue ? (1/0) 1
Enter the data2548

Do you wish to continue ? (1/0) 1


Enter the data3256

Do you wish to continue ? (1/0) 1


Enter the data1299

Do you wish to continue ? (1/0) 1


Enter the data1298

Do you wish to continue ? (1/0) 1


Enter the data1398
Collision Detected...!!!

Collision avoided successfully using LINEAR PROBING


Do you wish to continue ? (1/0) 0
1.Display ALL
2.Filtered Display
2
the hash table is
0 1398
34 1234
48 2548
56 3256
98 1298
99 1299

Do you wish to continue ? (1/0) 1


Enter the data2548

Do you wish to continue ? (1/0) 1


Enter the data3256
Do you wish to continue ? (1/0) 1
Enter the data1299
Do you wish to continue ? (1/0) 1
Enter the data1298
Do you wish to continue ? (1/0) 1
Enter the data1398
Collision Detected...!!!
Collision avoided successfully using LINEAR PROBING
Do you wish to continue ? (1/0) 0
1.Display ALL
2.Filtered Display
the hash table is
0 1398
34 1234
48 2548
56 3256

Dept. Of C.S.E.,MSEC-2019 Page 36


Data structures laboratory 21CS32

98 1298
99 1299

Dept. Of C.S.E.,MSEC-2019 Page 37


Data structures laboratory 21CS32

Dept. Of C.S.E.,MSEC-2019 Page 38

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy