0% found this document useful (0 votes)
26 views

DSA 1-7 Programs

Data structures and algorithms programs

Uploaded by

Shankar Sourav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

DSA 1-7 Programs

Data structures and algorithms programs

Uploaded by

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

EXPERIMENT: 1

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).

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

Week's Activity Details:


Day 1:
Day Name: Sunday
Date: 1
Activity: Learning
Day 2:
Day Name: Monday
Date: 2
Activity: Coding
Day 3:
Day Name: Tuesday
Date: 3
Activity: Testing
Day 4:
Day Name: Wednesday
Date: 4
Activity: Debugging
Day 5:
Day Name: Thrusday
Date: 5
Activity: Publishing
Day 6:
Day Name: Friday
Date: 6
Activity: Marketing
Day 7:
Day Name: Saturday
Date: 7
Activity: Earning
EXPERIMENT-2

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 .

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

Enter the pat string: MORNING

Enter the replace string: EVENING

The string before pattern match is:


GOOD MORNING

The string after pattern match and replace is:


GOOD EVENING

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:

#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;
case '^': return 4;
}
}
void main() /* Main Program */
{
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
printf("\n\nRead the Infix Expression ? ");
scanf("%s", infx);
push('#');
while ((ch = infx[i++]) != '\0')
{
if (ch == '(') push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
else if (ch == ')')
{
while (s[top] != '(')
pofx[k++] = pop();
elem = pop(); /* Remove ( */
}
else /* Operator */
{
while (pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}
}
while (s[top] != '#') /* Pop from stack till empty */
pofx[k++] = pop();
pofx[k] = '\0'; /* Make pofx as valid string */
printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);
}

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

PROGRAM CODE 5A:


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
float compute(char symbol, float op1, float op2)
{
switch (symbol)
{
case '+': return op1 + op2;
case '-': return op1 - op2;
case '*': return op1 * op2;
case '/': return op1 / op2;
case '$':
case '^': return pow(op1,op2);
default : return 0;
}
}
void main()
{
float s[20], res, op1, op2;
int top, i;
char postfix[20], symbol;
printf("\nEnter the postfix expression:\n");
scanf ("%s", postfix);
top=-1;
for (i=0; i<strlen(postfix) ;i++)
{
symbol = postfix[i];
if(isdigit(symbol))
s[++top]=symbol - '0';
else
{
op2 = s[top--];
op1 = s[top--];
res = compute(symbol, op1, op2);
s[++top] = res;
}
}
res = s[top--];
printf("\nThe result is : %f\n", res);
}

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

Move disc 2 from A to B

Move disc 1 from C to B

Move disc 3 from A to C

Move disc 1 from B to A


Move disc 2 from B to C
Move disc 1 from A to C
Total Number of moves are: 7
Experiment - 6
6. Develop a menu driven Program in C for the followingoperations on Circular QUEUE of
Characters (Array Implementation of Queue with maximum size MAX)

a. Insert an Element on to Circular QUEUE


b. Delete an Element from Circular QUEUE c. Demonstrate Overflow and Underflow situations on
Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit Support the program with appropriate functions for each of the above operations.

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

Enter choice: 2 //Display


List elements are:
USN: 005
Name: RAZI
Branch: CSE
Sem: 4
Phno: 88552211
USN: 004
Name: abc
Branch: CSE
Sem: 3
Phno: 99004455
Enter choice: 5 // Insert at beg
Enter USN: 003
Enter Name : Pinku
Enter Branch: CSE
Enter Sem: 5
Enter Phno: 55662211
Enter choice: 2 //Display
List elements are:
USN:003
Name: Pinku
Branch: CSE
Sem:5
Phno:55662211
USN:005
Name:RAZI
Branch:CSE
Sem:4
Phno:88552211
USN:004
Name: abc
Branch: CSE
Sem:3
Phno: 9900
Enter choice: 6 // Delete at begining
Element deleted is:
USN: 003
Name: Pinku
Branch: CSE
Sem: 3
Phno: 55662211
Enter choice: 3 //Insert at End
Enter USN: 006
Enter Name: RANI
Enter Branch: CSE
Enter Sem: 3
Enter Phno: 88552255
Enter choice: 2 //Display
List elements are:
USN:005
Name: RAZI
Branch: CSE
Sem: 4
Phno: 02258884
USN: 004
Name: abc
Branch: CSE
Sem: 3
Phno: 990055
USN: 006
Name: RANI
Branch: CSE
Sem: 3
Phno: 88552255
Enter choice: 4 // Delete at End
Element deleted is:
USN: 006
Name: RANI
Branch: CSE
Sem: 3
Phno: 88552255
Enter choice: 2 //Display
List elements are:
USN: 005
Name: RAZI
Branch: CSE
Sem: 4
Phno: 88552211
USN: 004
Name: abc
Branch: CSE
Sem: 3
Phno: 99004455
Enter choice: 7 //Exit
Exit

You might also like