0% found this document useful (0 votes)
11 views20 pages

Ds Lab Programs

Data structures lab manual 3rd sem cse

Uploaded by

Umesh H. S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views20 pages

Ds Lab Programs

Data structures lab manual 3rd sem cse

Uploaded by

Umesh H. S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

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.

a)

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct Day {

char *dayname;

char *activitydescription;

};

int main() {

struct Day calendar[7];

for (int i = 0; i < 7; i++) {

calendar[i].dayname = (char *)malloc(20 * sizeof(char));

calendar[i].activitydescription = (char *)malloc(100 * sizeof(char));

}
strcpy(calendar[0].dayname, "Monday");

strcpy(calendar[0].activitydescription, "Work");

strcpy(calendar[1].dayname, "Tuesday");

strcpy(calendar[1].activitydescription, "Meeting");

strcpy(calendar[2].dayname, "Wednesday");

strcpy(calendar[2].activitydescription, "Gym");

strcpy(calendar[3].dayname, "Thursday");

strcpy(calendar[3].activitydescription, "Study");

strcpy(calendar[4].dayname, "Friday");

strcpy(calendar[4].activitydescription, "Movie night");

strcpy(calendar[5].dayname, "Saturday");

strcpy(calendar[5].activitydescription, "Shopping");

strcpy(calendar[6].dayname, "Sunday");

strcpy(calendar[6].activitydescription, "Relax");

for (int i = 0; i < 7; i++) {

printf("Day: %s\n", calendar[i].dayname);

printf("Activity: %s\n", calendar[i].activitydescription);

printf("\n");

}
for (int i = 0; i < 7; i++) {

free(calendar[i].dayname);

free(calendar[i].activitydescription);

return 0;

Output

Day: Monday

Activity: Work

Day: Tuesday

Activity: Meeting

Day: Wednesday

Activity: Gym

Day: Thursday

Activity: Study

Day: Friday

Activity: Movie night

Day: Saturday

Activity: Shopping

Day: Sunday

Activity: Relax
b)

#include <stdio.h>

#include <stdlib.h>
#include <string.h>

struct calendarDay{

char*dayname;

int date;

char*activity;

};

void create(struct calendarDay calendar[7]){

for(int i=0;i<7;i++){

calendar[i].dayname = (char *)malloc(20);

calendar[i].activity = (char *)malloc(100);

void read(struct calendarDay calendar[7]){

for(int i=0;i<7;i++){

printf("Enter dayname for day %d:",i+1);

scanf("%s",calendar[i].dayname);

printf("Enter date for day %d:",i+1);

scanf("%d",&calendar[i].date);

printf("Enter activity for day %d:",i+1);

scanf("%[^\n]",calendar[i].activity);

void display(struct calendarDay calendar[7]){

printf("\nActivity Details for the week:\n");

for(int i=0;i<7;i++){

printf("dayname: %s\n", calendar[i].dayname);

printf("Date: %d\n", calendar[i].date);

printf("activity: %s\n\n", calendar[i].activity);


}

int main()

struct calendarDay calendar[7];

create(calendar);

read(calendar);

display(calendar);

for(int i=0;i<7;i++){

free(calendar[i].dayname);

free(calendar[i].activity);

return 0;

OUTPUT

Enter dayname for day 1:MONDAY

Enter date for day 1:12

Enter activity for day 1:WORK

Enter dayname for day 2:TUESDAY

Enter date for day 2:13

Enter activity for day 2:MEETING

Enter dayname for day 3:WEDNESDAY

Enter date for day 3:14

Enter activity for day 3:GYM

Enter dayname for day 4:THURSDAY

Enter date for day 4:15

Enter activity for day 4:STUDY

Enter dayname for day 5:FRIDAY


Enter date for day 5:16

Enter activity for day 5:MOVIE

Enter dayname for day 6:SATURDAY

Enter date for day 6:17

Enter activity for day 6:SHOPPING

Enter dayname for day 7:SUNDAY

Enter date for day 7:18

Enter activity for day 7:RELAX

Activity Details for the week:

dayname: MONDAY

Date: 12

activity: WORK

dayname: TUESDAY

Date: 13

activity: MEETING

dayname: WEDNESDAY

Date: 14

activity: GYM

dayname: THURSDAY

Date: 15

activity: STUDY

dayname: FRIDAY

Date: 16

activity: MOVIE

dayname: SATURDAY
Date: 17

activity: SHOPPING

dayname: SUNDAY

Date: 18

activity: RELAX

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.

a)
#include <stdio.h>

#include <string.h>

int main() {

char STR[100], PAT[100], REP[100], ans[100];

int i, j, c, m, k, flag = 0;

printf("\nEnter the MAIN string:\n");

fgets(STR, sizeof(STR), stdin);

STR[strcspn(STR, "\n")] = '\0';

printf("\nEnter the PATTERN string:\n");

fgets(PAT, sizeof(PAT), stdin);

PAT[strcspn(PAT, "\n")] = '\0';

printf("\nEnter the REPLACE string:\n");

fgets(REP, sizeof(REP), stdin);

REP[strcspn(REP, "\n")] = '\0';

i = m = c = j = 0;

while (STR[c] != '\0') {

if (STR[c] == PAT[i]) {

i++;

if (PAT[i] == '\0') {

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


ans[j] = REP[k];

i = 0;

c++;

} else {

ans[j] = STR[c];

j++;

c++;

i = 0;

if (i == 0) {

ans[j] = '\0';

printf("\nThe Resultant string is: %s\n", ans);

} else {

printf("\nPattern doesn't found!!!\n");

OUTPUT

Enter the MAIN string:

GOOD MORNING

Enter the PATTERN string:

MORNING

Enter the REPLACE string:


EVENING

The Resultant string is: GOOD EVENING

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
#include <stdio.h>

#include <stdlib.h>

int stack[6],rev[6];

int top=-1,k=0;

int size;

void push();void pop();

void display();

int pali();

int main()

int choice,f;

printf("Enter the size for stack\n");

scanf("%d",&size);

printf("1.push\n 2.pop\n 3.display\n 4.check for palindrome\n 5.exit\n");

while(1){

printf("Enter the choice\n");

scanf("%d",&choice);

switch(choice)

case 1:push();

break;

case 2:pop();

break;

case 3:display();

break;

case 4:f=pali();

if(f==1)

printf("It's palindrome\n");

else
printf("It's not a palindrome\n");

break;

case 5:

exit(0);

default:printf("wrong choice...\n");

void push()

int num;

if(top==(size-1))

printf("stack overflow\n");

else{

printf("enter the number to be pushed\n");

scanf("%d",&num);

top++;

stack[top]=num;

void pop()

int num;

if(top==-1)

printf("stack underflow\n");

}
else{

num=stack[top];

printf("popped element is %d\n",num);

top--;

void display()

int i;

if(top==-1)

printf("stack underflow\n");

else{

printf("stack contents...\n");

for(i=top;i>=0;i--){

printf("%d\n",stack[i]);

rev[k++]=stack[i];

int pali()

int i,flag=1;

for(i=top;i>=0;i--)

if(stack[i]!=rev[--k])

flag=0;
}

return flag;

Output

Enter the size for stack

1.push

2.pop

3.display

4.check for palindrome

5.exit

Enter the choice

enter the number to be pushed

Enter the choice

enter the number to be pushed

Enter the choice

enter the number to be pushed

Enter the choice

enter the number to be pushed

Enter the choice


1

enter the number to be pushed

Enter the choice

enter the number to be pushed

Enter the choice

stack contents...

Enter the choice

stack overflow

Enter the choice

popped element is 6

Enter the choice

popped element is 5

Enter the choice

stack contents...

9
4

Enter the choice

It's not a palindrome

Enter the choice

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 <ctype.h>

#define SIZE 50

char s[SIZE];

int top = -1;

int push(char elem) {

s[++top] = elem;

return 0;

}
char pop() {

return s[top--];

int pr(char elem) {

switch (elem) {

case '#': return 0;

case '(': return 1;

case '+': case '-': return 2;

case '*': case '/': case '%': return 3;

case '^': return 4;

default: return -1;

int main() {

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

int i = 0, k = 0;

printf("\nEnter the infix expression: ");

scanf("%s", infx);

push('#');

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

if (isalnum(ch)) {

pofx[k++] = ch;

}
else if (ch == '(') {

push(ch);

else if (ch == ')') {

while (s[top] != '(') {

pofx[k++] = pop();

elem = pop();

else {

// Pop operators with higher or equal precedence than the current


one

while (pr(s[top]) >= pr(ch)) {

pofx[k++] = pop();

push(ch);

while (s[top] != '#') {

pofx[k++] = pop();

pofx[k] = '\0';

printf("\nGiven infix expression: %s\nPostfix expression: %s\n", infx,


pofx);

return 0;
}

OUTPUT

Enter the infix expression: A+B*C/D-F+A*E

Given infix expression: A+B*C/D-F+A*E

Postfix expression: ABC*D/+F-AE*+

You might also like