In this blog post, you will find solutions for the laboratory subject DATA STRUCTURES LABORATORY (BCSL305) course work for the III semester of VTU university. The solutions to the lab component are coded in C. We recommend using the Code:Blocks as the integrated development environment (IDE). Along with C programs for each question I have provided samples of program output as well.
You can find the lab syllabus here.
After getting the necessary development environment setup, Now lets focus on the solutions. Click on the appropriate hyperlink to go to your program of choice.
- Calendar Application
- String Operations
- Stack of Integers
- Infix to Postfix Conversion
- Stack Applications
- Circular Queue
- Singly Linked List of Student Data
- Doubly Linked List of Employee Data
- Polynomial Evaluation and Addition
- Binary Search Tree
- Graph Reachability using DFS/BFS
- Hashing & Linear Probing
Program 01 : Calendar Application
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.
C Code
/***************************************************************************
*File : 01Calender.c
*Description: Calendar operations
*Author : Prabodh C P
*Compiler : gcc compiler, Ubuntu 22.04
*Date : 28 September 2023
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NUM_DAYS_IN_WEEK 7
// Structure to represent a day
typedef struct
{
char *acDayName; // Dynamically allocated string for the day name
int iDate; // Date of the day
char *acActivity; // Dynamically allocated string for the activity description
}DAYTYPE;
void fnFreeCal(DAYTYPE *);
void fnDispCal(DAYTYPE *);
void fnReadCal(DAYTYPE *);
DAYTYPE *fnCreateCal();
int main()
{
// Create the calendar
DAYTYPE *weeklyCalendar = fnCreateCal();
// Read data from the keyboard
fnReadCal(weeklyCalendar);
// Display the week's activity details
fnDispCal(weeklyCalendar);
// Free allocated memory
fnFreeCal(weeklyCalendar);
return 0;
}
DAYTYPE *fnCreateCal()
{
DAYTYPE *calendar = (DAYTYPE *)malloc(NUM_DAYS_IN_WEEK * sizeof(DAYTYPE));
for(int i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
calendar[i].acDayName = NULL;
calendar[i].iDate = 0;
calendar[i].acActivity = NULL;
}
return calendar;
}
void fnReadCal(DAYTYPE *calendar)
{
char cChoice;
for(int i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
printf("Do you want to enter details for day %d [Y/N]: ", i + 1);
scanf("%c", &cChoice); getchar();
if(tolower(cChoice) == 'n')
continue;
printf("Day Name: ");
char nameBuffer[50];
scanf("%s", nameBuffer);
calendar[i].acDayName = strdup(nameBuffer); // Dynamically allocate and copy the string
printf("Date: ");
scanf("%d", &calendar[i].iDate);
printf("Activity: ");
char activityBuffer[100];
scanf(" %[^n]", activityBuffer); // Read the entire line, including spaces
calendar[i].acActivity = strdup(activityBuffer);
printf("n");
getchar(); //remove trailing enter character in input buffer
}
}
void fnDispCal(DAYTYPE *calendar)
{
printf("nWeek's Activity Details:n");
for(int i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
printf("Day %d:n", i + 1);
if(calendar[i].iDate == 0)
{
printf("No Activitynn");
continue;
}
printf(" Day Name: %sn", calendar[i].acDayName);
printf(" Date: %dn", calendar[i].iDate);
printf(" Activity: %snn", calendar[i].acActivity);
}
}
void fnFreeCal(DAYTYPE *calendar)
{
for(int i = 0; i < NUM_DAYS_IN_WEEK; i++)
{
free(calendar[i].acDayName);
free(calendar[i].acActivity);
}
free(calendar);
}
Output
putta:~/.../Programs$ ./a.out
Do you want to enter details for day 1 [Y/N]: N
Do you want to enter details for day 2 [Y/N]: Y
Day Name: Monday
Date: 10
Activity: Meeting with Chairman.
Do you want to enter details for day 3 [Y/N]: N
Do you want to enter details for day 4 [Y/N]: N
Do you want to enter details for day 5 [Y/N]: Y
Day Name: Thursday
Date: 13
Activity: Product Survey
Do you want to enter details for day 6 [Y/N]: Y
Day Name: Friday
Date: 14
Activity: Budget Breakdown and Planning
Do you want to enter details for day 7 [Y/N]: Y
Day Name: Saturday
Date: 15
Activity: Outing with family
Week's Activity Details:
Day 1:
No Activity
Day 2:
Day Name: Monday
Date: 10
Activity: Meeting with Chairman.
Day 3:
No Activity
Day 4:
No Activity
Day 5:
Day Name: Thursday
Date: 13
Activity: Product Survey
Day 6:
Day Name: Friday
Date: 14
Activity: Budget Breakdown and Planning
Day 7:
Day Name: Saturday
Date: 15
Activity: Outing with family
Program 02 : String Operations
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.