DS_Lab_Programs_1TO_12[1]
DS_Lab_Programs_1TO_12[1]
Practical:1
calendar[1].date = 2;
strcpy(calendar[1].activity, "Meeting at 10 AM");
strcpy(calendar[2].name, "Wednesday");
calendar[2].date = 3;
strcpy(calendar[2].activity, "Gym at 6 PM");
strcpy(calendar[3].name, "Thursday");
Dept. of CSE, BTI, Bangalore-35 Page 1
Data Structures Laboratory (BCSL305) 2023-2024
calendar[3].date = 4;
strcpy(calendar[3].activity, "Dinner with friends at 7 PM");
strcpy(calendar[4].name, "Friday");
calendar[4].date = 5;
strcpy(calendar[4].activity, "Movie night at 8 PM");
strcpy(calendar[5].name, "Saturday");
calendar[5].date = 6;
strcpy(calendar[5].activity, "Weekend getaway");
strcpy(calendar[6].name, "Sunday");
calendar[6].date = 7;
strcpy(calendar[6].activity, "Relax and recharge");
// Print the calendar printf("Calendar for the week:\n");
for (int i = 0; i < 7; i++) {
printf("%s (Date: %d): %s\n", calendar[i].name, calendar[i].date, calendar[i].activity);
}
return 0;
}
OUTPUT:
AIM: 1B) 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.
PROGRAM CODE:
#include <stdio.h>
#include <string.h>
// Define a structure to represent a day
struct Day {
char name[20];
int date;
char activity[100];
};
// Function to create the calendar
void create(struct Day calendar[7]) {
for (int i = 0; i < 7; i++) {
printf("Enter details for %s:\n", calendar[i].name);
printf("Date: ");
scanf("%d", &calendar[i].date);
printf("Activity: ");
scanf(" %[^\n]", calendar[i].activity);
}
}
// Function to read data from the keyboard
void read(struct Day calendar[7]) {
FILE *file = fopen("calendar.txt", "r");
if (file == NULL) {
printf("Error opening the file.\n");
return;
}
for (int i = 0; i < 7; i++) {
fscanf(file, "%d", &calendar[i].date);
fscanf(file, " %[^\n]", calendar[i].activity);
}
fclose(file);
}
// Function to display the calendar
void display(struct Day calendar[7]) {
printf("Calendar for the week:\n");
for (int i = 0; i < 7; i++) {
printf("%s (Date: %d): %s\n", calendar[i].name, calendar[i].date, calendar[i].activity);
}
}
int main() {
struct Day calendar[7];
// Initialize the names of the days
strcpy(calendar[0].name, "Monday");
strcpy(calendar[1].name, "Tuesday");
strcpy(calendar[2].name, "Wednesday");
strcpy(calendar[3].name, "Thursday");
strcpy(calendar[4].name, "Friday");
strcpy(calendar[5].name, "Saturday");
strcpy(calendar[6].name, "Sunday");
int choice;
printf("1. Create Calendar\n");
printf("2. Read Calendar from File\n");
OUTPUT:
Practical:2
}}
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);
}
getch();
}
OUTPUT:
Practical:3
AIM: 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.
PROGRAM CODE:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define max_size 5
int stack[max_size],top=-1,flag=1;
int i,temp,item,rev[max_size],num[max_size];
void push();
void pop();
void display();
void pali();
void main()
{
int choice;
printf("\n\n--------STACK OPERATIONS--------");
printf("1.Push\n");
printf("2.Pop\n"); printf("3.Palindrome\n"); printf("4.Display\n"); printf("5.Exit\n");
printf(" ");
while(1)
{
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
temp=top;
}
void pop() //deleting an element from the stack
{
if(top==-1)
{
printf("Stack Underflow:");
flag=0;
}
else
{
item=stack[top];
top=top-1;
}
}
void pali()
{ i=0;
if(top==-1)
{
printf("Push some elements into the stack first\n");
}
else
{
while(top!=-1)
{
rev[top]=stack[top]; pop();
}
top=temp; for(i=0;i<=temp;i++)
{
if(stack[top--]==rev[i])
{
if(i==temp)
{
printf("Palindrome\n"); return;
}
}
}
}
printf("Not Palindrome\n");
}
void display()
{
int i; top=temp;
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:
Practical:4
AIM: 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.
PROGRAM CODE:
#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;
case '+':
case '-': return 2;
case '*': case '/':
case '%': return 3;
Practical:5
if(isoperand(postfix[i]))
push(postfix[i]-48);
else
{
op1=pop();
op2=pop();
ans=operate(op1,op2,postfix[i]);
push(ans);
printf("%f %c %f = %f\n",op2,postfix[i],op1,ans);
}
i++;
}
printf("%f",s.str[s.top]);
getch();
}
int isoperand(char x)
{
if(x>='0' && x<='9')
return 1;
else return 0;
}
void push(float x)
{
if(s.top==MAX-1)
printf("Stack is full\nStack overflow\n");
else
{
s.top++;
s.str[s.top]=x;
}}
float pop()
{
if(s.top==-1)
{
printf("Stack is emplty\nSTACK UNDERFLOW\n");
getch();
}
else
{
s.top--;
return s.str[s.top+1];
}}
float operate(float op1,float op2,char a)
{
switch(a)
{
case '+' : return op2+op1;
case '-' : return op2-op1;
case '*' : return op2*op1;
case '/' : return op2/op1;
case '^' : return pow(op2,op1);
}}
OUTPUT:
PROGRAM CODE:
//5B Solving Tower of Hanoi problem with n disks
#include <stdio.h>
#include <conio.h>
void tower(int n, int source, int temp,int destination)
{
if(n == 0)
return;
tower(n-1, source, destination, temp);
printf("\nMove disc %d from %c to %c", n, source, destination);
tower(n-1, temp, source, destination);
}
int main()
{
int n;
printf("\nEnter the number of discs: \n");
scanf("%d", &n);
tower(n, 'A', 'B', 'C');
printf("\n\nTotal Number of moves are: %d", (int)pow(2,n)-1);
return 0;
}
OUTPUT:
Practical:9
AIM: 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
PROGRAM CODE:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
POLY *getNode();
void read_poly(POLY *head, int n);
void print_poly(POLY *head);
POLY *add_poly(POLY *h1, POLY *h2);
int compare(POLY *temp1, POLY *temp2);
void attach(float cf, POLY *exptemp, POLY **tempres);
POLY* delete(POLY *head, POLY *temp);
void main()
{
int n1, n2;
POLY *POLY1 = getNode();
POLY *POLY2 = getNode();
POLY *POLYSUM = getNode();
POLY1->expx = -1;
POLY1->link = POLY1;
POLY2->link = POLY2;
POLYSUM->link = POLYSUM;
printf("\nEnter the number of terms for both polynomials\n");
scanf("%d%d",&n1, &n2);
printf("\nEnter 1st Polynomial\n");
read_poly(POLY1, n1);
printf("\n1st Polynomial is\n");
print_poly(POLY1);
printf("\nEnter 2nd Polynomial\n");
read_poly(POLY2, n2);
printf("\n2nd Polynomial is\n");
print_poly(POLY2);
POLYSUM = add_poly(POLY1, POLY2);
printf("\nThe Resultant polynomial is\n");
print_poly(POLYSUM);
evaluate(POLYSUM);
}
POLY *getNode()
{
POLY *temp = (POLY *) malloc(sizeof(POLY));
if(temp == NULL)
{
printf("No Memory\n");
exit(0);
}
return temp;
}
case 1:
cf = temp1->coef + temp2->coef;
if(cf)
{
attach(cf, temp1, &tempres);
}
temp1 = temp1->link;
h2 = delete(h2, temp2);
temp2 = h2->link;
break;
case 2:
temp2 = temp2->link;
break;
}
}
if(temp1 != h1)
{
attach(temp1->coef, temp1, &tempres);
temp1 = temp1->link;
}
}
temp2 = h2->link;
while(temp2 != h2)
{
attach(temp2->coef, temp2, &tempres);
temp2 = temp2->link;
}
tempres->link = result;
return result;
}
OUTPUT:
Practical:10
AIM: 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
PROGRAM CODE:
#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);
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
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.BST Traversals:");
printf("\n3.Search ");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
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;
case 4: exit(0);
}
}
}
OUTPUT:
Practical:11
AIM: 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
PROGRAM CODE:
#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);
}
}
}
}
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()
{
{
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
}
for(i=1;i<=n;i++)
visited[i]=0;
printf("\nEnter the starting vertex: ");
scanf("%d",&start);
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;
dfs(start);
break;
case 3: exit(0);
default: printf("\nPlease enter valid choice:");
}
}
OUTPUT:
Practical:12
AIM: 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.
PROGRAM CODE:
#include<stdio.h>
#include<stdlib.h>
int key[20],n,m;
int *ht,index;
int count = 0;
void display()
{
int i;
if(count == 0)
{
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);
ht = (int *)malloc(m*sizeof(int));
for(i=0; i<m; i++)
ht[i] = -1;
printf("\nEnter the four digit key values (K) for N Employee Records:\n ");
for(i=0; i<n; i++)
scanf("%d", &key[i]);
for(i=0;i<n;i++)
{
if(count == m)
{
printf("\n~~~Hash table is full. Cannot insert the record %d key~~~",i+1);
break;
}
insert(key[i]);
}
OUTPUT:
Title of the Practical: Program to search an element of array using linear search
Q9 During linear search, when the record is present in first position then how many
comparisons are made ?
A9 Only one comparison.
Q10 During linear search, when the record is present in last position then how many
comparisons are made?
A10 n comparisons have to be made.
Title of the Practical: Program to reverse the element of array. Insertion and
deletion on array at specified position.
Title of the Practical: Program to implement PUSH and POP operation on stack.
Q1 What is stack?
A1 Stack is an ordered collection of elements like array.
Q7 What is recursion.
A7 Recursion is defined as function calling itself.
Title of the Practical: Program based on infix to prefix and post fix notation.
Q1 What is Stack ?
A1 Stack is ordered collection of element like arrays out it has a special feature that
deletion and insertion of element can be done only from one end called top of stack .It
is also called LIFO(last in first out).
Q2 Application of Stack ?
A2 Infix Prefix Postfix
Title of the Practical: Program based on queue & their operations for an
application
Q1 Define queue.
A1Queue is an homogeneous collection of elements. It is logically a first in first out
(FIFO) type of list.Queue means a line.
Q9 When an element is added to the dequeue with n memory cells ,what happens to
LEFT or RIGHT.
A9 If the element is added an the left , then LEFT is decreases by 1 (mod n) . IF the
element is added on the right , then RIGHT is increase by 1 (mod n)
Q3 Explain Dequeue ?
A3 It is also a homogenous list of element in which insertion deletion of element are
perform both the ends we insert element from the rear or from the front end.
Q4 What is Queue ?
A4 It is a non primitive linear data structure. In a Queue new element are added at the
one end called rear end and the element are removed from another end called front
end.
Q5 Variation in a Queue ?
A5 Circular Queue -> Dequeue ->Priority Queue
Q7 Application of Queue ?
A7 1> Customer service center 2> Ticket counter 3> Queue is used for determine
space complexity & time complexity.
Title of the Practical: Program based on list operations and its applications.
Dept. of CSE, BTI, Bangalore-35 Page 53
Data Structures Laboratory (BCSL305) 2023-2024
Q1 Which of the following abstract data types are NOT used by Integer Abstract Data
type group?
A1. A)Short B)Int C)float D)long
Q6: What is the difference between const char *myPointer and char *const
myPointer?
A6: Const char *myPointer is a non constant pointer to constant data; while char
*const myPointer is a constant pointer to non constant data.
Q7: From which is the pointer to object of a base class type compatible ?
A7: Pointers to object of a base class type is compatible with pointer to onject of a
derived class . Therefore , we can use single pointer variable to point to objects of
base class as well as derived class.
Q1 What is a tree?
Dept. of CSE, BTI, Bangalore-35 Page 55
Data Structures Laboratory (BCSL305) 2023-2024
A1 A tree is a non linear data structure in which items are arranged in a sorted
sequence.It is a finite set of one or more data items.