DSA LAB MANUAL(NEW EDITED)
DSA LAB MANUAL(NEW EDITED)
UNIVERSITY
JNANA SANGAMA, BELGAVI-590018, KARNATAKA
PREPARED BY:
INDHUMATHI R (ASST.PROF DEPT OF DS (CSE), KNSIT)
PROGRAM LISTS:
1 Develop a Program in C for the following:
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
4 Develop a Program in C for converting an Infix Expression to Postfix Expression. Program
should support for both parenthesized and free parenthesized expressions with the
operators: +, -, *, /, % (Remainder), ^ (Power) and alphanumeric operands.
5 Develop 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
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
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations
7 Develop a menu driven Program in C for the following operations on Singly Linked List (SLL)
of Student Data with the fields: USN, Name, Programme, Sem, PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit
2
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
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) = 6x 2 y 2 z-4yz 5 +3x 3 yz+2xy 5 z-2xyz 3
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
3
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
PROGRAM 1
#include <stdio.h>
#include <stdlib.h>
struct Day {
char *dayName;
int date;
char *activity;
};
4
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
int main() {
int size;
5
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
if (calendar == NULL) {
printf("Memory allocation failed. Exiting program.\n");
return 1;
}
read(calendar, size);
display(calendar, size);
freeMemory(calendar, size);
free(calendar);
return 0;
}
OUTPUT:
Enter the number of days in the week: 7
6
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
7
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
Activity: Learning
Day 2:
Day Name: Monday
Date: 2
Activity: Coding
Day 3:
Day Name: Tuesday
Date: 3
Activity: Testing
Day 4:
Day Name: Wednesday
Date: 4
Activity: Debugging
Day 5:
Day Name: Thrusday
Date: 5
Activity: Publishing
Day 6:
Day Name: Friday
Date: 6
Activity: Marketing
Day 7:
Day Name: Saturday
Date: 7
8
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
Activity: Earning
9
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
PROGRAM 2
#include<stdio.h>
10
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
}
else
{
res[j] = str[c];
j++;
c++;
m = c;
i = 0;
}
}
res[j] = '\0';
}
void main()
{
printf("Enter the main string:");
gets(str);
printf("\nEnter the pat string:");
gets(pat);
printf("\nEnter the replace string:");
gets(rep);
printf("\nThe string before pattern match is:\n %s", str);
stringmatch();
if (flag == 1)
printf("\nThe string after pattern match and replace is: \n %s ", res);
else
printf("\nPattern string is not found");
}
11
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
OUTPUT:
**************************OUTPUT 1*******************************
**************************OUTPUT 2*******************************
12
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
PROGRAM 3
>>Develop a menu driven Program in C for the following operations on STACK of Integers (Array
Implementation of Stack with maximum size MAX)
f. Exit
Support the program with appropriate functions for each of the above operations
#include<stdio.h>
#include<stdlib.h>
#define MAX 3
int s[MAX];
int top = -1;
void main()
{
int choice, item;
while (1)
{
printf("\n\n\n\n-----------Menu----------- : ");
13
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
14
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
}
}
}
top = top + 1;
s[top] = item;
}
int pop()
{
int item;
if (top == -1)
{
printf("\n-----------Stack underflow-----------");
return -1;
}
item = s[top];
top = top - 1;
return item;
}
void display()
15
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
{
int i;
if (top == -1)
{
printf("\n-----------Stack is empty-----------");
return;
}
printf("\nStack elements are:\n ");
for (i = top; i >= 0; i--)
printf("| %d |\n", s[i]);
}
void palindrome()
{
int flag = 1, i;
printf("\nStack content are:\n");
for (i = top; i >= 0; i--)
printf("| %d |\n", s[i]);
16
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
}
if (flag == 1)
{
printf("\nIt is palindrome number");
}
else
{
printf("\nIt is not a palindrome number");
}
}
OUTPUT:
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
17
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
18
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
19
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
=>4.Display
=>5.Exit
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
20
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
=>4.Display
=>5.Exit
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
21
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
=>4.Display
=>5.Exit
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
It is palindrome number
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
22
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
=>4.Display
=>5.Exit
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
23
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
24
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
-----------Menu----------- :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
25
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
PROGRAM 4
>>Develop a Program in C for converting an Infix Expression to Postfix Expression. Program should
support for both parenthesized and free parenthesized expressions with the operators: +, -, *, /, %
(Remainder), ^ (Power) and alphanumeric operands.
#include<stdio.h>
#include<stdlib.h>
void evaluate();
void push(char);
char pop();
int prec(char);
void main()
{
printf("\n Enter the valid infix expression:");
scanf("%s", infix);
evaluate();
printf("\nThe entered infix expression is :\n %s \n", infix);
printf("\nThe corresponding postfix expression is :\n %s \n", postfix);
}
void evaluate()
{
int i = 0, j = 0;
char symb, temp;
push('#');
26
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
case ')':
temp = pop();
while (temp != '(')
{
postfix[j] = temp;
j++;
temp = pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
case '$':
while (prec(stack[top]) >= prec(symb))
{
temp = pop();
postfix[j] = temp;
27
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
j++;
}
push(symb);
break;
default:
postfix[j] = symb;
j++;
}
}
while (top > 0)
{
temp = pop();
postfix[j] = temp;
j++;
}
postfix[j] = '\0';
}
char pop()
{
char item;
item = stack[top];
top = top - 1;
return item;
28
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
case '(':
case ')':
p = 0;
break;
case '+':
case '-':
p = 1;
break;
case '*':
case '/':
case '%':
p = 2;
break;
case '^':
case '$':
p = 3;
29
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
break;
}
return p;
}
OUTPUT:
30
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
PROGRAM 5
Develop a Program in C for the following Stack Applications
5a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int pop()
{
int item;
item = s[top];
top = top - 1;
return item;
}
void main()
31
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
{
printf("\nEnter a valid postfix expression:\n");
scanf("%s", postfix);
for (i = 0; postfix[i] != '\0'; i++)
{
symb = postfix[i];
if (isdigit(symb))
{
push(symb - '0');
}
else
{
op2 = pop();
op1 = pop();
switch (symb)
{
case '+':
push(op1 + op2);
break;
case '-':
push(op1 - op2);
break;
case '*':
push(op1 * op2);
break;
case '/':
push(op1 / op2);
break;
case '%':
push(op1 % op2);
32
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
break;
case '$':
case '^':
push(pow(op1, op2));
break;
default:
push(0);
}
}
}
res = pop();
printf("\n Result = %d", res);
}
OUTPUT:
Enter a valid postfix expression:
623+-382/+*2$3+
Result = 52
33
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
#include <stdio.h>
OUTPUT:
Enter the number of discs: 3
Move disc 1 from A to C
Move disc 2 from A to B
Move disc 1 from C to B
Move disc 3 from A to C
Move disc 1 from B to A
Move disc 2 from B to C
Move disc 1 from A to C
34
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
PROGRAM 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)
e. Exit
Support the program with appropriate functions for each of the above operations
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
char circular_queue[MAX];
int front = -1, rear = -1;
int isEmpty()
{
if (front == -1 && rear == -1)
return 1;
else
return 0;
}
int isFull()
{
if ((rear + 1) % MAX == front)
return 1;
else
return 0;
35
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
void deleteElement()
{
if (isEmpty())
{
printf("Circular Queue Underflow\n");
return;
}
else if (front == rear)
{
front = rear = -1;
36
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
}
else
{
front = (front + 1) % MAX;
}
}
void display()
{
int i;
if (isEmpty())
{
printf("Circular Queue is empty\n");
return;
}
printf("Circular Queue elements: ");
i = front;
do
{
printf("%c ", circular_queue[i]);
i = (i + 1) % MAX;
}
while (i != (rear + 1) % MAX);
printf("\n");
}
int main()
{
int choice;
char element;
37
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
do
{
printf("\n\n---- Circular Queue Menu ----\n");
printf("1. Insert an Element\n");
printf("2. Delete an Element\n");
printf("3. Display Circular Queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter element to be inserted: ");
scanf(" %c", &element);
insertElement(element);
break;
case 2:
deleteElement();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please enter a valid option.\n");
}
}
38
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
while(choice != 4);
return 0;
}
OUTPUT:
---- Circular Queue Menu ----
1. Insert an Element
2. Delete an Element
3. Display Circular Queue
4. Exit
Enter your choice: 1
Enter element to be inserted: A
39
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
40
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
PROGRAM 7
Develop a menu driven Program in C for the following operations on Singly Linked List (SLL) of
Student Data with the fields: USN, Name, Programme, Sem, PhNo
e. Exit
#include<stdio.h>
#include<stdlib.h>
struct node
{
char usn[25], name[25], branch[25];
int sem;
long int phone;
struct node * link;
};
typedef struct node * NODE;
NODE create()
{
NODE snode;
snode = (NODE) malloc(sizeof(struct node));
41
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
if (snode == NULL)
{
printf("\nMemory is not available");
exit(1);
}
printf("\nEnter the usn,Name,Branch, sem,PhoneNo of the student:");
scanf("%s %s %s %d %ld", snode -> usn, snode -> name, snode -> branch, & snode ->
sem, & snode -> phone);
snode -> link = NULL;
count++;
return snode;
}
NODE insertfront()
{
NODE temp;
temp = create();
if (start == NULL)
{
return temp;
}
NODE deletefront()
{
NODE temp;
if (start == NULL)
42
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
{
printf("\nLinked list is empty");
return NULL;
}
NODE insertend()
{
NODE cur, temp;
temp = create();
if (start == NULL)
{
return temp;
}
cur = start;
43
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
NODE deleteend()
{
NODE cur, prev;
if (start == NULL)
{
printf("\nLinked List is empty");
return NULL;
}
prev = NULL;
cur = start;
while (cur -> link != NULL)
{
prev = cur;
44
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
printf("\nThe student node with the usn:%s is deleted", cur -> usn);
free(cur);
prev -> link = NULL;
count--;
return start;
}
void display()
{
NODE cur;
int num = 1;
if (start == NULL)
{
45
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
void stackdemo()
{
int ch;
while (1)
{
printf("\n--------Stack Demo using SLL--------\n");
printf("\n1:Push operation \n2: Pop operation \n3: Display \n4:Exit \n");
printf("\nEnter your choice for stack demo:");
scanf("%d", & ch);
switch (ch)
{
case 1:
start = insertfront();
break;
case 2:
start = deletefront();
break;
case 3:
display();
break;
default:
return;
}
}
return;
}
46
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
int main()
{
int ch, i, n;
while (1)
{
printf("\n--------Menu--------");
printf("\nEnter your choice for SLL operation \n");
printf("\n1:Create SLL of Student Nodes");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n5:Stack Demo using SLL(Insertion and Deletion at Front)");
printf("\n6:Exit \n");
printf("\nEnter your choice:");
scanf("%d", & ch);
switch (ch)
{
case 1:
printf("\nEnter the no of students: ");
scanf("%d", & n);
for (i = 1; i <= n; i++)
start = insertfront();
break;
case 2:
display();
break;
case 3:
47
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
start = insertend();
break;
case 4:
start = deleteend();
break;
case 5:
stackdemo();
break;
case 6:
exit(0);
default:
printf("\nPlease enter the valid choice");
}
}
}
OUTPUT:
--------Menu--------
Enter your choice for SLL operation
48
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
--------Menu--------
Enter your choice for SLL operation
49
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
--------Menu--------
Enter your choice for SLL operation
50
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
3426527765
--------Menu--------
Enter your choice for SLL operation
--------Menu--------
Enter your choice for SLL operation
51
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
--------Menu--------
Enter your choice for SLL operation
--------Menu--------
Enter your choice for SLL operation
52
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
6:Exit
--------Menu--------
Enter your choice for SLL operation
1:Push operation
2: Pop operation
3: Display
4:Exit
53
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
6587594335
1:Push operation
2: Pop operation
3: Display
4:Exit
1: Push operation
2: Pop operation
3: Display
4: Exit
54
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
5
9869754354
1:Push operation
2: Pop operation
3: Display
4:Exit
55
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
1:Push operation
2: Pop operation
3: Display
4:Exit
1: Push operation
2: Pop operation
3: Display
4: Exit
--------Menu--------
Enter your choice for SLL operation
56
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
6:Exit
Enter your choice:6
57
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
PROGRAM 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
c. Perform Insertion and Deletion at End of DLL d. Perform Insertion and Deletion at Front of DLL
f. Exit
#include<stdio.h>
#include<stdlib.h>
struct node
{
char ssn[25], name[25], dept[10], designation[25];
int sal;
long int phone;
struct node * llink;
struct node * rlink;
};
typedef struct node * NODE;
NODE create()
{
NODE enode;
enode = (NODE) malloc(sizeof(struct node));
if (enode == NULL)
58
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
{
printf("\n Running out of memory ");
exit(0);
}
printf("\n Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:
\n");
scanf("%s %s %s %s %d %ld", enode -> ssn, enode -> name, enode -> dept, enode ->
designation, & enode -> sal, & enode -> phone);
enode -> llink = NULL;
enode -> rlink = NULL;
count++;
return enode;
}
NODE insertfront()
{
NODE temp;
temp = create();
if (first == NULL)
{
return temp;
}
temp -> rlink = first;
first -> llink = temp;
return temp;
}
void display()
{
NODE cur;
int nodeno = 1;
59
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
cur = first;
if (cur == NULL)
printf("\nNo Contents to display in DLL ");
while (cur != NULL)
{
printf("\nENode:%d|SSN:%s| |Name:%s| |Department:%s| |Designation:%s| |Salary:%d|
|Phone no:%ld|", nodeno, cur -> ssn, cur -> name, cur -> dept, cur -> designation, cur -> sal,
cur -> phone);
cur = cur -> rlink;
nodeno++;
}
printf("\nNo of employee nodes is %d", count);
}
NODE deletefront()
{
NODE temp;
if (first == NULL)
{
printf("\nDoubly Linked List is empty ");
return NULL;
}
if (first -> rlink == NULL)
{
printf("\nThe employee node with the ssn:%s is deleted ", first -> ssn);
free(first);
count--;
return NULL;
}
temp = first;
first = first -> rlink;
60
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
NODE insertend()
{
NODE cur, temp;
temp = create();
if (first == NULL)
{
return temp;
}
cur = first;
while (cur -> rlink != NULL)
{
cur = cur -> rlink;
}
NODE deleteend()
{
61
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
prev = NULL;
cur = first;
62
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
void deqdemo()
{
int ch;
while (1)
{
printf("\nDemo Double Ended Queue Operation ");
printf("\n1:InsertQueueFront\n 2: DeleteQueueFront\n 3:InsertQueueRear\n
4:DeleteQueueRear\n 5:DisplayStatus\n 6: Exit \n");
scanf("%d", & ch);
switch (ch)
{
case 1:
first = insertfront();
break;
case 2:
first = deletefront();
break;
case 3:
first = insertend();
break;
case 4:
first = deleteend();
break;
case 5:
display();
break;
default:
return;
63
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
}
}
}
void main()
{
int ch, i, n;
while (1)
{
printf("\n\n--------Menu--------");
printf("\n1:Create DLL of Employee Nodes ");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n5:InsertAtFront");
printf("\n6:DeleteAtFront");
printf("\n7:Double Ended Queue Demo using DLL ");
printf("\n8:Exit \n");
printf("\nPlease enter your choice: ");
scanf("%d", & ch);
switch (ch)
{
case 1:
printf("\nEnter the no of Employees: ");
scanf("%d", & n);
for (i = 1; i <= n; i++)
first = insertend();
break;
64
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
case 2:
display();
break;
case 3:
first = insertend();
break;
case 4:
first = deleteend();
break;
case 5:
first = insertfront();
break;
case 6:
first = deletefront();
break;
case 7:
deqdemo();
break;
case 8:
exit(0);
default:
printf("\nPlease Enter the valid choice ");
}
}
65
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
OUTPUT:
--------Menu--------
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 1
Enter the no of Employees: 2
66
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
--------Menu--------
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 2
--------Menu--------
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 3
67
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
Meeting
Manager
30000
8237462936
--------Menu--------
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 2
--------Menu--------
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
68
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
--------Menu--------
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
69
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
No of employee nodes is 4
--------Menu--------
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 4
--------Menu--------
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 6
--------Menu--------
1:Create DLL of Employee Nodes
70
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 2
--------Menu--------
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
Please enter your choice: 7
71
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
5:DisplayStatus
6: Exit
Please enter your choice: 2
72
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
Please enter your choice: 6
--------Menu--------
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit
73
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
PROGRAM 9
Develop a Program in C for the following operationson Singly Circular Linked List (SCLL) with header
nodes
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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct node
{
int coef;
int xexp, yexp, zexp;
struct node * link;
};
typedef struct node * NODE;
NODE getnode()
{
NODE x;
x = (NODE) malloc(sizeof(struct node));
if (x == NULL)
{
printf("Running out of memory \n");
return NULL;
74
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
}
return x;
}
NODE attach(int coef, int xexp, int yexp, int zexp, NODE head)
{
NODE temp, cur;
temp = getnode();
temp -> coef = coef;
temp -> xexp = xexp;
temp -> yexp = yexp;
temp -> zexp = zexp;
cur = head -> link;
while (cur -> link != head)
{
cur = cur -> link;
}
cur -> link = temp;
temp -> link = head;
return head;
}
75
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
printf("\n\t\tCoef = ");
scanf("%d", & coef);
printf("\n\t\tEnter Pow(x) Pow(y) and Pow(z): ");
scanf("%d", & xexp);
scanf("%d", & yexp);
scanf("%d", & zexp);
head = attach(coef, xexp, yexp, zexp, head);
}
return head;
}
76
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
77
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
case 0:
if (a -> yexp > b -> yexp)
{
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
else if (a -> yexp < b -> yexp)
{
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
}
else if (a -> zexp > b -> zexp)
{
78
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
else if (a -> zexp < b -> zexp)
{
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
}
case 1:
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
break;
}
if (a -> yexp != 0 || b -> yexp != 0)
{
switch (COMPARE(a -> yexp, b -> yexp))
{
case -1:
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
case 0:
if (a -> zexp > b -> zexp)
{
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
79
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
break;
}
else if (a -> zexp < b -> zexp)
{
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
}
case 1:
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
break;
}
if (a -> zexp != 0 || b -> zexp != 0)
{
switch (COMPARE(a -> zexp, b -> zexp))
{
case -1:
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
break;
case 1:
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
break;
}
break;
}
80
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
}
}
while (a != head1)
{
head3 = attach(a -> coef, a -> xexp, a -> yexp, a -> zexp, head3);
a = a -> link;
}
while (b != head2)
{
head3 = attach(b -> coef, b -> xexp, b -> yexp, b -> zexp, head3);
b = b -> link;
}
return head3;
}
void main()
{
NODE head, head1, head2, head3;
int res, ch;
head = getnode();
head1 = getnode();
head2 = getnode();
head3 = getnode();
while (1)
81
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
{
printf("\n--------Menu--------");
printf("\n1.Represent and Evaluate a Polynomial P(x,y,z)");
printf("\n2.Find the sum of two polynomials POLY1(x,y,z)");
printf("\nEnter your choice:");
scanf("%d", & ch);
switch (ch)
{
case 1:
printf("\n----Polynomial evaluation P(x,y,z)----\n");
head = read_poly(head);
printf("\nRepresentation of Polynomial for evaluation: \n");
display(head);
res = poly_evaluate(head);
printf("\nResult of polynomial evaluation is : %d \n", res);
break;
case 2:
printf("\nEnter the POLY1(x,y,z): \n");
head1 = read_poly(head1);
printf("\nPolynomial 1 is: \n");
display(head1);
82
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
display(head3);
break;
case 3:
exit(0);
}
}
}
OUTPUT:
--------Menu--------
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z)
Enter your choice: 1
83
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
--------Menu--------
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z)
Enter your choice: 2
84
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
Polynomial 1 is:
6x^4y^4z^4 + 3x^4y^3z^1 + 5x^0y^1z^1 + 10x^0y^1z^0 + 5x^0y^0z^0
Polynomial 2 is:
8x^4y^4z^4 + 4x^4y^2z^1 + 30x^0y^1z^0 + 20x^0y^0z^1 + 3x^0y^0z^0
--------Menu--------
1.Represent and Evaluate a Polynomial P(x,y,z)
85
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
86
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
PROGRAM 10
Develop a menu driven Program in C for the following operations on Binary Search Tree (BST) of
Integers .
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit
87
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
#include<stdio.h>
#include<stdlib.h>
struct BST
{
int data;
struct BST * lchild;
struct BST * rchild;
};
typedef struct BST * NODE;
NODE create()
{
NODE temp;
temp = (NODE) malloc(sizeof(struct BST));
printf("\nEnter The value: ");
scanf("%d", & temp -> data);
88
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
89
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
cur = root;
while (cur != NULL)
{
if (cur -> data == key)
{
printf("\nKey element is present in BST ");
return;
}
if (key < cur -> data)
cur = cur -> lchild;
else
cur = cur -> rchild;
}
printf("\nKey element is not found in the BST ");
}
90
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
void main()
{
int ch, key, val, i, n;
NODE root = NULL, newnode;
while (1)
{
printf("\n-------BST MENU-------");
printf("\n1.Create a BST ");
printf("\n2.Search ");
printf("\n3.BST Traversals: ");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", & ch);
switch (ch)
91
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
{
case 1:
printf("\nEnter the number of elements: ");
scanf("%d", & n);
for (i = 1; i <= n; i++)
{
newnode = create();
if (root == NULL)
root = newnode;
else
insert(root, newnode);
}
break;
case 2:
if (root == NULL)
printf("\nTree Is Not Created ");
else
{
printf("\nThe Preorder display: ");
preorder(root);
printf("\nThe Inorder display: ");
inorder(root);
printf("\nThe Postorder display: ");
postorder(root);
}
break;
case 3:
search(root);
break;
92
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
case 4:
exit(0);
}
}
}
OUTPUT:
-------BST MENU-------
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
Enter your choice: 1
-------BST MENU-------
1.Create a BST
93
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
2.Search
3.BST Traversals:
4.Exit
Enter your choice: 3
-------BST MENU-------
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
Enter your choice: 2
-------BST MENU-------
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
Enter your choice: 2
-------BST MENU-------
94
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
Enter your choice: 4
95
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
PROGRAM 11
Develop a Program in C for the following operations on Graph(G) of Cities
b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS method
#include<stdio.h>
#include<stdlib.h>
void bfs(int v)
{
int i, cur;
visited[v] = 1;
q[++rear] = v;
while (front != rear)
{
cur = q[++front];
for (i = 1; i <= n; i++)
{
if ((a[cur][i] == 1) && (visited[i] == 0))
{
q[++rear] = i;
visited[i] = 1;
printf("%d ", i);
}
}
96
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
}
}
void dfs(int v)
{
int i;
visited[v] = 1;
s[++top] = v;
for (i = 1; i <= n; i++)
{
if (a[v][i] == 1 && visited[i] == 0)
{
printf("%d ", i);
dfs(i);
}
}
}
int main()
{
97
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
printf("\n==>1. BFS: Print all nodes reachable from a given starting node");
printf("\n==>2. DFS: Print all nodes reachable from a given starting node");
printf("\n==>3:Exit");
printf("\nEnter your choice: ");
scanf("%d", & ch);
switch (ch)
{
case 1:
printf("\nNodes reachable from starting vertex %d are: ", start);
bfs(start);
for (i = 1; i <= n; i++)
{
if (visited[i] == 0)
printf("\nThe vertex that is not reachable is %d", i);
}
break;
case 2:
printf("\nNodes reachable from starting vertex %d are:\n", start);
dfs(start);
break;
case 3:
exit(0);
default:
98
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
OUTPUT:
*************************case-1*************************
*************************case-2*************************
99
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
Enter your choice: 1
Nodes reachable from starting vertex 2 are: 3 4
The vertex that is not reachable is 1
*************************case-3*************************
*************************case-4*************************
100
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
0101
0010
0001
0000
Enter the starting vertex: 2
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
Enter your choice: 2
Nodes reachable from starting vertex 2 are: 3 4
101
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
PROGRAM 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.
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>
int key[20], n, m;
int * ht, index;
int count = 0;
void display()
{
int i;
if (count == 0)
102
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
{
printf("\nHash Table is empty");
return;
}
void main()
{
int i;
printf("\nEnter the number of employee records (N): ");
scanf("%d", & n);
printf("\nEnter the two digit memory locations (m) for hash table: ");
scanf("%d", & m);
printf("\nEnter the four digit key values (K) for N Employee Records:\n ");
for (i = 0; i < n; i++)
scanf("%d", & key[i]);
103
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
{
printf("\n-----Hash table is full. Cannot insert the record %d key-----", i + 1);
break;
}
insert(key[i]);
}
display();
}
OUTPUT:
Enter the number of employee records (N) :10
Enter the two digit memory locations (m) for hash table:15
Enter the four digit key values (K) for N Employee Records:
4020
4560
9908
6785
0423
7890
6547
3342
9043
6754
Hash Table contents are:
104
DEPT OF CSE/DS/ISE
DATA STRUCTURES LABORATORY BCSL305
105
DEPT OF CSE/DS/ISE