DS LAB Programs
DS LAB Programs
DS LAB Programs
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
struct Calendar
{
char *name;
int date;
char *activity;
};
void create( )
{
C=(struct Calendar *)malloc(7* sizeof(struct Calendar));
}
void read( )
{
char buffer[20];
for(i=0;i<7;i++)
{
printf("\n enter weekday");
scanf("%s",buffer);
(C+i)->name=(char*)malloc(strlen(buffer)+1);
strcpy((C+i)->name,buffer);
void main()
{
create();
read();
display();
free(C);
Output:
enter weekdayMonday
Enter the date 11
Enter the activityYoga
enter weekdayTuesday
Enter the date 12
Enter the activityYoga
enter weekdayWednesday
Enter the date 13
Enter the activityHIT
enter weekdayThursday
Enter the date 14
Enter the activityStrengthTraining
enter weekdayFriday
Enter the date 15
Enter the activityWeightTraining
enter weekdaySaturday
Enter the date 16
Enter the activityRest
enter weekdaySunday
Enter the date 17
Enter the activityMarathonRun
11 Monday Yoga
12 Tuesday Yoga
13 Wednesday HIT
14 Thursday StrengthTraining
15 Friday WeightTraining
16 Saturday Rest
17 Sunday MarathonRun
#include<stdio.h>
#include<string.h>
void stringmatch()
{
i = m = j = 0;
while(str[m] != '\0')
{
if(str[m] == pat[i]) // ...... matching
{
i++; m++;
if(pat[i] == '\0') //.....found occurrences.
{
flag = 1;
for(k = 0; rep[k] != '\0'; k++, j++)
ans[j] = rep[k];
i = 0;
}
}
else
ans[j++] = str[m++];
} //while end
ans[j] = '\0';
}
void main()
{
Output:
Run1
Enter a main string
apple is an apple
Enter a pattern string
app
Enter a replace string
coup
The resultant string is
couple is an couple
Run2
Enter a main string
apple is an apple
Enter a pattern string
dfg
Enter a replace string
coup
Pattern string NOT found
Program 3. Design, Develop and Implement 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>
#include<math.h>
# define maxsize 5
int stack[maxsize];
int top=-1;
int stackfull()
{
if (top==maxsize-1)
return 1;
else
return 0;
}
int stackempty()
{
if(top==-1)
return 1;
else
return 0;
}
int pop()
{
return stack[top--];
}
void display()
{
int i;
if(top==-1)
printf("\nStack Underflow");
else
{
for(i=top;i>=0;i--)
printf("\n%d",stack[i]);
}
}
void palindrome()
{
int num,i,rem,rev,temp;;
printf("\n Enter a number");
scanf("%d",&num);
temp=num;
while(num!=0)
{
rem=num%10;
push(rem);
num=num/10;
}
rev=0, i=0;
while (top!=-1)
{
rev=rev+pop()*pow(10,i);
i=i+1;
}
if(temp==rev)
printf("\n No is palindrome");
else
printf("\n No is not a plaidrome");
}
void main()
{
int choice, item;
while(1)
{
printf("\n STACK OPERATIONS\n");
printf("\n 1. PUSH");
printf("\n 2.POP");
printf("\n 3.DISPLAY");
printf("\n 4.PALINDROME");
printf("\n 5.EXIT");
printf("\n \nEnter ypur choice");
scanf("%d", &choice);
switch(choice)
{
case 1: if (stackfull()==1)
printf("\nStack Overflow");
else
{
printf("\n Enter element to pushed");
scanf("%d",&item);
push(item);
}
break;
case 2: if(stackempty()==1)
printf("\nStack Underflow");
else
printf("\n Element popped %d",pop());
break;
case 3: display();
break;
case 4: palindrome();
break;
case 5: exit(0);
Output:
STACK OPERATIONS
1. PUSH
2.POP
3.DISPLAY
4.PALINDROME
5.EXIT
1. PUSH
2.POP
3.DISPLAY
4.PALINDROME
5.EXIT
1. PUSH
2.POP
3.DISPLAY
4.PALINDROME
5.EXIT
STACK OPERATIONS
1. PUSH
2.POP
3.DISPLAY
4.PALINDROME
5.EXIT
1. PUSH
2.POP
3.DISPLAY
4.PALINDROME
5.EXIT
1. PUSH
2.POP
3.DISPLAY
4.PALINDROME
5.EXIT
1. PUSH
2.POP
3.DISPLAY
4.PALINDROME
5.EXIT
STACK OPERATIONS
1. PUSH
2.POP
3.DISPLAY
4.PALINDROME
5.EXIT
1. PUSH
2.POP
3.DISPLAY
4.PALINDROME
5.EXIT
1. PUSH
2.POP
3.DISPLAY
4.PALINDROME
5.EXIT
1. PUSH
2.POP
3.DISPLAY
4.PALINDROME
5.EXIT
1. PUSH
2.POP
3.DISPLAY
4.PALINDROME
5.EXIT
Enter your choice2
Stack Underflow
STACK OPERATIONS
1. PUSH
2.POP
3.DISPLAY
4.PALINDROME
5.EXIT
1. PUSH
2.POP
3.DISPLAY
4.PALINDROME
5.EXIT
1. PUSH
2.POP
3.DISPLAY
4.PALINDROME
5.EXIT
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
char stack[20];
int top=-1;
int priority(char c)
{
switch(c)
{
case '^':
case '$':return 3;
case '*':
case '/':
case '%':return 2;
case '+':
case '-':return 1;
void infixtopostfix()
{
char infix[20],postfix[20];
int i=0,j=0;
printf("\n Enter infix expression\n");
gets(infix);
for(i=0;infix[i]!='\0';i++)
{
if(isalpha(infix[i]))
postfix[j++]=infix[i];
else if(infix[i]=='(')
push(infix[i]);
else if(infix[i]==')')
{
while(stack[top]!='(')
postfix[j++]=pop();
pop();
}
else
{
postfix[j++]=pop();
push(infix[i]);
}
} // for loop end
while(top!=-1)
postfix[j++]=pop();
postfix[j]='\0';
printf("\n\n Postfix %s",postfix);
}
void main()
{
infixtopostfix();
Output:
RUN1
Enter infix expression
((a+b)*c^d)
Postfix ab+cd^*
RUN2
Enter infix expression
((a+b)*c)
Postfix ab+c*
a.EvaluationofSuffixexpressionwithsingledigitoperandsandoperators:+,-,*,/,%,^
#include <stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include <math.h>
int stack[20];
int top =-1;
char postfix[20];
int pop()
{
return(stack[top --]);
}
void postfixeval( )
{
int i=0,op1,op2,result;
printf("\n\nEnter the postfix Expression:");
gets(postfix);
for(i=0;postfix[i]!='\0';i++)
{
if(isdigit(postfix[i]))
push(postfix[i]-'0');
else
{
op2=pop();
op1=pop();
switch(postfix[i])
{
case'+': push(op1+op2);
break;
case '-': push(op1-op2);
break;
case'*': push(op1*op2);
break;
case'/': push(op1/op2);
break;
case'%':push(op1%op2);
break;
case'^':push(pow(op1,op2));
break;
} //switch end
}//for end
}
printf("\n Given sufffix Expression n: %s\n", postfix);
printf("\nResult after Evaluation:%d\n",stack[top]);
}
void main()
{
postfixeval();
}
Output:
Enter the postfix Expression: 62+5*
62+5*
Given sufffix Expression n: 62+5*
#include <stdio.h>
void main()
{
int n; // Number of disks
printf("Enter number of disks\n");
scanf("%d", &n);
towerohanoi(n, 'S', 'D', 'T');
}
Output:
#include<stdio.h>
#include<stdlib.h>
#define maxsize 5
char cq[maxsize];
int front,rear;
cq[rear]=item;
printf("\nRear=%d Front= %d\n",rear,front);
}
}
void deleteq()
{
if((front==-1))
printf("\n CIRCULAR QUEUE EMPTY\n");
else
{
printf("\nDELETED ELEMENT FROM QUEUE IS:%c\n", cq[front]);
cq[front]= ‘-‘;
if(front==rear)
front=rear=-1;
else
front=(front+1)%maxsize;
printf("\nRear = %d Front = %d \n",rear,front);
}
}
void display()
{
int i;
if((front==-1))
printf("QUEUE EMPTY\n");
else
{
printf("The Queue Elements are\n");
for(i=0;i<maxsize;i++)
printf("%c \t",cq[i]);
printf("\nRear = %d Front = %d \n",rear,front);
}
}
void main( )
{
int choice;
char item ;
front=-1; rear=-1;
while(1)
{
printf("\n\n-------QUEUE MENU \n");
printf("\n1.INSERT INTO QUEUE”);
printf(“\n2.DELETEF FROM QUEUE”);
printf(“\n3.DISPLAY QUEUE”);
printf(“\n4.EXIT”);
case 2: deleteq();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("\nInvalidChoice.\n");
} //switch end
}//while end
}
Output:
-------QUEUE MENU
1.INSERT INTO QUEUE
2.DELETE FROM QUEUE
3.DISPLAY QUEUE
4. EXIT
-------QUEUE MENU
-------QUEUE MENU
1.INSERT INTO QUEUE
2.DELETE FROM QUEUE
3.DISPLAY QUEUE
4. EXIT
-------QUEUE MENU
-------QUEUE MENU
-------QUEUE MENU
-------QUEUE MENU
-------QUEUE MENU
Rear = 4 Front = 1
-------QUEUE MENU
Rear = 4 Front = 2
-------QUEUE MENU
-------QUEUE MENU
1
ENTER THE QUEUE ELEMENT:F
Rear=0 Front= 2
-------QUEUE MENU
-------QUEUE MENU
-------QUEUE MENU
-------QUEUE MENU
-------QUEUE MENU
Rear = 1 Front = 3
-------QUEUE MENU
Rear = 1 Front = 4
-------QUEUE MENU
Rear = 1 Front = 0
-------QUEUE MENU
Rear = 1 Front = 1
-------QUEUE MENU
Rear = -1 Front = -1
-------QUEUE MENU
-------QUEUE MENU
-------QUEUE MENU