DSA 1-7 Programs
DSA 1-7 Programs
Program
#include <stdio.h>
#include <stdlib.h>
struct Day
{
char *dayName;
int date;
char *activity;
};
void create(struct Day *day)
{
day->dayName = (char *)malloc(sizeof(char) * 20);
day->activity = (char *)malloc(sizeof(char) * 100);
printf("Enter the day name: ");
scanf("%s", day->dayName);
printf("Enter the date: ");
scanf("%d", &day->date);
printf("Enter the activity for the day: ");
scanf(" %[^\n]s", day->activity);
}
void read(struct Day *calendar, int size)
{
for (int i = 0; i < size; i++)
{
printf("Enter details for Day %d:\n", i + 1);
create(&calendar[i]);
}
}
void display(struct Day *calendar, int size)
{
printf("\nWeek's Activity Details:\n");
for (int i = 0; i < size; i++) {
printf("Day %d:\n", i + 1);
printf("Day Name: %s\n", calendar[i].dayName);
printf("Date: %d\n", calendar[i].date);
printf("Activity: %s\n", calendar[i].activity);
printf("\n");
}
}
void freeMemory(struct Day *calendar, int size) {
for (int i = 0; i < size; i++)
{
free(calendar[i].dayName);
free(calendar[i].activity);
}
} int main()
{
int size;
printf("Enter the number of days in the week: ");
scanf("%d", &size);
struct Day *calendar = (struct Day *)malloc(sizeof(struct Day) * size);
if (calendar == NULL) {
printf("Memory allocation failed. Exiting program.\n");
return 1;
}
read(calendar, size);
display(calendar, size);
freeMemory(calendar, size);
free(calendar);
return 0;
}
OUTPUT
Enter the number of days in the week: 7
Enter details for Day 1:
Enter the day name: Sunday
Enter the date: 1
Enter the activity for the day: Learning
Enter details for Day 2:
Enter the day name: Monday
Enter the date: 2
Enter the activity for the day: Coding
Enter details for Day 3:
Enter the day name: Tuesday
Enter the date: 3
Enter the activity for the day: Testing
Enter details for Day 4:
Enter the day name: Wednesday
Enter the date: 4
Enter the activity for the day: Debugging
Enter details for Day 5:
Enter the day name: Thrusday
Enter the date: 5
Enter the activity for the day: Publishing
Enter details for Day 6:
Enter the day name: Friday
Enter the date: 6
Enter the activity for the day: Marketing
Enter details for Day 7:
Enter the day name: Saturday
Enter the date: 7
Enter the activity for the day: Earning
PROGRAM
#include<stdio.h>
char str[50], pat[20], rep[20], res[50];
int c = 0, m = 0, i = 0, j = 0, k, flag = 0;
void stringmatch()
{
while (str[c] != '\0')
{
if (str[m] == pat[i])
{
i++;
m++;
if (pat[i] == '\0')
{
flag = 1;
for (k = 0; rep[k] != '\0'; k++, j++)
{
res[j] = rep[k];
}
i = 0;
c = m;
}
}
else
{
res[j] = str[c];
j++;
c++;
m = c;
i = 0;
}
}
res[j] = '\0';
}
void main()
{
printf("Enter the main string:");
gets(str);
printf("\nEnter the pat string:");
gets(pat);
printf("\nEnter the replace string:");
gets(rep);
printf("\nThe string before pattern match is:\n %s", str);
stringmatch();
if (flag == 1)
printf("\nThe string after pattern match and replace is: \n %s ", res);
else
printf("\nPattern string is not found");
}
Sample Output1
Enter the main string: GOOD MORNING
Sample Output 2
Enter the MAIN string: hi vcet
Enter a PATTERN string: bye
Enter a REPLACE string: : hello
Pattern doesn't found!!
Experiment-3
3. Develop a menu driven Program in C for thefollowing 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
PROGRAM CODE:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define max_size 4
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();
int 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)
{
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
if(flag)
printf("\nThe poped element: %d\t",item);
temp=top;
break;
case 3: pali();
top=temp;
break;
case 4: display();
break;
case 5: exit(0);
break;
default: printf("\nInvalid choice:\n");
break;
}
}
//return 0;
}
void push() //Inserting element into the stack
{
if(top==(max_size-1))
{
printf("\nStack Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
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
1.Push 2.Pop
3.Palindrome 4.Display 5.Exit
Enter your choice: 1
Enter the element to be inserted: 1
Enter your choice: 1
Enter the element to be inserted: 2
Enter your choice: 1
Enter the element to be inserted: 1
Enter your choice: 1
Enter the element to be inserted: 5
Enter your choice: 2 The poped element: 5
Enter your choice: 4
The stack elements are:
1
2
1
Enter your choice: 3 Numbers= 1
Numbers= 2
Numbers= 1 reverse operation : reverse array : 1
2
1
check for palindrome : It is palindrome number
Enter your choice: 5
Exit
Experiment-4
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.
PROGRAM CODE:
Sample Output 1
Read the Infix Expression
(a+b)*c/d^5%1
Given Infix Expn: (a+b)*c/d^5%1
Postfix Expn: ab+c*d5^/1%
Sample Output 2
Read the Infix Expression
(a+(b-c)*d)
Given Infix Expn: (a+(b-c)*d)
Postfix Expn: abc-d*+
EXPERIMENT:5
5. a. Develop and Implement a Program in C for the following Stack Applications a. Evaluation of Suffix expression
with single digit operands and operators: +, -, *, /, %, ^
b. Solving Tower of Hanoi problem with n disks
Sample Output 1
Insert a postfix notation :: 22^32*+
Result :: 10
Sample Output 2
Insert a postfix notation :: 23+
Result :: 5
b. Solving Tower of Hanoi problem with n disks
PROGRAM CODE 5B
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void tower(int n, int source, int temp, int destination);
void tower(int n, int source, int temp, int 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("\n Enter the number of discs: \n\n");
scanf("%d", &n);
printf("\n The sequence of moves involved in the Tower of Hanoi are\n");
tower(n, 'A', 'B', 'C');
printf("\n\n Total Number of moves are: %d\n", (int)pow(2,n)-1);
}
Sample Output
Enter the number of discs: 3
The sequence of moves involved in the Tower of Hanoi are
Move disc 1 from A to C
PROGRAM CODE:
#include <stdio.h>
#include <stdlib.h>
#define max 5
int q[max],f=-1,r=-1;
void ins()
{
if(f==(r+1)%max)
printf("\nQueue overflow");
else
{
if(f==-1)
f++;
r=(r+1)%max;
printf("\nEnter element to be inserted:");
scanf("%d",&q[r]);
}
}
void del()
{
if(r==-1)
printf("\nQueue underflow");
else
{
printf("\nElemnt deleted is:%d",q[f]);
if(f==r)
f=r=-1;
else
f=(f+1)%max;
}
}
void disp()
{
if(f==-1)
printf("\nQueue empty");
else
{
int i;
printf("\nQueue elements are:\n");
for(i=f;i!=r;i=(i+1)%max)
printf("%d\t",q[i]);
printf("%d",q[i]);
printf("\nFront is at:%d\nRear is at:%d",q[f],q[r]);
}
}
int main()
{
printf("\nCircular Queue operations");
printf("\n1.Insert");
printf("\n2.Delete");
printf("\n3.Display");
printf("\n4.Exit");
int ch;
do
{
printf("\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:ins();break;
case 2:del();break;
case 3:disp();break;
case 4:exit(0);
default:printf("\nInvalid choice...!");
}
}while(1);
return 0;
}
Sample Output 1
Circular Queue operations
1.Insert
2.Delete
3.Display
4.Exit
Enter choice: 1
Enter element to be inserted:10
Enter choice: 1
Enter element to be inserted:20
Enter choice: 1
Enter element to be inserted:30
Enter choice: 3
Queue elements are:
10 20 30
Front is at:10
Rear is at:30
Enter choice: 2
Elemnt deleted is:10
Enter choice: 3
Queue elements are:
20 30
Front is at:20
Rear is at:30
Enter choice: 4
EXPERIMENT: 7
7. Develop a menu driven Program in C for the followingoperations on Singly Linked List (SLL) of
Student Data with the fields: USN, Name, Branch, 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 and Deletion at End of SLL
d. Perform Insertion and Deletion at Front of SLL
e. Demonstrate how this SLL can be used as STACK and QUEUE
f. Exit
PROGRAM CODE:
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
struct stud
{
char usn[11],name[15],branch[4],phno[11];
int sem;
struct stud *next;
}*f=NULL,*r=NULL,*t=NULL;
void ins(int ch)
{
t=(struct stud*)malloc(sizeof(struct stud));
printf("\nEnter USN:");
scanf("%s",t->usn);
printf("Enter Name:");
scanf("%s",t->name);
printf("Enter Branch:");
scanf("%s",t->branch);
printf("Enter Sem:");
scanf("%d",&t->sem);
printf("Enter Phno:");
scanf("%s",t->phno);
t->next=NULL;
if(!r)
f=r=t;
else
{
if(ch)
{
r->next=t;
r=t;
}
else
{
t->next=f;
f=t;
}
}
}
void del(int ch)
{
if(!f)
printf("\nList Empty");
else
{
struct stud *t1;
if(f==r)
{
t1=f;
f=r=NULL;
}
else if(ch)
{
t1=r;
for(t=f;t->next!=r;t=t->next)
r=t;
r->next=NULL;
}
else
{
t1=f;
f=f->next;
}
printf("\nElement deleted is:\n");
printf("USN:%s\nName:%s\nBranch:%s\nSem:%d\nPhno:%s\n",t1->usn,t1->name,t1->branch,t1->sem,
t1->phno);
free(t1);
}
}
void disp()
{
if(!f)
printf("\nList Empty!!!");
else
printf("\nList elements are:\n");
for(t=f;t;t=t->next)
printf("\nUSN:%s\nName:%s\nBranch:%s\nSem:%d\nPhno:%s\n",t->usn,t->name,t->branch,t->sem,
t->phno);
}
void main()
{
int ch,n,i;
printf("\n........Menu..........,\n");
printf("1.Create\n");
printf("2.Display\n");
printf("3.Insert at end\n");
printf("4.Delete at end\n");
printf("5.Insert at beg\n");
printf("6.Delete at beg\n");
printf("7.Exit\n");
while(1)
{
printf("\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter no. of Students:");
scanf("%d",&n);
for(i=0;i<n;i++)
ins(0);
break;
case 2:disp();
break;
case 3:ins(1);
break;
case 4:del(1);
break;
case 5:ins(0);
break;
case 6:del(0);
break;
case 7:exit(0);
default:printf("\nInvalid choice!!!!");
}
}
}
OUTPUT
........Menu..........,
1.Create
2.Display
3.Insert at end
4.Delete at end
5.Insert at beg
6.Delete at beg
7.Exit
Enter choice: 1
Enter no. of Students: 2
Enter USN: 004
Enter Name: abc
Enter Branch: CSE
Enter Sem: 3
Enter Phno: 99004455
Enter USN: 005
Enter Name: RAZI
Enter Branch: CSE
Enter Sem: 4
Enter Phno: 88552211