0% found this document useful (0 votes)
16 views60 pages

Dsa Iot Manual (New) .............

Uploaded by

abdvilliers64
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)
16 views60 pages

Dsa Iot Manual (New) .............

Uploaded by

abdvilliers64
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/ 60

PROGRAM 1.

Develop a Program 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).

#include <stdio.h>

#include <string.h>

struct Day {

char name[20];

int date;

char activity[100];

};

int main() {

struct Day calendar[7];

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

calendar[0].date = 1;

strcpy(calendar[0].activity, "Work from 9am to 5pm");

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

calendar[1].date = 2;

strcpy(calendar[1].activity, "Meeting at 10Am");

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

calendar[2].date = 3;

strcpy(calendar[2].activity, "Gym at 6PM");

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

calendar[3].date = 4;

strcpy(calendar[3].activity, "Dinnerr with friend at 7PM");


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

calendar[4].date = 5;

strcpy(calendar[4].activity, "Movie night at 8PM");

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

calendar[5].date = 6;

strcpy(calendar[5].activity, "Weekend getaway");

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

calendar[6].date = 7;

strcpy(calendar[6].activity, "Relax and Recharge");

printf("Calendar:\n");

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

printf(" %s \t Date: %d \t %s\n", calendar[i].name, calendar[i].date,

calendar[i].activity);

return 0;

OUTPUT:-

Calender:

Monday Date : 1 Work from 9am to 5pm

Tuesday Date : 2 Meeting at 10 am

Wednesday Date : 3 gym at 6pm

Thursday Date : 4 Dinner with friend at 7pm

Friday Date : 5 Movie night at 8pm

Saturday Date : 6 Weekend get away

Sunday Date : 7 relax and Recharge

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.

#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 day name 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("Day Name: %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 day name for day 1: monday

Enter date for day 1: 01

Enter activity for day 1: waking up

Enter day name for day 2: tuesday

Enter date for day 2: 02

Enter the activity for the day: brushing

Enter day name for day 3: wednesday

Enter date for day 3: 03

Enter the activity for the day: bathing


Enter day name for day 4: thursday

Enter date for day 4: 04

Enter the activity for the day: eating

Enter day name for day 5: friday

Enter date for day 5: 05

Enter the activity for the day: watching tv

Enter day name for day 6: saturday

Enter date for day 6: 06

Enter the activity for the day: crying

Enter day name for day 7: sunday

Enter date for day 7: 07

Enter the activity for the day: sleeping

Activity details for the week:

Day Name : Monday

date : 01

Activity : Waking up

Day Name : Tuesday

date : 02

Activity : brushing

Day Name : wednesday

date : 03

Activity : bathing

Day Name : thursday

date : 04

Activity : eating

Day Name : Friday

date : 05
Activity : watching tv

Day Name : saturday

date : 06

Activity : crying

Day Name : sunday

date : 07

Activity : sleeping

2. Develop a Program in C for the following operations on Strings.

a) ReadamainString (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

#include<stdio.h>

void main()

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

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

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

gets(STR);

printf("Enter a PATTERN string:\n");

gets(PAT);

printf("Enter a REPLACE string:\n");

gets(REP);

i=m=c=j=0;
while(STR[c]!='\0')

if(STR[m]==PAT[i])

i++;

m++;

flag=1;

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

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

ans[j]=REP[k];

i=0;

c=m;

else

ans[j]=STR[c];

j++;

c++;

m=c;

i=0;

if(flag==0)

printf("Pattern doesn't found!!!");


}

else

ans[j]='\0';

printf("\n The RESULTANT string is:%s\n", ans);

OUTPUT : -

Enter the MAIN string:

Good Morning.

Enter a PATTERN string:

morning

Enter replace string:

evening

The RESULTANT string is: Good evening.

3 DevelopamenudrivenProgram in Cfor 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>
#define MAX 20

int stack_full(int top);

int stack_empty(int top);

void push(int stack[], int *top, int ele);

int pop(int stack[], int *top);

int is_palindrome(int stack[], int top);

void display(int stack[], int top);

void main()

int stack[MAX], top =-1, ele, ch;

for(;;)

printf("\nMenu\n");

printf("1. Push\n2. Pop\n3. Check Palindrome\n4. Stack Status\n5. Display\n6. Exit\n");

scanf("%d", &ch);

switch(ch)

case 1:

if(stack_full(top))

printf("Stack Full\n");

else

printf("Enter an Element\n");

scanf("%d", &ele);

push(stack, &top, ele);

break;

case 2:
if(stack_empty(top))

printf("Stack Empty\n");

else

ele = pop(stack, &top);

printf("Deleted Element is %d", ele);

break;

case 3:

if(stack_empty(top))

printf("Stack Empty\n");

else if(is_palindrome(stack, top))

printf("Stack is Palindrome\n");

display(stack, top);

else

printf("Stack is not Palindrome\n");

break;

case 4:

if(stack_empty(top))

printf("Stack Empty\n");

else if(stack_full(top))

printf("Stack Full\n");

else

printf("Stack contains %d elements\n", top+1);

break;

case 5:
if(stack_empty(top))

printf("Stack Empty\n");

else

display(stack, top);

break;

case 6:

exit(0);

int stack_full(int top)

if(top == MAX-1)

return 1;

return 0;

int stack_empty(int top)

if(top ==-1)

return 1;

return 0;

void push(int stack[], int *top, int ele)

stack[++(*top)] = ele;

int pop(int stack[], int *top)

{
return stack[(*top)--];

int is_palindrome(int stack[], int top)

int i, not_palindrome=0;

for(i=0; i<=top/2; i++)

if(stack[i] != stack[top-i])

not_palindrome = 1;

break;

if(not_palindrome)

return 0;

else

return 1;

void display(int stack[], int top)

int i;

for(i=0; i <= top; i++)

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

printf("\n");

}
OUTPUT :-

Menu

1.Push

2.Pop

3.Check Palindrome

4.Stack Status

5.Display

6.Exit

Enter an element

10

Menu

1.Push

2.Pop

3.Check Palindrome

4.Stack Status

5.Display

6.Exit

Enter an element

20

Menu

1.Push

2.Pop

3.Check Palindrome

4.Stack Status

5.Display

6.Exit
2

Deleted Element is 20

Menu

1.Push

2.Pop

3.Check Palindrome

4.Stack Status

5.Display

6.Exit

Menu

1.Push

2.Pop

3.Check Palindrome

4.Stack Status

5.Display

6.Exit

Stack contains 1 elements

Menu

1.Push

2.Pop

3.Check Palindrome

4.Stack Status

5.Display

6.Exit

10
Menu

1.Push

2.Pop

3.Check Palindrome

4.Stack Status

5.Display

6.Exit

4 DevelopaProgram inCfor 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.

#define SIZE 50

#include <ctype.h>

#include <stdio.h>

char s[SIZE];

int top = -1;

void push(char elem)

s[++top] = elem;

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;

void main()

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

int i = 0, k = 0;

clrscr();

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();

else

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

pofx[k++] = pop();

push(ch);

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

pofx[k++] = pop();

pofx[k] = '\0';

printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);

getch();

OUTPUT :-

Read the Infix Expression ? a*b+c^d

Given Infix Expn: a*b+c^d

Postfix Expn: ab*cd^+

5.DevelopaProgram in C for the following Stack Applications

a.Evaluation of Suffix expression with single digit operands and operators: +,-,*,/,%,^.

#include<stdio.h>

#include<stdlib.h>
#include<math.h>

int i, top =-1;

int op1, op2, res, s[20];

char postfix[90], symb;

void push(int item)

top = top+1;

s[top] = item;

int pop()

int item;

item = s[top];

top = top-1;

return item;

void main()

printf("\nEnter a valid postfix expression:\n");

scanf("%s", postfix);

for(i=0; postfix[i]!='\0'; i++)

symb =postfix[i];

if(isdigit(symb))

push(symb- '0');

else
{

op2 =pop();

op1 = pop();

switch(symb)

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 '$':

case '^':

push(pow(op1, op2));

break;

default : push(0);

}
res = pop();

printf("\n Result = %d", res);

OUTPUT :-

Enter a valid postfix expression:

657-+/82+*3$5

Result =21957

Enter a valid postfix expression:

623+-382/+2$3+

Result = 172

Program 5b:

b. Solving Tower of Hanoi problem with n disks

#include<stdio.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("\nMove disc %d from %c to %c", n, source, destination);

tower(n-1, temp, source, destination);

void main ()

int n;
printf("\nEnter the number of discs: \n\n");

scanf("%d", &n);

printf("\nThe sequence of moves involved in the Tower of Hanoi are\n");

tower(n, 'A', 'B', 'C');

printf("\n\nTotal Number of moves are: %d\n", (int)pow(2,n)-1);

OUTPUT:

Enter the number of discs:

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

Program 6:

Develop a menu driven Program in C for the following operations on Circular QUEUE of

Characters (Array Implementation of Queue with maximum size MAX)

a. Insert an Element onto 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

Supportthe program with appropriate functions for each of the above operations.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define SIZE 5

int q[SIZE], i, r=-1, f=0, option, count=0, j;

int main( )

for(;;)

printf("\n 1.Insert 2.Delete\n 3.Display 4.Exit");

printf("\nEnter your option:");

scanf("%d",&option);

switch(option)

case 1:

if(count==SIZE)

printf("\n Q is Full\n");

else

r=(r+1)%SIZE;

printf("\nEnter the item:");

scanf("%d",&q[r]);

count++;

break;
case 2:

if(count==0)

printf("\nQ is empty\n");

else

printf("\nDeleted item is: %d",q[f]);

count--;

f=(f+1)%SIZE;

break:

case 3:

if(count==0)

printf("\nQ is Empty\n");

else

i=f;

for(j=0;j<count;j++) {

printf(" %d",q[i]); \ i=(i+1)%SIZE);

break;

default: exit(0);

Output:

1.Insert 2.Delete 3.Display 4.Exit

Enter your option:1


Enter the item:10

1.Insert 2.Delete 3.Display 4.Exit

Enter your option:1

Enter the item:20

1.Insert 2.Delete 3.Display 4.Exit

Enter your option:2

Deleted item is:10

1.Insert 2.Delete 3.Display 4.Exit

Enter your option:3

20

1.Insert 2.Delete 3.Display 4.Exit

Enter your option:4

Program 7:

Develop a menu driven Program in C for the following operations on Singly Linked List (SLL) of Student
Data with the fields: USN, Name, Programme, 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 / Deletion at End of SLL

d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)

e. Exit

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

typedef struct{

int usn;

char name[20];

char branch[20];
int semester;

char phone[20];

}STUDENT;

struct node{

int usn;

char name[20];

char branch[20];

int semester;

char phone[20];

struct node *link;

};

typedef struct node*NODE;

NODE getnode(){

NODE x;

x=(NODE)malloc(sizeof(struct node));

if(x==NULL){

printf("out of memory\n");

exit(0);

return x;

NODE insert_front(STUDENT item,NODE first){

NODE temp; temp=getnode();

temp->usn=item.usn;

strcpy(temp->name,item.name);

strcpy(temp->branch,item.branch);
temp->semester=item.semester;

strcpy(temp->phone,item.phone);

temp->link=NULL;

if(first==NULL)

return temp; temp->link=first;

return temp;

NODE insert_rear(STUDENT item,NODE first){

NODE temp,cur; temp=getnode();

temp->usn=item.usn;

strcpy(temp->name,item.name);

strcpy(temp->branch,item.branch);

temp->semester=item.semester;

strcpy(temp->phone,item.phone);

temp->link=NULL;

if(first==NULL)

return temp; cur=first;

while(cur->link!=NULL){

cur=cur->link;

cur->link=temp;

return first;

NODE delete_front(NODE first){

NODE temp;

if(first==NULL){

printf("student list is empty\n");


return NULL;

temp=first;

temp=temp->link;

printf("delete student record:USN=%d\n",first->usn);

free(first);

return temp;

NODE delete_rear(NODE first){

NODE cur,prev;

if(first==NULL){

printf("student list is empty cannot delete\n");

return first;

if(first->link==NULL){

printf("delete student record:USN=%d\n",first->usn);

free(first);

return NULL;

prev=NULL;

cur=first;

while(cur->link!=NULL){

prev=cur;

cur=cur->link;

printf("delete student record:USN=%d\n",cur->usn);

free(cur);
prev->link=NULL; return first;

void display(NODE first){

NODE cur; int count=0;

if(first==NULL){

printf("student list is empty\n");

return;

cur=first;

while(cur!=NULL){

printf("%d\t%s\t%s\t%d\t%s\t\n",cur->usn,cur->name,cur->branch,cur->semester,cur-
>phone);

cur=cur->link;

count++;

printf("numbrt of students=%d\n",count);

void main(){

NODE first;

int choice;

STUDENT item;

first=NULL;

for(;;){

printf("1.insert_front\n2.insert_rear\n3.delete_front\n4.delete_rear\n5.display\n6.exit\n");

printf("Enter the choice\n");

scanf("%d",&choice);

switch(choice){
case 1: printf("USN :\n");

scanf("%d",&item.usn);

printf("name :\n");

scanf("%s",item.name);

printf("branch :\n");

scanf("%s",item.branch);

printf("semester:\n");

scanf("%d",&item.semester);

printf("phone :\n");

scanf("%s",item.phone);

first=insert_front(item,first);

break;

case 2: printf("USN :\n");

scanf("%d",&item.usn);

printf("name :\n");

scanf("%s",item.name);

printf("branch :\n");

scanf("%s",item.branch);

printf("semester:\n");

scanf("%d",&item.semester);

printf("phone :\n");

scanf("%s",item.phone);

first=insert_rear(item,first);

break;

case 3: first=delete_front(first);

break;

case 4: first=delete_rear(first);

break;
case 5: display(first);

break;

default:exit(0);

OUTPUT:

1.insert_front

2.insert_rear

3.delete_front

4.delete_rear

5.display

6.exit

Enter the choice

USN :

12

name :

ram

branch :

iot

semester:

phone :

9875423610

1.insert_front

2.insert_rear

3.delete_front
4.delete_rear

5.display

6.exit

Enter the choice

USN :

14

name :

raj

branch :

cs

semester:

phone :

7584236915

1.insert_front

2.insert_rear

3.delete_front

4.delete_rear

5.display

6.exit

Enter the choice

12 ram iot 3 9875423610

14 raj cs 3 7584236915

numbrt of students=2

1.insert_front

2.insert_rear
3.delete_front

4.delete_rear

5.display

6.exit

Enter the choice

delete student record:USN=12

1.insert_front

2.insert_rear

3.delete_front

4.delete_rear

5.display

6.exit

Enter the choice

Program 8:-

Develop a menu driven Program in C for the following operations on Doubly Linked List (DLL) of
Employee Data with the fields: SSN, Name, Dept, Designation, Sal, PhNo

a. Create a DLL of N Employees Data by using end insertion.

b. Display the status of DLL and count the number of nodes in it

c. Perform Insertion and Deletion at End of DLL

d. Perform Insertion and Deletion at Front of DLL

e. Demonstrate how this DLL can be used as Double Ended Queue.

f. Exit

#include <stdio.h>

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

struct Enode{

char ssn[15];

char name[20];

char dept[5];

char designation[10];

int salary;

long long int phno;

struct Enode *left;

struct Enode *right;

}*head=NULL;

struct Enode *tail,*temp1,*temp2;

void create(char [],char [],char [],char [],int ,long long int);

void ins_beg(char [],char [],char [],char [],int ,long long int);

void ins_end(char [],char [],char [],char [],int ,long long int);

void del_beg();

void del_end();

void display();

int count=0;

void main(){

int choice;

char s[15],n[20],dpt[5],des[10];

int sal;

long long int p;

printf("1.Create\n2.Display\n3.Insert at beginning\n4.Insert at End\n5.Delete at


beginning\n6.Delete at End\n7.Exit\n");

while(1){

printf("\nEnter your choice\n");


scanf("%d",&choice);

switch(choice){

case 1:

printf("Enter the required data(Emp no,Name,Dept,Desig,sal,phone\n");

scanf("%s%s%s%s%d%lld",s,n,dpt,des,&sal,&p); create(s,n,dpt,des,sal,p);

break;

case 2:

display();

break;

case 3:

printf("Enter the required data (Emp no,Name,Dept,Desig,sal,phone\n");

scanf("%s%s%s%s%d%lld",s,n,dpt,des,&sal,&p);

ins_beg(s,n,dpt,des,sal,p);

break;

case 4:

printf("Enter the required data(Emp no,Name,Dept,Desig,sal,phone\n");

scanf("%s%s%s%s%d%lld",s,n,dpt,des,&sal,&p); ins_end(s,n,dpt,des,sal,p);

break;

case 5:

del_beg();

break;

case 6:

del_end();

break;

case 7:

exit(0);

}
}

void create(char s[15],char n[20],char dpt[5],char des[10],int sal,long long int p){

if(head==NULL){

head=(struct Enode *)malloc(1*sizeof(struct Enode));

strcpy(head->ssn,s);

strcpy(head->name,n);

strcpy(head->dept,dpt);

strcpy(head->designation,des);

head->salary=sal;

head->phno=p;

head->left=NULL; head->right=NULL;

tail=head;

else{

temp1=(struct Enode *)malloc(1*sizeof(struct Enode));

strcpy(temp1->ssn,s);

strcpy(temp1->name,n);

strcpy(temp1->dept,dpt);

strcpy(temp1->designation,des);

temp1->salary=sal;

temp1->phno=p; tail->right=temp1;

temp1->right=NULL;

temp1->left=tail;

tail=temp1;

void display(){
temp1=head;

printf("Employee Details \n");

while(temp1!=NULL){

printf(" \n");

printf("%s\n%s\n%s\n%s\n%d\n%lld\n",temp1->ssn,temp1->name,temp1->dept,temp1-
>designation,temp1->salary,temp1->phno);

printf(" "); temp1=temp1->right;

void ins_beg(char s[15],char n[20],char dpt[5],char des[10],int sal,long long int p){

temp1=(struct Enode *)malloc(1*sizeof(struct Enode));

strcpy(temp1->ssn,s);

strcpy(temp1->name,n);

strcpy(temp1->dept,dpt);

strcpy(temp1->designation,des);

temp1->salary=sal;

temp1->phno=p;

temp1->right=head;

head->left=temp1;

head=temp1;

temp1->left=NULL;

void ins_end(char s[15],char n[20],char dpt[5],char des[10],int sal,long long int p){

temp1=(struct Enode *)malloc(1*sizeof(struct Enode));

strcpy(temp1->ssn,s);

strcpy(temp1->name,n);

strcpy(temp1->dept,dpt);

strcpy(temp1->designation,des);
temp1->salary=sal;

temp1->phno=p;

tail->right=temp1;

temp1->left=tail;

temp1->right=NULL;

tail=temp1;

void del_beg(){

temp1=head->right;

free(head); head=temp1;

head->left=NULL;

void del_end(){

temp1=tail->left;

free(tail);

tail=temp1;

tail->right=NULL;

OUTPUT:

Program 9:

Develop a Program in C for the following operations on Singly Circular Linked List(SCLL) with header
nodes a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y 2z-4yz5+3x3yz+2xy5z-2xyz3

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int coef,px,py,pz, x,y,z,i;

int val;
struct node

int coef,px, py,pz;

struct node *next;

};

typedef struct node NODE;

NODE *first;

void insert(int coef,int px, int py, int pz)

NODE *temp,*cur;

temp= (NODE *)malloc(sizeof(NODE));

temp->coef=coef;

temp->px=px;

temp->py=py;

temp->pz=pz;

if(first==NULL

temp->next=temp;

first=temp;

return;

if(first->next==first)

first->next=temp;

temp->next=first;

cur=first;
while(cur->next!=first)

cur=cur->next;

cur->next=temp;

temp->next=first;

return;

void display()

NODE *cur;

if(first==NULL

printf("List is empty\n");

return;

cur=first;

while(cur->next!=first)

printf("%d ",cur->coef);

printf(" x^%d",cur->px);

printf(" y^%d",cur->py);

printf(" z^%d + ",cur->pz);

cur=cur->next;

printf("%d ",cur->coef);

printf(" x^%d",cur->px);
printf(" y^%d",cur->py);

printf(" z^%d\n",cur->pz);

return;

int evaluate(int x, int y, int z)

NODE *cur; int v,s=0, v1,v2,v3;

if(first==NULL)

printf("List is empty\n");

return 0;

cur=first;

while(cur->next!=first)

v=cur->coef*pow(x, cur->px)*pow(y, cur->py)*pow(z,cur->pz);

s=s+v;

cur=cur->next;

v=cur->coef*pow(x, cur->px)*pow(y, cur->py)*pow(z,cur->pz);

s=s+v;

return s;

int main()

int coef,px,py,pz,

x,y,z,i;

int val;
first=NULL;

while(1)

printf("1. Insert polynomial at end\n");

printf("2. Display\n");

printf("3. Evaluate\n");

printf("4. Exit\n");

printf("Enter Choice= \t");

scanf("%d",&i);

switch(i)

case 1 : printf("Enter Coefficient= \t");

scanf("%d",&coef);

printf("Enter powers of x y z values= \t");

scanf("%d%d%d",&px, &py,&pz);

insert(coef,px,py,pz);

break;

case 2 : display();

break;

case 3 :

printf("\n Enter x y & z values for evaluation: \t");

scanf("%d%d%d",&x,&y,&z);

val=evaluate(x,y,z);

printf("\nValue=%d\n",val);

break;

case 4 :

return 0;

default :
printf(" Wrong choice. Enter 1,2 3\n");

break;

OUTPUT:-

1. Insert polynomial at end

2. Display

3. Evaluate

4. Exit

Enter Choice = 1

Enter Coefficient = 4

Enter powers of x y z values = 3 3 2

1. Insert polynomial at end

2. Display

3. Evaluate

4. Exit

Enter Choice = 1

Enter Coefficient = 5

Enter powers of x y z values = 3 2 2

1. Insert polynomial at end

2. Display

3. Evaluate

4. Exit

Enter Choice = 1
Enter Coefficient = 3

Enter powers of x y z values = 2 2 2

1. Insert polynomial at end

2. Display

3. Evaluate

4. Exit

Enter Choice = 1

Enter Coefficient = 6

Enter powers of x y z values = 1 1 1

1. Insert polynomial at end

2. Display

3. Evaluate

4. Exit

Enter Choice = 2

4 x^3 y^3 z^2 + 5 x^3 y^2 z^2 + 3 x^2 y^2 z^2 + 6 x^1 y^1 z^1

1. Insert polynomial at end

2. Display

3. Evaluate

4. Exit

Enter Choice = 3

Enter x y & z values for evaluation : 3 2 2

Value = 6120

1. Insert polynomial at end

2. Display

3. Evaluate
4. Exit

Enter Choice = 4

PROGRAM 9b:-

b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store theresult in
POLYSUM(x,y,z). Support the program with appropriate functions for each of the above operations.

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

struct node

int coef, px, py, pz,flag;

struct node *link;

};

typedef struct node * NODE;

NODE create_list(NODE head)

int i,n,cf,px,py,pz;

printf("Enter the number of terms : ");

scanf("%d",&n);

for(i=1;i<=n;i++)

printf("Enter the Co-ef, px, py, pz : ");

scanf("%d %d %d %d",&cf,&px,&py,&pz);

insert(head,cf,px,py,pz);

}
return head;

insert(NODE head,int cof,int x,int y, int z)

NODE cur,tmp;

tmp= (NODE)malloc(sizeof(struct node));

cur=head->link;

tmp->coef=cof;

tmp->px=x;

tmp->py=y;

tmp->pz=z;

tmp->flag=0;

while(cur->link!=head)

cur=cur->link;

cur->link=tmp;

tmp->link=head;

NODE add_poly(NODE h1,NODE h2,NODE h3)

NODE cur1,cur2,scf; cur1=h1->link; cur2=h2->link;

while(cur1 != h1)

if(cur2 == h2)

cur2=h2->link;

while(cur2 != h2)
{

if(cur1->px == cur2->px && cur1->py == cur2->py && cur1->pz == cur2->pz)

scf=cur1->coef+cur2->coef;

insert(h3,scf,cur1->px,cur1->py,cur1->pz);

cur2->flag=1;

cur2=h2->link;

break;

cur2=cur2->link;

if(cur1 == h1)

break;

if(cur2 == h2)

insert(h3,cur1->coef,cur1->px,cur1->py,cur1->pz);

cur1=cur1->link;

cur2=h2->link;

while(cur2 != h2)

if(cur2->flag==0)

insert(h3,cur2->coef,cur2->px,cur2->py,cur2->pz);

cur2=cur2->link;

return h3;
}

void display(NODE head)

NODE cur;

if(head->link==head)

printf("List is empty\n");

return;

cur=head->link;

while(cur != head)

if(cur->coef > 0)

printf(" +%dx^%dy^%dz^%d ",cur->coef,cur->px,cur->py,cur->pz);

else if (cur->coef < 0)

printf(" %dx^%dy^%dz^%d ",cur->coef,cur->px,cur->py,cur->pz);

cur=cur->link;

printf("\n");

void main()

int choice,data,item,pos; NODE head1,head2,head3;

head1=(NODE)malloc(sizeof(struct node));
head1->link=head1;

head2=(NODE)malloc(sizeof(struct node));

head2->link=head2;

head3=(NODE)malloc(sizeof(struct node));

head3->link=head3;

printf("\n1.Create Polynomial 1\n");

head1=create_list(head1);

printf("\n2.Create Polynomial 2\n");

head2=create_list(head2);

printf("\nPolynomial 1 is :");

display(head1);

printf("\nPolynomial 2 is :");

display(head2);

head3=add_poly(head1,head2,head3);

printf("\nAddition of two Polynomial is :");

display(head3);

OUTPUT:-

1.Create Polynomial 1

Enter the number of terms : 2

Enter the Co-ef, px, py, pz : 5 3 3 3

Enter the Co-ef, px, py, pz : 9 1 0 1

2.Create Polynomial 2
Enter the number of terms : 2

Enter the Co-ef, px, py, pz : 8 2 1 2

Enter the Co-ef, px, py, pz : 9 1 1 0

Polynomial 1 is : +5x^3y^3z^3 +9x^1y^0z^1

Polynomial 2 is : +8x^2y^1z^2 +9x^1y^1z^0

Addition of two Polynomial is : 5x^3y^3z^3 + 9x^1y^0z^1 + 8x^2y^1z^2 + 9x^1y^1z^0

PROGRAM 10:-

Develop a menu driven Program in C for the following operations on Binary Search Tree (BST)of
Integers. a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2

b. Traverse the BST in Inorder, Preorder and Post Order

c. Search the BST for a given element (KEY) and report the appropriate message

d. Exit

#include <stdio.h>

#include <stdlib.h>

struct BST

int data;

struct BST *left;

struct BST *right;

};

typedef struct BST NODE;

NODE *node;

NODE * createtree(NODE *node,int data)

if (node == NULL)
{

NODE *temp;

temp= (NODE*)malloc(sizeof(NODE));

temp->data = data;

temp->left = temp->right = NULL;

return temp;

if(data<(node->data))

node->left=createtree(node->left, data);

else if(data>node->data)

node -> right = createtree(node->right, data);

return node;

NODE* search(NODE *node, int data)

if(node == NULL)

printf("\nElement not found");

else if(data < node->data)

node->left=search(node->left, data);

}
else if(data > node->data)

node->right=search(node->right, data);

else

printf("\nElement found is: %d", node->data);

return node;

void inorder(NODE *node)

if(node != NULL)

inorder(node->left);

printf("%d\t", node->data);

inorder(node->right);

void preorder(NODE *node)

if(node!=NULL)

printf("%d\t", node->data);

preorder(node->left);

preorder(node->right);

}
}

void postorder(NODE *node)

if(node != NULL)

postorder(node->left);

postorder(node->right);

printf("%d\t", node->data);

void main()

int data, ch, i, n;

NODE *root=NULL;

while (1)

printf("\n1.Insertion in Binary Search Tree");

printf("\n2.Search Element in Binary Search Tree");

printf("\n3.Inorder\n4.Preorder\n5.Postorder\n6.Exit");

printf("\nEnter your choice: ");

scanf("%d", &ch);

switch (ch)

case 1:

printf("\nEnter N value: " );


scanf("%d", &n);

printf("\nEnter the values to create BST \n");

for(i=0;i<n;i++)

scanf("%d", &data);

root=createtree(root, data);

break;

case 2:

printf("\nEnter the element to search: ");

scanf("%d", &data);

root=search(root, data);

break;

case 3:

printf("\nInorder Traversal: \n");

inorder(root);

break;

case 4:

printf("\nPreorder Traversal: \n");

preorder(root);

break;

case 5:

printf("\nPostorder Traversal: \n");

postorder(root);

break;
case 6:exit(0);

default:

printf("\nWrong option");

break;

}}}

OUTPUT:-

1.Insertion in Binary Search Tree

2.Search Element in Binary Search Tree

3.Inorder

4.Preorder

5.Postorder

6.Exit

Enter your choice: 1

Enter N value: 5

Enter the values to create BST

28465

1.Insertion in Binary Search Tree

2.Search Element in Binary Search Tree

3.Inorder

4.Preorder

5.Postorder

6.Exit

Enter your choice: 2

Enter the element to search: 8

Element found is: 8


1.Insertion in Binary Search Tree

2.Search Element in Binary Search Tree

3.Inorder

4.Preorder

5.Postorder

6.Exit

Enter your choice: 2

Enter the element to search: 1

Element not found

1.Insertion in Binary Search Tree

2.Search Element in Binary Search Tree

3.Inorder

4.Preorder

5.Postorder

6.Exit

Enter your choice: 3

Inorder Traversal:

2 4 5 6 8

1.Insertion in Binary Search Tree

2.Search Element in Binary Search Tree

3.Inorder

4.Preorder

5.Postorder

6.Exit

Enter your choice: 4


Preorder Traversal:

2 8 4 6 5

1.Insertion in Binary Search Tree

2.Search Element in Binary Search Tree

3.Inorder

4.Preorder

5.Postorder

6.Exit

Enter your choice: 5

Postorder Traversal:

5 6 4 8 2

1.Insertion in Binary Search Tree

2.Search Element in Binary Search Tree

3.Inorder

4.Preorder

5.Postorder

6.Exit

Enter your choice: 6

PROGRAM 11:-

Design, develop and implement a Program in C for the following operations on Graph (G) of Cities Create
a Graph of N cities using Adjacency Matrix. Print all the nodes reachable from agiven starting node in a
digraph using BFS method.

#include<stdio.h>

#include<stdlib.h>

int n, a[10][10],i,j,source,s[10],choice,count;
void bfs(int n,int a[10][10],int source,int s[])

int q[10],u;

int front=1,rear=1;

s[source]=1;

q[rear]=source;

while(front<=rear)

u=q[front];

front=front+1;

for(i=1;i<=n;i++)

if(a[u][i]==1 && s[i]==0)

rear=rear+1;

q[rear]=i;

s[i]=1;

int main()

printf("Enter the number of nodes : ");

scanf("%d",&n);

printf("\n Enter the adjacency matrix\n");

for(i=1;i<=n;i++)
for (j=1;j<=n;j++)

scanf("%d",&a[i][j]);

while(1)

printf("\nEnter your choice\n");

printf("1.BFS\n 2.Exit\n");

scanf("%d",&choice);

switch(choice)

case 1: printf("\n Enter the source :");

scanf("%d",&source);

for(i=1;i<=n;i++)

s[i]=0;

bfs(n,a,source,s);

for(i=1;i<=n;i++)

if(s[i]==0)

printf("\n The node %d is not reachable",i);

else

printf("\n The node %d is reachable",i);

break;

case 2:

exit(0);

}}}
OUTPUT:-

Enter the number of nodes : 5

Enter the adjacency matrix

01010

00100

10000

00100

00110

Enter your choice

1.BFS

2.Exit

Enter the source :3

The node 1 is reachable

The node 2 is reachable

The node 3 is reachable

The node 4 is reachable

The node 5 is not reachable

Enter your choice

1.BFS

2.Exit

PROGRAM 12:-

You might also like