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

Dsc Lab Manual Final

The Data Structures Lab Manual (21CSL36) outlines a laboratory course for third-semester students focusing on practical applications of dynamic memory management and various data structures, including linear and non-linear types. It includes a series of programming tasks in C, such as creating a calendar, string manipulation, stack operations, and implementing data structures like linked lists and binary search trees. The manual aims to enhance students' understanding and skills in coding, debugging, and evaluating data structures and algorithms.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Dsc Lab Manual Final

The Data Structures Lab Manual (21CSL36) outlines a laboratory course for third-semester students focusing on practical applications of dynamic memory management and various data structures, including linear and non-linear types. It includes a series of programming tasks in C, such as creating a calendar, string manipulation, stack operations, and implementing data structures like linked lists and binary search trees. The manual aims to enhance students' understanding and skills in coding, debugging, and evaluating data structures and algorithms.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 82

DATA STRUCUTRES LAB MANUAL(21CSL36)

DATA STRUCTURES LABORATORY


SEMESTER – III

Course Code 21CSL36 CIE Marks 50


Number of Contact Hours/Week 02 SEE Marks 50
Total Number of Lab Contact Hours 20 Exam Hours 03
Credits – 1
Course Learning Objectives:
This laboratory course enables students to get practical experience in design, develop, implement, analyze
and evaluation/testing of
● Dynamic memory management
● Linear data structures and their applications such as stacks, queues and lists
● Non-Linear data structures and their applications such as trees and graphs

Descriptions (if any):


● Implement all the programs in “C ” Programming Language and Linux OS.
Programs List:
1. Develop a Program in C for the following:
a) Declare a calendar as an array of 7 elements (A dynamically Created array) to represent
7 days of a week. Each Element of the array is a structure having three fields. The first
field is the name of the Day (A dynamically allocated String), The second field is the
date of the Day (A integer), the third field is the description of the activity for a
particular day (A dynamically allocated String).
b) Write functions create(), read() and display(); to create the calendar, to read the data
from the keyboard and to print weeks activity details report on screen.

2. Develop a Program in C for the following operations on Strings.


a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in
STR with REP if PAT exists in STR. Report suitable messages in case PAT does not
exist in STR
Support the program with functions for each of the above operations. Don't use Built-in
functions.
3. Develop 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 how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations

DEPT.OF CSE ,FETW,SUK,KLB


6. Develop a menu driven Program in C for the following operations on Circular QUEUE of
Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
DATA
b. Delete STRUCUTRES
an Element from Circular LABQUEUE MANUAL(21CSL36)
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
4. e. Exit
Develop a Program in C for converting an Infix Expression to Postfix Expression. Program
should support
Support for both
the program withparenthesized and free parenthesized
appropriate functions for each of the above operations
7. expressions
Develop withdriven
a menu the operators:
Program+,in-,C*,for
/, %
the(Remainder), ^ (Power)onand
following operations alphanumeric
Singly Linked List
operands.
(SLL) of Student Data with the fields: USN, Name, Programme, Sem,
5. PhNo
Develop a Program in C for the following Stack Applications
a.
a. Create a SLL
Evaluation ofof N Students
Suffix Datawith
expression by using
singlefront
digitinsertion.
operands and operators: +, -, *, /, %,
b. Display
^ the status of SLL and count the number of nodes in it
c.
b. Perform Insertion
Solving Tower of /Hanoi
Deletion at Endwith
problem of SLL
n disks
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit
8. Develop a menu driven Program in C for the following operations on Doubly Linked List
(DLL) of Employee Data with the fields: SSN, Name, Dept, Designation,
Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue.
f. Exit
9. Develop a Program in C for the following operationson Singly Circular Linked List (SCLL)
with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the
result in POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations
10. Develop a menu driven Program in C for the following operations on Binary Search Tree
(BST) of Integers .
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit
11. Develop 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 digraph using DFS/BFS
method

12. Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine
the records in file F. Assume that file F is maintained in memory by a Hash Table (HT) of m
memory locations with L as the set of memory addresses (2-digit) of locations in HT. Let the
keys in K and addresses in L are Integers. Develop a Program in C that uses Hash function H:
K →L as H(K)=K mod m (remainder method), and implement hashing
technique to map a given key K to the address space L. Resolve the collision (if any) using
linear probing.
Laboratory Outcomes: The student should be able to:

Course Outcomes

● Analyze and Compare various linear and non-linear data structures


● Code, debug and demonstrate the working nature of different types of data structures and
their applications
● Implement, analyze and evaluate the searching and sorting algorithms
● Choose
DEPT.OF the appropriate data structure for solving real world problems.
CSE ,FETW,SUK,KLB
Conduct of Practical Examination: • Experiment distribution
a) For laboratories having only one part: Students are allowed to pick one experiment from the
lot with equal opportunity.
DATA STRUCUTRES LAB MANUAL(21CSL36)

1)

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

2) Develop a Program in C for the following:


a) Declare a calendar as an array of 7 elements (A dynamically Created array) to represent
7 days of a week. Each Element of the array is a structure having three fields.
The first field is the name of the Day (A dynamically allocated String),
The second field is the date of the Day (A integer),
the third field is the description of the activity for a particular day (A dynamically
allocated String).
b) Write functions create(), read() and display(); to create the calendar, to read the data
from the keyboard and to print weeks activity details report on screen..
#include <stdio.h>
#include <stdlib.h>
// Structure to represent a day in the calendar
struct Day {
char * dayName; // Dynamically allocated string for the day name
int date;
char * activity; // Dynamically allocated string for the activity description
};// Function to create a day
void create(struct Day * day) {
// Allocate memory for the day name and activity
day -> dayName = (char * ) malloc(sizeof(char) * 20); // Assuming day names are less than 20
characters
day -> activity = (char * ) malloc(sizeof(char) * 100); // Assuming activity descriptions are less
than 100 characters// Input the day details
printf("Enter the day name:");
scanf("%s", day -> dayName);printf("Enter the date:");
scanf("%d", & day -> date);printf("Enter the activity for the day:");
scanf(" %s", day -> activity); // Read the entire line, including spaces
}// Function to read data from the keyboard and create the calendar
void read(struct Day * calendar, int size) {
for (int i = 0; i < size; i++) {
printf("Enter details for Day %d:\n", i + 1);

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

create( & calendar[i]);


}
}// Function to display the calendar
void display(struct Day * calendar, int size) {
printf("\nWeek's Activity Details:\n");
for (int i = 0; i < size; i++) {
printf("Day %d:\n", i + 1);
printf("Day Name: %s\n", calendar[i].dayName);
printf("Date: %d\n", calendar[i].date);
printf("Activity: %s\n", calendar[i].activity);
printf("\n");
}
}// Function to free the dynamically allocated memory
void freeMemory(struct Day * calendar, int size) {
for (int i = 0; i < size; i++) {
free(calendar[i].dayName);
free(calendar[i].activity);
}
}int main() {
int size;
printf("Enter the number of days in the week:");
scanf("%d", & size);// Dynamically allocate memory for the calendar
struct Day * calendar = (struct Day * ) malloc(sizeof(struct Day) * size);// Check if memory
allocation is successful
if (calendar == NULL) {
printf("Memory allocation failed. Exiting program.\n");
return 1;
}// Read and display the calendar

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

read(calendar, size);
display(calendar, size);// Free the dynamically allocated memory
freeMemory(calendar, size);// Free the memory allocated for the calendar array
free(calendar);
return 0;
}
OUTPUT
Enter the number of days in the week: 7Enter details for Day 1:
Enter the day name: Sunday
Enter the date: 1
Enter the activity for the day: Learning Enter details for Day 2:
Enter the day name: Monday
Enter the date: 2
Enter the activity for the day: CodingEnter details for Day 3:
Enter the day name: Tuesday
Enter the date: 3
Enter the activity for the day: TestingEnter details for Day 4:
Enter the day name: Wednesday
Enter the date: 4
Enter the activity for the day: DebuggingEnter details for Day 5:
Enter the day name: Thrusday
Enter the date: 5
Enter the activity for the day: PublishingEnter details for Day 6:
Enter the day name: Friday
Enter the date: 6
Enter the activity for the day: MarketingEnter details for Day 7:
Enter the day name: Saturday

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

Enter the date: 7


Enter the activity for the day: EarningWeek's Activity Details:
Day 1:
Day Name: Sunday
Date: 1
Activity: LearningDay 2:
Day Name: Monday
Date: 2
Activity: CodingDay 3:
Day Name: Tuesday
Date: 3
Activity: TestingDay 4:
Day Name: Wednesday
Date: 4
Activity: DebuggingDay 5:
Day Name: Thrusday
Date: 5
Activity: PublishingDay 6:
Day Name: Friday
Date: 6
Activity: MarketingDay 7:
Day Name: Saturday
Date: 7
Activity: Earning

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

2) Design, Develop and Implement a Program in C for the following operations on Strings

1)Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
2)Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR
with REP if PAT exists in STR. Report suitable messages in case PAT does not exist in STR

#include<sdio.h>
void main()
{
char STR[100],PAT[100],REP[100],ans[100];
int i,j,c,m,k,flag=0;
printf("\nEnter the MAIN string: \n");
gets(STR);
printf("\nEnter a PATTERN string: \n");
gets(PAT);
printf("\nEnter a REPLACE string: \n");
gets(REP);
i = m = c = j = 0;
while ( STR[c] != '\0')
{
// Checking for Match
if (STR[m] == PAT[i])
{
i++;
m++;
if ( PAT[i] == '\0')
{
//copy replace string in ans string

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

for(k=0; REP[k] != '\0';k++,j++)


{
ans[j] = REP[k];
flag=1;
}
i=0;
c=m;
}
}
else //mismatch
{
ans[j] = STR[c];
j++;
c++;
m = c;
i=0;
}
}
if(flag==0)
{
printf("Pattern doesn't found!!!");
}
else
{
ans[j] = '\0';
printf("\nThe RESULTANT string is:%s\n" ,ans);
}

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

}
Output
linux:~/dslab # gedit string.c
linux:~/dslab # cc string.c
linux:~/dslab # ./a.out
Enter the MAIN string: good
morning
Enter a PATTERN string:
morning
Enter a REPLACE string:
evening
The RESULTANT string is: good evening
linux:~/dslab # ./a.out Enter
the MAIN string: hi SJCIT
Enter a PATTERN string: NICE

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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).
1. Push an Element on to Stack
2. Pop an Element from Stack
3. Demonstrate how Stack can be used to check Palindrome
4. Demonstrate Overflow and Underflow situations on Stack
5. Display the status of Stack
6. Exit

#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\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)

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
pali();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}
return 0;
}
void push() //Inserting element into the stack
{
int item,n;
if(top==(max_size-1))
{
printf("\nStack Overflow:");
}
else

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

{
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 ,length = 0;
int num[len],rev[len],i=0;
while(top!=-1)
{
digit= stack[top];
num[i]=digit;
top--;
i++;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

}
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]);
}
printf("check for palindrome :\n");
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");

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

}
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]);
}
}
}
OUTPUT
linux:~/dslab # gedit stack.c
linux:~/dslab # cc stack.c
linux:~/dslab # ./a.out
--------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

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

Enter the element to be inserted: 5


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

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

4) Design, Develop and Implement a Program in C for converting an Infix Expression to


Postfix Expression. Program should support for both parenthesizedand free parenthesized
expressions with the operators: +, -, *, /, %(Remainder), ^(Power) and alphanumeric
operands.

#define SIZE 50 /* Size of Stack */

#include <ctype.h>

#include <stdio.h>

char s[SIZE];

int top = -1; /* Global declarations */

push(char elem) /* Function for PUSH operation */

s[++top] = elem;

char pop() /* Function for POP operation */

return (s[top--]);

int pr(char elem) /* Function for precedence */

switch (elem)

case '#':

return 0;

case '(':

return 1;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

case '+':

case '-':

return 2;

case '*':

case '/':

case '%':

return 3;

case '^':

return 4;

void main() /* Main Program *

char infx[50], pofx[50], ch, elem;

int i = 0, k = 0;

printf("\n\nEnter the Infix Expression ");

scanf("%s", infx);

push('#');

while ((ch = infx[i++]) != '\0')

if (ch == '(')

push(ch);

else if (isalnum(ch))
pofx[k++] = ch;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

else if (ch == ')')


{
while (s[top] != '(')
pofx[k++] = pop();
elem = pop(); /* Remove ( */
}
else /* Operator */
{
while (pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}

while (s[top] != '#') /* Pop from stack till empty */

pofx[k++] = pop();

pofx[k] = '\0'; /* Make pofx as valid string */

printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);

OUTPUT

linux:~/dslab # gedit intopost.c

linux:~/dslab # cc intopost.c

linux:~/dslab # ./a.out

Enter the Infix Expression:

(a+b)*c/d^5%1

Given Infix Expn: (a+b)*c/d^5%1

Postfix Expn: ab+c*d5^/1%

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define MAX 50
int stack[MAX];
char post[MAX];
int top= -1;
/*fUNCTION PROTOYPE */
void pushstack(int tmp);
void calculator(char c);
void main()
{
int i;
printf("Insert a postfix notation :: ");
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]);
}
}

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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;
case '*':
ans=b*a;
break;
case '/':
ans=b/a;
break;
case '^':

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

ans=pow(b,a);
break;
default:
ans=0;
}
top++;
stack[top]=ans;
}
OUTPUT:
Enter a valid postfix expression:
623+-382/+*2$3+
Result = 52

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

5b) Solving Tower of Hanoi problem with n disks


#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);
}

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 CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

6) Design, Develop and Implement a menu driven Program in C for the following
operations on Circular QUEUE of Characters (Array Implementation of Queue with
maximum size MAX)
1. Insert an Element on to Circular QUEUE
2. Delete an Element from Circular QUEUE
3. Demonstrate Overflow and Underflow situations on Circular QUEUE
4. Display the status of Circular QUEUE
5. Exit
#include<stdio.h>
#include<stdlib.h>
#define max 10
int q[10],front=0,rear= -1;
void main()
{
int ch;
/*FUNCTION PROTOTYPE */
void insert();
void delet();
void display();
printf("\nCircular Queue operations\n");
printf("1.insert\n2.delete\n3.display\n4.exit\n");
while(1)
{
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Invalid option\n");
}
}
}
void insert()
{
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
scanf("%d",&x);
if(rear==max-1&&front>0)
{
rear=0;
q[rear]=x;
}

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
exit(1);
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else if(front==max-1)
{
a=q[front];
front=0;
}
else

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

a=q[front++];
printf("Deleted element is:%d\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
exit(1);
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%d",q[i]);
for(j=front;j<=max-1;j++)
printf("\t%d",q[j]);
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
else
{
for(i=front;i<=rear;i++)
{
printf("\t%d",q[i]);
}
printf("\nrear is at %d\n",q[rear]);

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

printf("\nfront is at %d\n",q[front]);
}
printf("\n");
}

OUTPUT
linux:~/dslab #gedit cirQ.c
linux:~/dslab #gcc cirQ.c
linux:~/dslab # ./a.out
Circular Queue
operations 1.insert
2.delete
3.display
4.exit
Enter your choice:1
Enter element to be insert:10
Enter your choice:1
Enter element to be insert:20
Enter your choice:1
Enter element to be insert:30
Enter your
choice:3 10 20 30
rear is at 30
front is at 10
Enter your choice:2
Deleted element is:10
Enter your
choice:3

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

20 30
rear is at 30
front is at 20
Enter your choice:4

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

7) Design, Develop and Implement a menu driven Program in C for the following
operations on Singly Linked List (SLL) of Student Data with the fields: USN, Name,
Branch, Sem, PhNo
1. Create a SLL of N Students Data by using front insertion.
2. Display the status of SLL and count the number of nodes in it
3. Perform Insertion / Deletion at End of SLL
4. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
5. Exit

#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;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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;
}

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

else
{
create();
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;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

return 0;
}
else
{
first=temp->next;
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)

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

exit(0);
default:
printf("wrong choice\n");
}
}
}

OUTPUT
linux:~/dslab #gedit slink.c
linux:~/dslab #gcc slink.c
linux:~/dslab # ./a.out

---------------MENU----------------
------
1 – create a SLL 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 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

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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 CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

8) Design, Develop and Implement a menu driven Program in C for the following
operations on Doubly Linked List (DLL) of Employee Data with the fields: SSN, Name,
Dept, Designation, Sal, PhNo
1. Create a DLL of N Employees Data by using end insertion.
2. Display the status of DLL and count the number of nodes in it
3. Perform Insertion and Deletion at End of DLL
4. Perform Insertion and Deletion at Front of DLL
5. Demonstrate how this DLL can be used as Double Ended Queue
6. Exit

#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);

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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)

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

{
create();
h = temp;
temp1 = h;
}
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;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

}
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;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

temp=h;
if(temp->next==NULL)
{
free(temp);
h=NULL;
}
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");

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

case 6:
deletebeg();
break;
case 7:
exit(0);
default:
printf("wrong choice\n");
}
}
}
OUTPUT
linux:~/dslab #gedit dlink.c
linux:~/dslab #gcc dlink.c
linux:~/dslab # ./a.out
MENU--------------------
–Create a DLL of n emp
- Display from beginning
- Insert at end
- Delete at end
- Insert at beg
- Delete at beg
- 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

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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
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 CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

9) Design, Develop and Implement a Program in C for the following operations on Singly
Circular Linked List (SCLL) with header nodes
1)Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
2)Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result
in POLYSUM(x,y,z)

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<math.h>
typedef struct node
{
int expo,coef;
struct node *next;
}node;
/*FUNCTION PROTOTYPE*/
node * insert(node *,int,int);
node * create();
node * add(node *p1,node *p2);
int eval(node *p1);
void display(node *head);
node *insert(node*head,int expo1,int coef1)
{
node *p,*q;
p=(node *)malloc(sizeof(node));
p->expo=expo1;
p->coef=coef1;
p->next=NULL;
if(head==NULL)

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

{
head=p;
head->next=head;
return(head);
}
if(expo1>head->expo)
{
p->next=head->next;
head->next=p;
head=p;
return(head);
}
if(expo1==head->expo)
{
head->coef=head->coef+coef1;
return(head);
}
q=head;
while(q->next!=head&&expo1>=q->next->expo)
q=q->next;
if(p->expo==q->expo)
q->coef=q->coef+coef1;
else
{
p->next=q->next;
q->next=p;
}

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

return(head);
}
node *create()
{
int n,i,expo1,coef1;
node *head=NULL;
printf("\n\nEnter no of terms of polynomial==>");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\nEnter coef & expo==>");
scanf("%d%d",&coef1,&expo1);
head=insert(head,expo1,coef1);
}
return(head);
}
node *add(node *p1,node *p2)
{
node *p;
node *head=NULL;
printf("\n\n\nAddition of polynomial==>");
p=p1->next;
do
{
head=insert(head,p->expo,p->coef);
p=p->next;
}while(p!=p1->next);

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

p=p2->next;
do
{
head=insert(head,p->expo,p->coef);
p=p->next;
}while(p!=p2->next);
return(head);
}
int eval(node *head)
{
node *p;
int x,ans=0;
printf("\n\nEnter the value of x=");
scanf("%d",&x);
p=head->next;
do
{
ans=ans+p->coef*pow(x,p->expo);
p=p->next;
}while(p!=head->next);
return(ans);
}
void display(node *head)
{
node *p,*q;
int n=0;
q=head->next;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

p=head->next;
do
{
n++;
q=q->next;
}while(q!=head->next);
printf("\n\n\tThe polynomial is==>");
do
{
if(n-1)
{
printf("%dx^(%d) + ",p->coef,p->expo);
p=p->next;
}
else
{
printf(" %dx^(%d)",p->coef,p->expo);
p=p->next;
}
n--;
} while(p!=head->next);
}
void main()
{
int a,x,ch;
node *p1,*p2,*p3;
p1=p2=p3=NULL;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

while(1)
{
printf("\n\t----------------<< MENU >>---------------");
printf("\n\tPolynomial Operations :");
printf(" 1.Add");
printf("\n\t\t\t\t2.Evaluate");
printf("\n\t\t\t\t3.Exit");
printf("\n\t------------------------------------------- ");
printf("\n\n\n\tEnter your choice==>");
scanf("%d",&ch);
switch(ch)
{
case 1 :
p1=create();
display(p1);
p2=create();
display(p2);
p3=add(p1,p2);
display(p3);
break;
case 2 :
p1=create();
display(p1);
a=eval(p1);
printf("\n\nValue of polynomial=%d",a);
break;
case 3 :

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

exit(0);
break;
default :
printf("\n\n\t invalid choice");
break;
}
}
}
OUTPUT
linux:~/dslab #gedit poly.c
linux:~/dslab #gcc poly.c
linux:~/dslab # ./a.out
-----------------<< MENU >>---------------
Polynomial Operations :
1.Add 2.Evaluate
3.Exit
---------------------------------------------------
Enter your choice==>1
Enter no of terms of polynomial==>
3
Enter coef & expo==>
43
Enter coef & expo==>
22
Enter coef & expo==> 5 1
The polynomial is==>5x^(1) + 2x^(2) +
4x^(3)

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

Enter no of terms of polynomial==>3


Enter coef & expo==>
41
Enter coef & expo==>
3
2
Enter coef & expo==>
53
The polynomial is==>4x^(1) + 3x^(2) + 5x^(3)
Addition of polynomial==>
The polynomial is==>9x^(1) + 5x^(2) + 9x^(3)
Enter your choice==>2
Enter no of terms of polynomial==>3
Enter coef & expo==>
31
Enter coef & expo==> 4
2
Enter coef & expo==> 5 4
The polynomial is==>3x^(1) + 4x^(2) + 5x^(4)
Enter the value of x=2
Value of polynomial=102
Enter your choice==>3
exit

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

10) Design, Develop and Implement a menu driven Program in C for the following
operations on Binary Search Tree(BST) of Integers
1.Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
2.Traverse the BST in Inorder, Preorder and Post Order
3.Search the BST for a given element (KEY) and report the appropriate message
4.Exit
Program
#include <stdio.h>
#include <stdlib.h>
int flag=0;
typedef struct BST
{
int data;
struct BST *lchild,*rchild;
} node;
/*FUNCTION PROTOTYPE*/
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;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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;
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 :");

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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;
}
}

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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);
}
if (new_node->data > root->data)
{
if (root->rchild == NULL)
root->rchild = new_node;
else
insert(root->rchild, new_node);

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

}
}
/*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)

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

{
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);
printf("%d\t", temp->data);
}
}

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

OUTPUT
linux:~/dslab #gedit bst.c
inux:~/dslab #gcc bst.c
linux:~/dslab # ./a.out
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

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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
Enter your choice :4

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

11) Design, Develop and Implement a Program in C for the following operations on
Graph(G) of Cities
1. Create a Graph of N cities using Adjacency Matrix.
2. Print all the nodes reachable from a given starting node in a digraph 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;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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

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

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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

linux:~/dslab #gedit bfs.c

linux:~/dslab #gcc bfs.c

linux:~/dslab # ./a.out

Enter the number of vertices:5

Enter graph data in matrix

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

form: 0 1 0 1 0

10101

01010

10100

01000

1.BFS

2.DFS

3.Exit

1->2

2->3

3->4

2->5

Graph is connected

Enter the number of

vertices:5

Enter graph data in matrix

form: 0 1 0 1 0

10100

01010

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

10100

00000

1.BFS

2.DFS

3.Exit

1->2

2->3

3->4

Graph is not connected

Enter the number of vertices:5

Enter graph data in matrix

form: 0 1 1 0 0

00010

00000

00100

00100

1.BFS

2.DFS

3.Exit

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

Enter the starting vertex:1

The nodes which are reachable from

1: 2 3 4

Enter graph data in matrix

form: 0 1 1 0 0

00010

00000

00100

00100

1.BFS

2.DFS

3.Exit

Enter the starting

vertex:0 BFS is not

possible

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

12)Given a File of N employee records with a set K of Keys(4-digit) which uniquely


determine
the records in file F. Assume that file F is maintained in memory by a Hash Table(HT) of
m memory locations with L as the set of memory addresses (2-digit) of locations in HT.Let
the keys in K and addresses in L are Integers. Design and develop a program in C that
uses Hash function H: K → L as H(K)=K mod m (remainder 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

/*FUNCTION PROTOTYPE */

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;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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");

if(count == MAX)

printf("\n Hash table is full");

display(a);

exit(1);

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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++;

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

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 CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

OUTPUT

linux:~/dslab #gedit hash.c

linux:~/dslab #gcc hash.c

linux:~/dslab # ./a.out

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 Data Structures Lab manual

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)

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

0 1.Display ALL

2.Filtered

Display 2

the hash table is

0 1398

34 1234

48 2548

56 3256

981298

991299

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

DATA STRUCTURES VIVA QUESTIONS

1) What is a Data Structure?


Ans) A Data Structure is a data object together with the relationships that exists among
theinstances & among the individual elements that compose an instance.
2) Types of Data Structures and give examples?
Ans) There are two types of Data Structures:
1.Linear Data Structures:
A data structure is said to be linear if the elements form asequence. It is sequential and continues
in nature i.e. access the data in sequentialmanner.In linear data structure we can not insert an
item in middle place and it maintains a linearrelationship between its elementsegs: Array, Linked
list, Stack, Queue, Dequeue etc.
2.Non Linear Data Structures:
A data structure is said to be non-linear if elements do notform a sequence. (Not sequential).
It does not maintain any linear relationship between their elements. Every data item is attached toseveral other data
items in a way that is specific for reflecting relationships. The data items arenot arranged in a sequential structure.
egs: Trees, Graphs.
[A data structure is linear if every item is related with next and previous item and it is nonlinear if it is
attach with many of the items in specific ways to reflect relationship.]
3) What is a Singly Linked List?
Ans) Singly Linked List is a Sequence of dynamically allocated Storage elements, eachelement
of which contains a pointer to its successor. A pointer to the first element of thelist is called as
head and a pointer to the last element of the list is called as tail used tokeep track of the list
elements.
4) What is Doubly Linked List?
Ans) In Doubly Linked List each element contains two pointers: One Pointer points
to itssuccessor and another to its predecessor (previous element).It is also called as
two waylinked list (traversing can be done in both directions).

STACKS: (LIFO DATA STRUCTURE)


5) What is a Stack? (LIFO Data Structure)
Ans) Stack is an ordered collection of items into which items can be inserted and deleted from

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

only one end called as “Top” of the Stack. It is also called as LIFO list.(Last In FirstOut)
6) What is Stack Underflow?
Ans) Is Stack is empty and POP operation is performed it is not possible to delete the items.This
situation is called Stack Underflow.
7) What is Stack Overflow?
Ans) If Stack is full and PUSH operation is performed it is not possible to insert or Push thenew
items into the stack. This situation is called Stack Overflow.
8) What are the Applications of Stack?
Ans) i) Stacks are used to convert Infix expression into Postfix.ii) Stacks are used to Evaluate
Postfix Expression.iii) Stacks are used in recursion etc.
9) What is the use of Postfix expressions?
Ans) Postfix Expressions are easy to evaluate as postfix expressions does not make
use ofoperator precedence not does it require the use of parenthesis.
QUEUES: (FIFO DATA STRUCTURE)
10) What is a Queue?
Ans) It is an ordered collection of items into which items can be inserted from one end calledas
REAR end and items are deleted from other end called as FRONT end of the Queue. Itis also
called as FIRST IN FIRST OUT (FIFO) LIST).
11) What are the applications of Queues?
Ans) i) Queues are used in Breadth First Traversal of a Tree.ii) Queues are used in
implementation of Scheduling algorithms of Operating Systems.
12) What is a Circular Queue?
Ans) In Circular Queue, the first position of the array is kept behind the last position of thearray.
13) Differentiate Linear Queue and Circular Queue?
Ans) In Linear Queue once the queue is full and the deletion is performed, even if first positionis
free(vacant) it is not possible to insert the item in that position whereas in CircularQueue it is
possible since the first position is kept behind the last position.
14) What is Dequeue? (Double Ended Queue)
Ans) In Double Ended Queue insertion and deletion are possible from both the ends.

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

TREES:

15) What is a Tree?


Ans) Tree is a finite non-empty set of nodes with the following properties:i)A designated node of
the set is called as root of the tree and ii)The remaining nodes are partitioned into n>=0 subsets,
each of which is a tree.
Degree of a node:
The number of sub trees attached to a node is called degree of that node and the maximum degree of any node in
a tree is called
degree of that tree.[Note: In a general tree degree of a node is not fixed]
Nodes that have degree zero are calledLeaf or Terminal Nodes
. Consequently, the other nodes are referred to as Non-Terminals.
The Level of a node is defined by letting the root be at level o or 1.The height or depth of a tree
is defined to be the maximum level of any node in the tree

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

Data Structures: The logical or mathematical model of a particular organization of data is called data
structures. Data structures is the study of logical relationship existing between individual data elements,
the way the data is organized in the memory and the efficient way of storing, accessing and manipulating
the data elements. Choice of a particular data model depends on two considerations: it must be rich
enough in structure to mirror the actual relationships of the data in the real world. On the other hand, the
structure should be simple enough that one can effectively process the data when necessary.

Data Structures can be classified as: Primitive data structures Non-Primitive data structures. Primitive
data structures are the basic data structures that can be directly manipulated/operated by machine
instructions. Some of these are character, integer, real, pointers etc. Non-primitive data structures are
derived from primitive data structures, they cannot be directly manipulated/operated by machine
instructions, and these are group of homogeneous or heterogeneous data items. Some of these are Arrays,
stacks, queues, trees, graphs etc.

The following operations play major role in the processing of data. i) Traversing. ii) Searching. iii)
Inserting. iv) Deleting. v) Sorting. vi) Merging STACKS: A stack is an ordered collection of items into
which new items may be inserted and from which items may be deleted at the same end, called the TOP
of the stack. A stack is a non-primitive linear data structure. As all the insertion and deletion are done
from the same end, the first element inserted into the stack is the last element deleted from the stack and
the last element inserted into the stack is the first element to be deleted. Therefore, the stack is called
Last-In First-Out (LIFO) data structure.

QUEUES: A queue is a non-primitive linear data structure. Where the operation on the queue is based on
First-In-FirstOut FIFO process — the first element in the queue will be the first one out. This is
equivalent to the requirement that whenever an element is added, all elements that were added before
have to be removed before the new element can be removed. For inserting elements into the queue are
done from the rear end and deletion is done from the front end, we use external pointers called as rear and
front to keep track of the status of the queue. During insertion, Queue Overflow condition has to be
checked. Likewise during deletion, Queue Underflow condition is checked.

LINKED LIST: Disadvantages of static/sequential allocation technique: 1) If an item has to be deleted


then all the following items will have to be moved by one allocation. Wastage of time. 2) Inefficient
memory utilization. 3) If no consecutive memory (free) is available, execution is not possible. Linear
Linked Lists Types of Linked lists: 1) Single Linked lists 2) Circular Single Linked Lists 3) Double
Linked Lists 4) Circular Double Linked Lists. NODE: Each node consists of two fields. Information
(info) field and next address(next) field. The info field consists of actual information/data/item that has to

DEPT.OF CSE ,FETW,SUK,KLB


DATA STRUCUTRES LAB MANUAL(21CSL36)

be stored in a list. The second field next/link contains the address of the next node. Since next field
contains the address, It is of type pointer.Here the nodes in the list are logically adjacent to each other.
Nodes that are physically adjacent need not be logically adjacent in the list. The entire linked list is
accessed from an external pointer FIRST that points to (contains the address of) the first node in the list.
(By an “external” pointer, we mean, one that is not included within a node. Rather its value can be
accessed directly by referencing a variable). The list containing 4 items/data 10, 20, 30 and 40 is shown
below. INFO NEXT NODE FIRST 10 INFO NEXT 20 INFO NEXT 30 INFO NEXT TTT 40 INFO
NEXT TT NODE1 NODE2 NODE3 NODE4

TREES: Definition: A data structure which is accessed beginning at the root node. Each node is either a
leaf or an internal node. An internal node has one or more child nodes and is called the parent of its child
nodes. All children of the same node are siblings. Contrary to a physical tree, the root is usually depicted
at the top of the structure, and the leaves are depicted at the bottom. A tree can also be defined as a
connected, acyclic di-graph.

DEPT.OF CSE ,FETW,SUK,KLB

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