0% found this document useful (0 votes)
8 views67 pages

Dsa (Aids)

The document contains multiple C programs that demonstrate various data structures and algorithms, including a calendar management system using structures and dynamic memory allocation, string pattern matching, stack operations, array operations, infix to postfix expression conversion, and circular queue operations. Each program includes functions for creating, reading, displaying, inserting, deleting, and managing data, along with handling overflow and underflow situations. The programs are designed to be menu-driven, allowing user interaction for performing different operations.

Uploaded by

adilamil15
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)
8 views67 pages

Dsa (Aids)

The document contains multiple C programs that demonstrate various data structures and algorithms, including a calendar management system using structures and dynamic memory allocation, string pattern matching, stack operations, array operations, infix to postfix expression conversion, and circular queue operations. Each program includes functions for creating, reading, displaying, inserting, deleting, and managing data, along with handling overflow and underflow situations. The programs are designed to be menu-driven, allowing user interaction for performing different operations.

Uploaded by

adilamil15
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/ 67

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>

// Structure definition for each day

struct Day {

char *name; // dynamically allocated string for day name

int date; // date of the day

char *activity; // dynamically allocated string for activity description

};

int main() {

int i;

struct Day *calendar[7];

// Initialize each element of the calendar array

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

calendar[i] = (struct Day *)malloc(sizeof(struct Day));

// Assume the day names and activities for demonstration purposes

switch (i) {

case 0:
calendar[i]->name = strdup("Monday");

calendar[i]->date = 1;

calendar[i]->activity = strdup("Work");

break;

case 1:

calendar[i]->name = strdup("Tuesday");

calendar[i]->date = 2;

calendar[i]->activity = strdup("Meeting");

break;

case 2:

calendar[i]->name = strdup("Wednesday");

calendar[i]->date = 3;

calendar[i]->activity = strdup("Gym");

break;

case 3:

calendar[i]->name = strdup("Thursday");

calendar[i]->date = 4;

calendar[i]->activity = strdup("Study");

break;

case 4:

calendar[i]->name = strdup("Friday");

calendar[i]->date = 5;

calendar[i]->activity = strdup("Movie night");

break;

case 5:

calendar[i]->name = strdup("Saturday");

calendar[i]->date = 6;
calendar[i]->activity = strdup("Family outing");

break;

case 6:

calendar[i]->name = strdup("Sunday");

calendar[i]->date = 7;

calendar[i]->activity = strdup("Relax");

break;

// Display the calendar

printf("Calendar:\n");

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

printf("%s (Date: %d): %s\n", calendar[i]->name, calendar[i]->date,


calendar[i]->activity);

// Free allocated memory

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

free(calendar[i]->name);

free(calendar[i]->activity);

free(calendar[i]);

return 0;

Expected Output:

Calendar:

Monday (Date: 1): Work

Tuesday (Date: 2): Meeting


Wednesday (Date: 3): Gym

Thursday (Date: 4): Study

Friday (Date: 5): Movie night

Saturday (Date: 6): Family outing

Sunday (Date: 7): Relax

b)

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// Structure definition for each day

struct Day {

char *name; // dynamically allocated string for day name

int date; // date of the day

char *activity; // dynamically allocated string for activity description

};

// Function to create the calendar

void create(struct Day *calendar[]) {

int i;

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

calendar[i] = (struct Day *)malloc(sizeof(struct Day));

calendar[i]->name = NULL;

calendar[i]->activity = NULL;

// Function to read data from the keyboard

void read(struct Day *calendar[]) {


int i;

char temp[100];

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

printf("Enter the name of the day: ");

scanf("%s", temp);

calendar[i]->name = strdup(temp);

printf("Enter the date of the day: ");

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

printf("Enter the activity for the day: ");

scanf("%s", temp);

calendar[i]->activity = strdup(temp);

// Function to display the calendar

void display(struct Day *calendar[]) {

int i;

printf("Calendar:\n");

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

printf("%s (Date: %d): %s\n", calendar[i]->name, calendar[i]->date,


calendar[i]->activity);

int main() {

struct Day *calendar[7];

// Create the calendar

create(calendar);

// Read data from the keyboard


read(calendar);

// Display the calendar

display(calendar);

// Free allocated memory

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

free(calendar[i]->name);

free(calendar[i]->activity);

free(calendar[i]);

return 0;

Expected Output:

Enter the name of the day: Monday

Enter the date of the day: 1

Enter the activity for the day: Work

Enter the name of the day: Tuesday

Enter the date of the day: 2

Enter the activity for the day: Meeting

Enter the name of the day: Wednesday

Enter the date of the day: 3

Enter the activity for the day: Gym

Enter the name of the day: Thursday

Enter the date of the day: 4

Enter the activity for the day: Study

Enter the name of the day: Friday

Enter the date of the day: 5

Enter the activity for the day: Movie


Enter the name of the day: Saturday

Enter the date of the day: 6

Enter the activity for the day: Outing

Enter the name of the day: Sunday

Enter the date of the day: 7

Enter the activity for the day: Relax

Calendar:

Monday (Date: 1): Work

Tuesday (Date: 2): Meeting

Wednesday (Date: 3): Gym

Thursday (Date: 4): Study

Friday (Date: 5): Movie

Saturday (Date: 6): Outing

Sunday (Date: 7): Relax

Design, Develop and Implement 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.

Program:

#include<stdio.h>

#include<stdlib.h>
char str[100],pat[100],rep[100],ans[100];

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

void stringmatch()

i=m=j=k=c=0;

while(str[c]!='\0')

if(str[m]==pat[i])

i++;

m++;

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

flag=1;

for(k=0;rep[k]!='\0';k++)

ans[j]=rep[k];

j++;

i=0;

c=m;

else

{
ans[j]=str[c];

j++;

c++;

m=c;

i=0;

ans[j]='\0';

int main()

printf("enter a main string\n");

gets(str);

printf("enter the pattern;\n");

gets(pat);

printf("enter a replace string\n");

gets(rep);

stringmatch();

if(flag==1)

printf("the resultant string is %s\n",ans);

else

printf("pattern string not found!!\n");


}

return 0;

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.

Program:

#include<stdio.h>

#include<stdlib.h>

#define max_size 5

int stack[max_size],top=-1,i,status=0,count=0,item;

void push();
void pop();

void display();

void palin();

int main()

int choice;

while(choice)

printf("\n\n STACK OPERATIONS\n");

printf("\n1.PUSH\n2.POP\n3.PALINDROME\n4.DISPLAY\n5.EXIT\n");

printf("enter your choice:");

scanf("%d",&choice);

switch(choice)

case 1: push();

display();

break;

case 2: pop();

display();

break;

case 3:

palin();

break;

case 4: display();

break;

case 5: exit(0);
break;

default:printf("invalid choice\n");

break;

return 0;

void push()

int item;

if(top==(max_size-1))

printf("\n STACK OVERFLOW\n");

else

printf("enter the element to be inserted\n");

scanf("%d",&item);

top=top+1;

stack[top]=item;

count++;

void pop()
{

if(top==-1)

printf("\n STACK UNDERFLOW\n");

else

item=stack[top];

printf("\n\nthe popped element is:%d\t",stack[top]);

top=top-1;

count--;

void palin()

int temp;

for(i=0,temp=count-1;i<count,temp>=0;i++,temp--)

if(stack[i]==stack[temp])

status++;

if(status>=count)
printf("palindrome");

else

printf("Not A Palindrome");

void display()

int i;

if(top==-1)

printf("NO elements in the stack\n");

else

printf("the stack elements are\n");

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

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

}
Design, Develop and Implement a menu driven Program in C for the
following

Array operations

a. Creating an Array of N Integer Elements

b. Display of Array Elements with Suitable Headings

c. Inserting an Element (ELEM) at a given valid Position (POS)

d. Deleting an Element at a given valid Position(POS)

e. Exit.

Support the program with functions for each of the above operations.

Program:

#include<stdio.h>

#include<stdlib.h>

int n,*a;

int count=0;

void create()

int i;

a=(int*)malloc(n*sizeof(int));

if(a==NULL)

printf("array not created\n");

exit(0);

}
printf("\n array created successfully");

printf("\n enter the elements of array\n");

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

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

void display()

int i;

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

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

void insert(ele,pos)

int j=n-1;

while(j>=pos)

a[j+1]=a[j];

j--;

a[pos]=ele;
n++;

void delete(pos)

int j,item;

item=a[pos];

printf("the deleted item is %d\n",item);

for(j=pos;j<n-1;j++)

a[j]=a[j+1];

n--;

if(n==0)

printf("no element in the array\n");

int main()

int ch,ele,pos;

while(1)

printf("enter the choice\n1:create array\n2:display array\


n3:insertion \n4:delete item\n5:exit\n");

scanf("%d",&ch);

switch(ch)
{

case 1:

printf("\n enter the size of an array\n");

scanf("%d",&n);

create(n);

break;

case 2:

display();

break;

case 3:

printf("enter the element to be inserted\n");

scanf("%d",&ele);

label:printf("\n enter the position to be inserted\n");

scanf("%d",&pos);

if(pos>=0&&pos<n)

insert(ele,pos);

else

goto label;

break;

case 4:

label2:printf("\n enter the position\n");

scanf("%d",&pos);
if((pos>=0)&&(pos<n))

delete(pos);

else

goto label2;

break;

default:exit(0);

return 0;

PROGRAM-4

Design, Develop and Implement 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:

#include<stdio.h>

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

int f(char symbol)

switch(symbol)

case'+':

case'-': return 2;

case'*':

case'/': return 4;

case'^':

case'$': return 5;

case'(': return 0;

case'#': return -1;

default: return 8;

int g(char symbol)

switch(symbol)

case'+':

case'-': return 1;

case'*':

case'/': return 3;

case'^':
case'$': return 6;

case'(': return 9;

case')': return 0;

default: return 7;

void infix_postfix(char infix[],char postfix[])

int i,j,top;

char symbol,s[30];

top=-1;

j=0;

s[++top]='#';

for(i=0;i<strlen(infix);i++)

symbol=infix[i];

while(f(s[top])>g(symbol))

postfix[j]=s[top--];

j++;

if(f(s[top])!=g(symbol))

s[++top]=symbol;
}

else

top--;

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

postfix[j++]=s[top--];

postfix[j]='\0';

int main()

char infix[50],postfix[50];

printf("enter the valid infix exp\n");

scanf("%s",infix);

infix_postfix(infix,postfix);

printf("the postfix exp\n");

printf("%s\n",postfix);

return 0;
}

PROGRAM-6

Design, Develop and Implement 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 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:

#include<stdio.h>

#include<stdlib.h>

#define MAXSIZE 4

int ch,front=0,rear=-1,count=0;

char q[MAXSIZE],item;

void insert()

if(count==MAXSIZE)
{

printf("\n Queue is full");

else

rear=(rear+1)%MAXSIZE;

q[rear]=item;

count++;

void delete()

if(count==0)

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

else

item=q[front];

printf("\n Deleted item if %c", item);

front=(front+1)%MAXSIZE;

count--;

}
void display()

int i,j;

if(count==0)

printf("\n Queue is Empty");

else

j=front;

printf("\n Content of Queue is \n");

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

printf(" %c\t",q[j]);

j=(j+1)%MAXSIZE;

int main()

do

printf("\n 1. Insert \n2. Delete \n3.Display \n4.Exit \n");


printf("Enter the Choice :");

scanf("%d",&ch);

switch(ch)

case 1:

printf("Enter the items to be inserted :");

scanf(" %c",&item);

insert();

break;

case 2:

delete();

break;

case 3:

display();

break;

case 4:

exit(0);

break;

while(ch!=4);

return 0;

PROGRAM-7
Design, Develop and Implement a menu driven Program in C for the following
operations 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 / Deletion at End of SLL

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

e. Exit

Program:

#include<stdio.h>

#include<stdlib.h>

int MAX=4,count;

struct student

char usn[10];

char name[30];

char branch[5];

int sem;

char phno[10];

struct student *link;

};

typedef struct student *NODE;

int countnodes(NODE first)


{

NODE temp;

count=0;

temp=first;

while(temp!=NULL)

count++;

temp=temp->link;

return count;

NODE getnode(NODE first)

NODE temp;

temp=(NODE)malloc(sizeof(struct student));

printf("enter usn:\n");

scanf("%s",temp->usn);

printf("enter the name\n");

scanf("%s",temp->name);

printf("enter the branch\n");

scanf("%s",temp->branch);

printf("enter the sem\n");

scanf("%d",&temp->sem);

printf("enter the phno:\n");

scanf("%s",temp->phno);
temp->link=NULL;

first=temp;

return first;

void display(NODE first)

NODE temp;

if(first==NULL)

printf("no student dat\n");

else

temp=first;

printf("\r...STUDENT DATA..\n");

printf("\rUSN\t NAME\t BRANCH\t SEM\t PHNO\n");

while(temp!=NULL)

printf("\n%s\t %s\t %s\t %d\t %s\n",temp->usn,temp-


>name,temp->branch,temp->sem,temp->phno);

temp=temp->link;

printf("the number of nodes in list is %d\n",countnodes(first));

NODE create(NODE first)

{
NODE temp;

if(first==NULL)

temp=getnode(first);

first=temp;

else

temp=getnode(first);

temp->link=first;

first=temp;

return first;

NODE insert_front(NODE first)

if(countnodes(first)==MAX)

printf("list is full/overflow!!\n");

else

first=create(first);

return first;

NODE insert_rear(NODE first)

NODE temp,cur;
cur=first;

if(countnodes(first)==MAX)

printf("\nlist is full:!");

else

if(first==NULL)

temp=getnode(first);

first=temp;

else

temp=getnode(first);

while(cur->link!=NULL)

cur=cur->link;

cur->link=temp;

return first;

NODE insert_node(NODE first)

int ch;

while(1)

printf("\nmenu\n 1:insert@front\n 2:insert@rear\n 3:exit\n");


printf("enter your choice:");

scanf("%d",&ch);

switch(ch)

case 1 : first=insert_front(first);

break;

case 2 : first=insert_rear(first);

break;

case 3 : return first;

display(first);

return first;

NODE delete_front(NODE first)

NODE temp;

if(first==NULL)

printf("list is empty/underflow!!\n");

else

temp=first;

first=first->link;

free(temp);

printf("front node is deleted\n");


}

return first;

NODE delete_rear(NODE first)

NODE prev,cur;

cur=first;

prev=NULL;

if(first==NULL)

printf("list is empty/underflow!!\n");

return first;

if(first->link==NULL)

free(first);

printf("last node deleted\n");

return NULL;

else

while(cur->link!=NULL)

prev=cur;

cur=cur->link;

}
prev->link=NULL;

free(cur);

printf("last node is deleted\n");

return first;

NODE deletenode (NODE first)

int ch;

while(1)

printf("\n 1:delete from front\n 2:delete from rear\n 3:Exit \n");

printf("enter your choice:");

scanf("%d",&ch);

switch (ch)

case 1:first=delete_front(first);

break;

case 2:first=delete_rear(first);

break;

case 3:return first;

display(first);

return first;
}

NODE stack(NODE first)

int ch;

while(1)

printf("SLL used as stack");

printf("\n 1.PUSH\t 2.POP\t 3.EXIT\n");

printf("enter your choice:");

scanf("%d",&ch);

switch(ch)

case 1:first=insert_front(first);

break;

case 2:first=delete_front(first);

break;

case 3:return first;

display(first);

return first;

NODE queue(NODE first)

int ch;
while(1)

printf("\nSLL used as queue\n");

printf("\n 1.insert\t 2.delete\t 3.exit\n");

printf("enter the choice:");

scanf("%d",&ch);

switch(ch)

case 1:first=insert_rear(first);

break;

case 2:first=delete_front(first);

break;

case 3:return first;

display(first);

return first;

int main()

int ch,i,n;

NODE first;

first=NULL;

printf("...STUDENT DATABASE...");

while(1)
{

printf("\n 1.create\n 2.display\n 3.insert\n 4.delete\n 5.stack\n


6.queue\n 7.exit\n");

printf("enter your choice:");

scanf("%d",&ch);

switch(ch)

case 1:printf("how many students database's you want to


create\n");

scanf("%d",&n);

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

first=create(first);

break;

case 2:display(first);

break;

case 3:first=insert_node(first);

break;

case 4:first=deletenode(first);

break;

case 5:first=stack(first);

break;

case 6:first=queue(first);

break;

case 7:exit(0);
default:printf("invalid option\n");

return 0;

PROGRAM-8

Design, Develop and Implement 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

Program:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int MAX=4,count;
struct node

int ssn;

char name[20];

char dept[10];

char design[10];

int sal;

char phno[10];

struct node * llink;

struct node * rlink;

};

typedef struct node *NODE;

int countnodes(NODE first)

NODE temp;

count=0;

temp=first;

while(temp!=NULL)

count++;

temp=temp->rlink;

return count;

NODE getnode()

{
NODE temp;

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

temp->rlink=NULL;

temp->llink=NULL;

printf("enter SSN\n");

scanf("%d",&temp->ssn);

printf("enter name\n");

scanf("%s",temp->name);

printf("enter dept\n");

scanf("%s",temp->dept);

printf("enter designation\n");

scanf("%s",temp->design);

printf("enter salary\n");

scanf("%d",&temp->sal);

printf("enter phno\n");

scanf("%s",temp->phno);

return temp;

void display(NODE first)

NODE temp;

if(first==NULL)

printf("no employee data\n");

else
{

temp=first;

printf("employee data\n");

printf("SSN\t Name\t Department\t Designation\t Salary\t Phone


no\n");

while(temp!=NULL)

printf("\n%d\t%s\t%s\t%s\t%d\t%s",temp->ssn,temp-
>name,temp->dept,temp->design,temp->sal,temp->phno);

temp=temp->rlink;

printf("\n the no.of nodes is %d\n",countnodes(first));

NODE create(NODE first)

NODE temp,cur;

if(first==NULL)

temp=getnode();

first=temp;

return first;

else

temp=getnode();
cur=first;

while(cur->rlink!=NULL)

cur=cur->rlink;

cur->rlink=temp;

temp->llink=cur;

return first;

NODE insert_front(NODE first)

NODE temp;

if(countnodes(first)==MAX)

printf("list is full/overflow\n");

else

if(first==NULL)

temp=getnode();

first=temp;

else
{

temp=getnode();

temp->rlink=first;

first->llink=temp;

first=temp;

return first;

NODE insert_rear(NODE first)

if(countnodes(first)==MAX)

printf("list is full/overflow\n");

else

first=create(first);

return first;

NODE insertnode(NODE first)

int ch;

while(1)

{
printf("\n1.insert front\n 2.insert rear\n 3.exit\n");

printf("enter your choice\n");

scanf("%d",&ch);

switch(ch)

case 1:first=insert_front(first);

display(first);

break;

case 2:first=insert_rear(first);

display(first);

break;

case 3:return first;

return first;

NODE delete_front(NODE first)

NODE temp;

if(first==NULL)

printf("list is empty/underflow\n");

else if(first->rlink==NULL)

{
free(first);

printf("first node deleted\n");

first=NULL;

return first;

else

temp=first;

first=first->rlink;

first->llink=NULL;

free(temp);

printf("first node is deleted\n");

return first;

NODE delete_rear(NODE first)

NODE cur,prev;

if(first==NULL)

printf("list is empty/underflow\n");

else if(first->rlink==NULL)

free(first);

printf("last node deleted\n");


first=NULL;

return first;

else

cur=first;

prev=NULL;

while(cur->rlink!=NULL)

prev=cur;

cur=cur->rlink;

prev->rlink=NULL;

free(cur);

printf("last node deleted\n");

return first;

NODE deletenode(NODE first)

int ch;

while(1)

printf("1.delete front\n 2.delete rear\n 3.exit\n");

printf("enter your choice\n");


scanf("%d",&ch);

switch(ch)

case 1:first=delete_front(first);

display(first);

break;

case 2:first=delete_rear(first);

display(first);

break;

case 3:return first;

return first;

NODE dequeue(NODE first)

int ch;

while(1)

printf("1.insert rear\n 2.insert front\n 3.delete rear\n 4.delete


front\n 5:exit\n");

printf("enter your choice\n");

scanf("%d",&ch);

switch(ch)

case 1:first=insert_rear(first);
display(first);

break;

case 2:first=insert_front(first);

display(first);

break;

case 3:first=delete_rear(first);

display(first);

break;

case 4:first=delete_front(first);

display(first);

break;

case 5:return first;

return first;

int main()

int ch,i,n;

NODE first=NULL;

printf("\n\temployee database\n");

while(1)

printf("1.create\n 2.display\n 3.insert\n 4.delete\n 5.dequeue\n


6.exit\n");
printf("enter your choice\n");

scanf("%d",&ch);

switch(ch)

case 1:printf("How many employees do you want to create\


n");

scanf("%d",&n);

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

first=create(first);

break;

case 2:display(first);

break;

case 3:first=insertnode(first);

break;

case 4:first=deletenode(first);

break;

case 5:first=dequeue(first);

break;

case 6:exit(0);

default:printf("invalid choice\n");

return 0;

}
PROGRAM-9

Design, Develop and Implement 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) = 6x2y2z-


4yz5+3x3yz+2xy5z-

2xyz3

b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and

Store the result in POLYSUM(x,y,z)

Program:

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

struct node

int coeff,expx,expy,expz,flag;

struct node *link;

};

typedef struct node *NODE;

NODE getnode();

NODE read_poly(NODE head);

NODE insert_rear(int coeff,int expx,int expy,int expz,NODE head);

void display(NODE head);


NODE add_poly(NODE head1,NODE head2,NODE head3);

void evaluate(NODE head);

NODE getnode()

NODE x;

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

if(x==NULL)

printf("insufficient memory\n");

exit(0);

return x;

NODE read_poly(NODE head)

int expx,expy,expz,coeff,ch=1;

while(ch!=0)

printf("enter the coeff\n");

scanf("%d",&coeff);

printf("enter power ofx\n");

scanf("%d",&expx);

printf("enter the power y\n");

scanf("%d",&expy);
printf("enter the power z\n");

scanf("%d",&expz);

head=insert_rear(coeff,expx,expy,expz,head);

printf("press 1 to enter one more item or 0 to end\n");

scanf("%d",&ch);

return head;

NODE insert_rear(int coeff,int expx,int expy,int expz,NODE head)

NODE temp,cur;

temp=getnode();

temp->coeff=coeff;

temp->expx=expx;

temp->expy=expy;

temp->expz=expz;

temp->link=NULL;

cur=head->link;

while(cur->link!=head)

cur=cur->link;

cur->link=temp;

temp->link=head;

return head;
}

void display(NODE head)

NODE temp;

if(head->link==head)

printf("polynomial doesn't exixt\n");

else

temp=head->link;

while(temp!=head)

printf("%dx^%dy^%dz^%d",temp->coeff,temp-
>expx,temp->expy,temp->expz);

if(temp->link!=head)

printf("+");

temp=temp->link;

printf("\n");

}
NODE add_poly(NODE head1,NODE head2,NODE head3)

NODE temp1,temp2;

int x1,x2,y1,y2,z1,z2,coeff1,coeff2,coeff;

temp1=head1->link;

while(temp1!=head1)

x1=temp1->expx;

y1=temp1->expy;

z1=temp1->expz;

coeff1=temp1->coeff;

temp2=head2->link;

while(temp2!=head2)

x2=temp2->expx;

y2=temp2->expy;

z2=temp2->expz;

coeff2=temp2->coeff;

if(x1==x2&&y1==y2&&z1==z2)

break;

temp2=temp2->link;

if(temp2!=head2)

coeff=coeff1+coeff2;

temp2->flag=1;
if(coeff!=0)

head3=insert_rear(coeff,x1,y1,z1,head3);

else

head3=insert_rear(coeff1,x1,y1,z1,head3);

temp1=temp1->link;

temp2=head2->link;

while(temp2!=head2)

if(temp2->flag==0)

head3=insert_rear(temp2->coeff,temp2->expx,temp2-
>expy,temp2->expz,head3);

temp2=temp2->link;

return head3;

void evaluate(NODE head)

NODE temp;

int x,y,z;

double result=0;
printf("enter x,y,z values\n");

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

temp=head->link;

while(temp!=head)

result=result+(temp->coeff*pow(x,temp->expx)*pow(y,temp-
>expy)*pow(z,temp->expz));

temp=temp->link;

printf("Polynomial evaluated result is %f\n",result);

int main()

NODE head,head1,head2,head3;

int ch;

head=getnode();

head1=getnode();

head2=getnode();

head3=getnode();

head->link=head;

head1->link=head1;

head2->link=head2;

head3->link=head3;

while(1)
{

printf("\tMENU\n1.evaluate polynomial\n2.polynomail addition\


n3.exit\n");

printf("enter your choice\n");

scanf("%d",&ch);

switch(ch)

case 1:printf("enter polynomial to evaluate\n");

head=read_poly(head);

display(head);

evaluate(head);

break;

case 2:printf("enter the first polynomial\n");

head1=read_poly(head1);

printf("enter the second polynomial\n");

head2=read_poly(head2);

head3=add_poly(head1,head2,head3);

printf("first polynomial is\n");

display(head1);

printf("second polynomial is\n");

display(head2);

printf("the sum of 2 polynomials is\n");

display(head3);

break;

case 3:exit(0);

default:printf("invalid choice\n");

}
}

return 0;

PROGRAM-11

Design, Develop and Implement a Program in C for the following operations


on Graph(G) of Cities

a. Create a Graph of N cities using Adjacency Matrix.

b. Print all the nodes reachable from a given starting node in a digraph
using

DFS/BFS method

Program:

#include<stdio.h>

#include<stdlib.h>

int a[10][10],src,visited1[10],visited2[10],n;

void create();

void bfs(int);

void dfs(int);

void check();
int main()

int i;

int ch;

while(1)

printf("\n1.Create\t 2.BFS\t 3.DFS\t 4.Exit \n");

printf("Enter the choice");

scanf("%d",&ch);

switch(ch)

case 1: create();

break;

case 2: printf("BFS Traverasl\n");

printf("Enter the source Vertices: \n");

scanf("%d",&src);

bfs(src);

break;

case 3: printf("DFS Traversal\n");

printf("Enter the source Vertices: \n");

scanf("%d",&src);

dfs(src);

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

if(visited2[i]==0)

{
printf("Graph is not connected \n");

exit(0);

printf("Graph is connected \n");

break;

case 4: exit(0);

default : printf("Invalid Option !!\n");

return 0;

void create()

int i,j;

printf("Enter the Number of vertices:\n");

scanf("%d",&n);

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

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

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

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

}
}

void bfs(int src)

int q[10],f=0,r=-1,i,j;

visited1[src]=1;

q[++r]=src;

printf("Nodes reachable are \n");

while(f<=r)

i=q[f];

f++;

printf("%d\t",i);

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

if(a[i][j]==1 && visited1[j]==0)

visited1[j]=1;

q[++r]=j;

printf("\n Vertices are not reachable \n");

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

if(visited1[i]==0 )

{
printf("%d\t",i);

visited1[i]=0;

void dfs(int src)

int j;

visited2[src]=1;

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

if(a[src][j]==1 && visited2[j]==0)

printf("%d------->%d\n", src,j);

dfs(j);

PROGRAM-12

Given a File of N employee records with a set K of Keys(4-digit) which


uniquely determine the records in file F. Assume that file F is maintained in
memory by a Hash Table(HT) of m memory locations with L as the set of
memory addresses (2- digit) of locations in HT. Let the keys in K and
addresses in L are Integers. Design and develop a Program in C that uses
Hash function H: K L as H(K)=K mod m (remainder method), and
implement hashing technique to map a given key K to the address space L.
Resolve the collision (if any) using linear probing.

Program:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define HASH_SIZE 5

typedef struct employee

int id;

char name[20];

}EMPLOYEE;

void initial_hashtable(EMPLOYEE a[])

int i;

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

a[i].id=0;

}
int H(int k)

return k%HASH_SIZE;

void insert_hashtable(int id, char name[], EMPLOYEE a[])

int i, index, hvalue;

hvalue=H(id);

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

index=(hvalue+i)%HASH_SIZE;

if(a[index].id==0)

a[index].id=id;

strcpy(a[index].name, name);

break;

if(i==HASH_SIZE)

return 0;

return 0;

}
int search_hashtable(int key, EMPLOYEE a[])

int i, index, hvalue;

hvalue=H(key);

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

index=(hvalue+i)%HASH_SIZE;

if(key==a[index].id)return 1;

if(a[index].id==0) return 0;

void display_hashtable(EMPLOYEE a[], int n)

int i;

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

printf("a[%d]=%d %s\n", i, a[i].id, a[i].name);

int main()

EMPLOYEE a[10];

char name[20];

int key, id, i, ch, flag, n;

initial_hashtable(a);

while(1)

printf("\nMENU\n1.Insert\n2.Search\n3.Display\n4.Exit\n");
printf("Enter your choice: ");

scanf("%d", &ch);

switch(ch)

case 1: printf("Enter the no. of emps: ");

scanf("%d", &n);

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

printf("Enter emp id: ");

scanf("%d", &id);

printf("Enter name: ");

scanf("%s", name);

insert_hashtable(id, name, a);

break;

case 2: printf("Enter the emp id: ");

scanf("%d", &key);

flag=search_hashtable(key, a);

if(flag==0)

printf("Key not found\n");

else printf("Key found\n");

break;

case 3: printf("Contents of hash table\n");

display_hashtable(a, HASH_SIZE);

printf("\n");

break;
case 4: exit(0);

default: printf("Invalid choice\n");

return 0;

You might also like