DS Lab Manual
DS Lab Manual
LAB MANUAL
DATA STRUCTURES LABORATORY
BCSL305
Name:
USN:
Section:
Batch:
Mission
Mission
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
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
14.09.2023
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:
Internal Schedule:
Date
IA Test 1 2
Programs included 1-6 7-12
Data Structures Laboratory - 18CSL38 III Semester / B.E
EXPERIMENT – 01: Array operations
1. 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).
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
// Declare an array of 7 elements, each of type struct Day
struct Day calendar[7];
Day: Tuesday
Date: 2
Activity: Meeting at 10 AM
Day: Wednesday
Date: 3
Activity: Gym at 6 PM
Day: Thursday
Date: 4
Activity: Dinner with friends at 7 PM
Day: Friday
Date: 5
Activity: Movie night at 8 PM
Day: Saturday
Date: 6
Activity: Weekend getaway
Day: Sunday
Date: 7
Activity: Relax and recharge
Additional Program:
➢ Input a word
➢ Insert new character at specified position and display the updated word
➢ Delete a character at specified position and display the updated word.
Evaluation of experiment: Conduction: 5, Modification: 5 Viva 4
CODE:
#include<stdio.h>
#include<stdlib.h>
char STR[100], PAT[50], REP[50], ANS [100];
int Pattern_Match_Replace() { // String Matching function
int i=0; //index for PAT
int c=0, m=0; //index for STR
int k=0 ; //index for REP
int flag=0;
int j=0; //index for ANS
PUSH operation:
POP operation:
• Compare both end of stack till TOP/2 if all element are matching
then display Palindrome otherwise not a palindrome
CODE:
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
void Display();
int stack[MAX],top = -1,i;
int isEmpty(){
if(top == -1){
printf("\nStack is Underflow!");
return 1; //return 1 if empty
}
else
return 0; //not empty
}
int isFull(){
if (top == (MAX-1)){
printf("\n\nStack is Overflow!");
return 1; //1 if full
}
else
return 0;//not full
}
Design, develop and Implement 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 .
Steps :
• Read the infix expression as a string.
• Scan the expression character by character till the end. Repeat
the following operations:-
• If it is an operand add it to the postfix expression.
• If it is a left parenthesis push it onto the stack.
• If it is a right parentheses, pop out elements from the stack
and add it to the postfix string. Pop out the left parentheses
but don’t add to postfix. (Observe the return value of “(“ of
stack and “)” of Input)
• If it is an operator, compare its precedence with that of the
element at the top of stack.
o If it is greater push it onto the stack.
o Otherwise pop and add elements to the postfix expression
until base symbol #.
o If # is reached, i.e the end of the expression, pop out
any leftover elements in the stack till it becomes empty.
o Append a null terminator at the end display the result
Code:
#include<stdio.h>
#include<string.h>
int Stack (char symbol) {
switch(symbol){
case '+': return 2;
case '-': return 2;
case '*': return 4;
case '/': return 4;
case '%': return 4; //modulus ex- 5% 3=2
case '^': return 5; //power
case '(': return 0;
case '#': return -1;
default : return 8;
}
}
Few examples:
(1+2+3)➔ left to right execution 3+3➔ 6
(1*2-3)➔ left to right, * has higher priority so 3-3=0
(1%3%6)➔ left to right 1%3➔1 1%6➔ 1
(2^3^2) right to left 2^9 ➔512
Algorithm:
Step 1: Move a tower of height-1 to an intermediate pole, using the
final pole.
Step 2: Move the remaining disk to the final pole.
Step 3: Move the tower of height-1 from the intermediate pole to the
CODE:
#include<stdio.h>
#include<math.h>
void Tower(int n, char source, char temp, char destination){
if(n = = 0) return;
Tower(n-1, source, destination, temp);
printf("\n Move disc %d from %c to %c", n, source, destination);
Tower(n-1, temp, source, destination);
}
void main(){
int n;
printf("\nEnter the number of discs: ");
scanf("%d", &n);
Tower(n, 'S', 'T', 'D');
printf("\n\n Total Number of moves are: %d", (int)pow(2,n)-1);
}
OUTPUT
cc Lab5b.c –lm
1 S D
2 S T
1 D T
3 S D
1 T S
2 T D
1 S D
Steps:
1. Insertion
• Display overflow if rear >-1 and front is rear+1
• Otherwise Insert new element at updated rear (i.e rear+1)
2. Deletion
• Display Underflow if front=0 and rear=-1
• Otherwise return the element at front and update front
(i.e front=front +1 if no element found( if rear=front)
then resetfront and rear.
3. Display
• Display all element from front to rear in circular fashion
CODE:
#include<stdio.h>
#include<stdlib.h>
char q[10];
int max=4;
int front=-1,rear=-1;
int isFull(){
if((front+1)%max==(rear+1)% max && rear>-1)
{
return 1; //if full
}
else
return 0; //if not full
}
int isEmpty(){
if((front==-1)&&(rear==-1)) {
return 1; //if empty
}
else{
return 0; //if not empty
}
}
#include<stdio.h>
#include<stdlib.h>
int MAX=4,count=0;
struct student{
char usn[10], name[30], branch[5];
char phno[10];
int sem;
struct student *next;
};
typedef struct student NODE;
NODE *head,*newnode;
NODE* Getnode(){
newnode=(NODE*)malloc(sizeof(NODE));
printf("\nEnter USN, Name, Branch, Sem, Ph.No:");
scanf("%s%s%s%d%s",
newnode->usn,newnode->name,newnode->branch, &newnode->sem, newnode->phno);
newnode->next=NULL;
count++;
return (newnode);
}
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
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
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
int MAX=4,count=0;
struct emp{
char ssn[5],name[20], dept[10], phno[10],desig[15];
int sal;
struct emp *left, *right;
};
typedef struct emp NODE;
NODE* Getnode(){
NODE *node=(NODE*)calloc(1,sizeof(NODE));
printf("\nEnter SSN, Name, Dept, Designation, Sal, Ph.No:");
scanf("%s%s%s%s%s%d",node ->ssn, node ->name, node ->dept, node ->phno ,node ->desig,&node -
>sal);
node->right=node ->left =NULL;
count ++;
return node;
}
void Display(NODE *p){
if(p != NULL){
printf("\nSSN\tNAME\tDEPT\tDESINGATION\tSAL\tPh.NO.\n");
while(p!=NULL){
// printf("count:%d %u",count,p);
printf("\n%s\t%s\t%s\t%s\t%s\t\t%d",p->ssn, p->name, p->dept, p->phno,p->desig,p->sal);
p = p->right;
}
printf("\nNumber of nodes%d",count);
}
else
printf("\nNo data available!!\n");
}
NODE* Insert_Rear(NODE *head){
NODE *p=head, *newnode;
if(count==MAX)
printf("\n Overflow!!");
else{
newnode=Getnode();
if(head==NULL)
head=newnode;
else{
while(p->right!=NULL){
p=p->right;
}
Algorithm:
Step for Node creation
➢ Get a node and assign coefficient and power of x,y,z
➢ Attach this to head in circular linked list fashion
Step for Node display
➢ From the first node last node display all data fields of
a node in a standard form Coeff.X^powx.Y^powx.Z^powxGet a
node and assign coefficient and power of x,y,z
Steps for compare
➢ Return aif corresponding coefficient X,Y and Z of two
polynomials are same, otherwise return 0.
Steps:
CODE:
#include <stdio.h>
#include <stdlib.h>
struct BST {
int data;
struct BST *left, *right;
};
typedef struct BST NODE;
Dept. of ISE, SIT, Mangaluru Page|25
Data Structures Laboratory - BCSL305 III Semester / B.E
NODE* Createtree(NODE *node, int data){
if (node == NULL) {
NODE *temp= (NODE*)calloc(1,sizeof(NODE));
temp->data = data;
return temp; //create and insert new node to BST
}
if (data < node->data) //TRAVERSE LEFT of NODE if key is less than data
node->left = Createtree(node->left, data);
else if (data > node->data) //TRAVERSE LEFT of NODE if key is greater than data
node -> right = Createtree(node->right, data);
else //reject the duplicate
printf(“duplicate data found”);
return node;
}
NODE* Search(NODE *node, int data){
if(node == NULL) //insert at proper place
printf("\n Element not found");
else if(data < node->data) //TRAVERSE LEFT of NODE if key is less than data
node->left=Search(node->left, data);
else if(data > node->data) //TRAVERSE LEFT of NODE if key is greater than data
node->right=Search(node->right, data);
else{
printf("\n Element found: %d", node->data);
}
return node;
}
void Inorder(NODE *node){ //LEFT-ROOT-RIGHT
if(node != NULL){
Inorder(node->left);
printf("%d\t", node->data);
Inorder(node->right);
}
}
void Preorder(NODE *node){ //ROOT—LEFT-RIGHT
if(node != NULL) {
printf("%d\t", node->data);
Preorder(node->left);
Preorder(node->right);
}
}
void Postorder(NODE *node){ //LEFT –RIGHT-ROOT
if(node != NULL) {
Postorder(node->left);
Postorder(node->right);
printf("%d\t", node->data);
}
}
void main()
{
int no, ch, i, n;
NODE *root=NULL;
do{
printf("\n1.Creation\n2.Search\n3. Inorder \n4. Preorder \n5. Postorder \n6.Exit");
Algorithm:
Input: A graph G and a vertex v of G
Output: All vertices reachable from v labeled as discovered
Step for BFS(G,v) (Breadth First Search)
➢ Visit all vertices from front to rear only once and after visiting a vertices, mark it as
visited and also display the data containing in the vertices
Step for DFS(G,v) (Depth First Search)
Algorithm:
CODE:
#include<stdio.h>
#include <stdlib.h>
#define MAX 10
struct employee{
int id;
char name[15];
};
typedef struct employee EMP;
EMP emp[MAX];
int a[MAX];
void Linear_prob(int key, int num){
int flag=0, i=key;
if(a[i] == -1)
flag=1;
else{
printf("\nCollision Detected...!!!\n");
i=(key+1)%MAX;
do{
if(a[i] == -1) {
flag = 1;
break;
}
else
i=(++i)%MAX;
}while(i!=key && flag==0);
}
Dept. of ISE, SIT, Mangaluru Page|31
Data Structures Laboratory - BCSL305 III Semester / B.E
if(flag==0)
printf("\n Hash table is full");
else{
printf("\nEnter emp id: ");
scanf("%d",&emp[i].id);
printf("\nEnter emp name: ");
scanf("%s",emp[i].name);
a[i]=i;
printf("\nCollision avoided successfully using LINEAR PROBING\n");
}
}
void Display(){
int i, ch;
printf("\n1.Display ALL\n2.Filtered Display \n Enter the choice:");
scanf("%d",&ch);
printf("\nHTKey\tEmpID\tEmpName");
for(i=0; i<MAX; i++){
if(ch==2 && a[i] == -1)
continue;
else
printf("\n%d\t%d\t%s", i, emp[i].id, emp[i].name);
}
}
int main(){
int num, key, i;
int ch = 1;
printf("\nCollision handling by linear probing: ");
for (i=0; i < MAX; i++) {
a[i] = -1;
}
do{
printf("\nEnter the data: ");
scanf("%d", &num);
Linear_prob(num % 100,num);
printf("\nDo you wish to continue? (1/0): ");
scanf("%d",&ch);
}while(ch);
Display();
}
OUTPUT
#Run 1
Collision handling by linear probing:
Enter the data: 2
Enter emp id: 100
Enter emp name: Ricky
Collision avoided successfully using LINEAR PROBING
Do you wish to continue? (1/0): 1
Enter the data: 7
Enter emp id: 101
Enter emp name: Reena
Collision avoided successfully using LINEAR PROBING
Do you wish to continue? (1/0): 0
1.Display ALL 2.Filtered Display
Dept. of ISE, SIT, Mangaluru Page|32
Data Structures Laboratory - BCSL305 III Semester / B.E
Enter the choice:1
HTKey EmpID EmpName
0 0
1 0
2 100 Ricky
3 0
4 0
5 0
6 0
7 101 Reena
8 0
9 0
#Run 2
Collision handling by linear probing:
Enter the data: 2
Enter emp id: 101
Enter emp name: Raj
Collision avoided successfully using LINEAR PROBING
Do you wish to continue? (1/0): 1
Enter the data: 4
Enter emp id: 102
Enter emp name: Raina
Collision avoided successfully using LINEAR PROBING
Do you wish to continue? (1/0): 0
1.Display ALL 2.Filtered Display
Enter the choice:2
The hash table is:
HTKey EmpID EmpName
2 101 Raj
4 102 Raina
#Run 3
Collision handling by linear probing:
Enter the data: 2
Enter emp id: 100
Enter emp name: Reema
Collision avoided successfully using LINEAR PROBING
Do you wish to continue? (1/0): 1
Enter the data: 2
Collision Detected...!!!
Enter emp id: 101
Enter emp name: Robert
Do you wish to continue? (1/0): 1
Enter the data: 2
Collision Detected...!!!
Enter emp id: 103
Enter emp name: Richard