0% found this document useful (0 votes)
32 views42 pages

DS LAB MANUAL - Deepa

Uploaded by

mir2mir424
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)
32 views42 pages

DS LAB MANUAL - Deepa

Uploaded by

mir2mir424
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/ 42

PROGRAM 1

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define the structure for a day of the week
struct Day {
char *dayName;
int date;
char *activity;
};
// Function to create a new day entry
struct Day create() {
struct Day newDay;
newDay.dayName = (char *)malloc(20 * sizeof(char));
newDay.activity = (char *)malloc(100 * sizeof(char));

printf("Enter Day Name: ");


scanf("%s", newDay.dayName);

printf("Enter Date: ");


scanf("%d", &newDay.date);
getchar();

printf("Enter Activity: ");


scanf(" %99[^\n]s", newDay.activity);

return newDay;
}
// Function to read data from the keyboard and populate the calendar
void read(struct Day calendar[], int numDays) {
for (int i = 0; i < numDays; i++) {
printf("Enter details for Day %d:\n", i + 1);
calendar[i] = create();
}
}
// Function to display the calendar
void display(struct Day calendar[], int numDays) {
printf("\nWeek's Activity Details Report:\n");
for (int i = 0; i < numDays; i++) {
printf("Day: %s, Date: %d, Activity: %s\n", calendar[i].dayName, calendar[i].date,
calendar[i].activity);
}
}
int main() {
int numDays = 7;
struct Day calendar[7];

read(calendar, numDays);
display(calendar, numDays);

// Free dynamically allocated memory


for (int i = 0; i < numDays; i++) {
free(calendar[i].dayName);
free(calendar[i].activity);
}
return 0;
}
OUTPUT:
PROGRAM 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
Support the program with functions for each of the above operations. Don't use Built-in
functions.

#include <stdio.h>
#include <string.h>

#define MAX 100

int main() {
char str[MAX], pat[MAX], rep[MAX],result[200];
int i = 0, j = 0, k, found = 0;

printf("Enter the main string (STR): ");


gets(str); // Use gets() for simplicity

printf("Enter the pattern string (PAT): ");


gets(pat);

printf("Enter the replace string (REP): ");


gets(rep);

while (str[i] != '\0') {


k = 0;

while (pat[k] != '\0' && str[i + k] == pat[k]) {


k++;
}

if (pat[k] == '\0') {
found = 1;
for (k = 0; rep[k] != '\0'; k++) {
result[j++] = rep[k];
}
i += strlen(pat);
} else {
result[j++] = str[i++];
}
}

result[j] = '\0';

if (found) {
printf("After replacement: %s\n", result);
} else {
printf("Pattern not found.\n");
}
return 0;
}

OUTPUT:
PROGRAM 3
3. Develop 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>
#define MAX 5
int a[MAX];
int top=-1;
void push(int item)
{
if(top==(MAX-1))
{
printf("STACK OVERFLOW\n");
return;
}
top=top+1;
a[top]=item;
}
int pop()
{
if(top==-1)
{
printf("STACK UNDERFLOW\n");
return -1;
}
int item=a[top];
top=top-1;
return item;
}
void display()
{
int i;
if(top==-1)
{
printf("STACK IS EMPTY");
return;
}
printf("STACK ELEMENTS ARE:\n");
for(i=top;i>=0;i--)
{
printf("%d ",a[i]);
}
printf("\n");
}
void cpalindrome()
{
int flag=1;
int i;
for(i=0; i<=top/2; i++)
{
if(a[i]!=a[top-i])
{
flag=0;
break;
}
}
if(flag==1)
{
printf("\n It is a Palindrome\n");
}
else
{
printf("\n It is not a Palindrome\n");
}
}
void main()
{
int choice,item;
for(;;)
{
printf("\nMENU\n");
printf("press 1:Push\t2:pop\t3:display\t4:cpalindrome\t5:exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("enter the item to be inserted\n");
scanf("%d",&item);
push(item);
break;
case 2: item=pop();
if(item!=-1)
printf("item popped: %d\n",item);
break;
case 3: display();
break;
case 4: cpalindrome();
break;
case 5: exit(0);
}
}
}

OUTPUT:
PROGRAM 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.

#include<stdio.h>
#include<string.h>
#define MAX 10
char st[MAX];
int top=-1;
void push ( char c)
{
if(top==MAX-1)
printf("stack is full");
else
{
top = top + 1;
st[top ] = c;
}
}
void pop ( )
{
if(top == -1)
printf("stack is empty");
else
{
top = top - 1;
}
}
int prec(char c)
{
if (c == '^')
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}
void infixToPostfix(char *s)
{

int n=strlen(s);
for (int i = 0; i < n; i++)
{
char c = s[i];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
printf("%c \t",c);

else if (c == '(')
push('(');

else if (c == ')')
{
while (st[top] != '(')
{
printf("%c \t",st[top]);
pop();
}
pop();
}

else if(prec(c) >prec(st[top]))


push(c);

else {
while (top!=-1 && prec(c) <= prec(st[top]))
{
printf("%c \t",st[top]);
pop();
}
push(c);
}
}

while (top!=-1)
{
printf("%c \t",st[top]);
pop();
}
}

int main()
{
char exp[20];
printf("Enter the Infix expression:");
scanf("%s",exp);
infixToPostfix(exp);
return 0;
}
OUTPUT:
PROGRAM 5
5a. Develop a Program in C for the following Stack Applications
Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %,^

#include <stdio.h>
#include <math.h>
#include <ctype.h>
#define MAX 100

int top = -1;


double s[MAX];

void push(double elem) {


if (top == MAX - 1) {
printf("\nStack Overflow!!!");
} else {
top = top + 1;
s[top] = elem;
}
}

double pop() {
double del;
if (top == -1) {
printf("\nStack Underflow!!!");
} else {
del = s[top];
top = top - 1;
return del;
}
}

double evaluate(char op, double op1, double op2) {


switch (op) {
case '+': return op1 + op2;
case '-': return op1 - op2;
case '*': return op1 * op2;
case '/': return op1 / op2;
case '%': return fmod(op1, op2);
case '^': return pow(op1, op2);
default:
printf("\nInvalid Operator!!!");
return 0;
}
}

int main() {
char suffix[50];
int i;
double val, op1, op2, res;

printf("\nEnter the Suffix Expression: ");


scanf("%s", suffix);

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


if (isdigit(suffix[i])) {
push(suffix[i] - '0'); // Convert character to integer
} else if (isalpha(suffix[i])) {
printf("\nEnter the value of %c: ", suffix[i]);
scanf("%lf", &val);
push(val);
} else {
op2 = pop();
op1 = pop();
res = evaluate(suffix[i], op1, op2);
push(res);
}
}

res = pop(); // Pop the final result


printf("\nThe result is %lf", res);
return 0;
}

OUTPUT:
5 b. Solving Tower of Hanoi problem with n disks

#include <stdio.h>

void tower_hanoi(int n, char src, char dest, char temp) {


if (n == 1) {
printf("\nMove disk %d from peg %c to peg %c", n, src, dest);
return;
}
tower_hanoi(n - 1, src, temp, dest);
printf("\nMove disk %d from peg %c to peg %c", n, src, dest);
tower_hanoi(n - 1, temp, dest, src);
}

int main() {
int n;
printf("\nEnter the number of disks: ");
scanf("%d", &n);
tower_hanoi(n, 'A', 'B', 'C');
return 0;
}

Output:
PROGRAM 6

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

#include <stdio.h>
#define MAX 5

int f = -1, r = -1;

void insert(char cq[MAX]) {


char elem;
printf("\nEnter the element to insert into the queue: ");
scanf("\n%c", &elem);

if (f == (r + 1) % MAX) {
printf("\nQueue Overflow!!!");
return;
} else if (f == -1) {
f = 0;
}
r = (r + 1) % MAX;
cq[r] = elem;
}

void delete(char cq[MAX]) {


if (f == -1) {
printf("\nQueue Underflow!!!");
return;
}
printf("\nThe deleted element is: %c", cq[f]);
if (f == r) {
f = -1;
r = -1;
} else {
f = (f + 1) % MAX;
}
}

void display(char cq[MAX]) {


int i;
if (f == -1) {
printf("\nQueue Underflow!!!");
} else {
printf("\nThe elements of queue are: ");
for (i = f; i != r; i = (i + 1) % MAX)
printf("\t%c", cq[i]);
printf("\t%c", cq[i]);
}
}

int main() {
int ch;
char cq[MAX];

for (;;) {
printf("\n---------------------------------------------");
printf("\nCIRCULAR QUEUE OPERATIONS");
printf("\n1: Insert \n2: Delete \n3: Display \n4: Exit");
printf("\n---------------------------------------------");
printf("\nEnter your choice: ");
scanf("%d", &ch);

switch (ch) {
case 1:
insert(cq);
break;
case 2:
delete(cq);
break;
case 3:
display(cq);
break;
case 4:
return 0;
default:
printf("\nInvalid Choice!!!\n");
}
}
return 0;
}
OUTPUT
PROGRAM 7

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>

struct student {
char usn[20];
char name[20];
char program[20];
int sem;
long int phno;
struct student *link;
};

typedef struct student* STUDENT;

STUDENT start = NULL;

STUDENT create() {
STUDENT getnode;
getnode = (STUDENT)malloc(sizeof(struct student));
if (getnode == NULL) {
printf("\nMemory could not be allocated!!!");
}
printf("\nEnter the details of Student");
printf("\nEnter the usn: ");
scanf("%s", getnode->usn);
printf("\nEnter the name: ");
scanf("%s", getnode->name);
printf("\nEnter the branch: ");
scanf("%s", getnode->program);
printf("\nEnter the sem: ");
scanf("%d", &getnode->sem);
printf("\nEnter the phno: ");
scanf("%ld", &getnode->phno);
getnode->link = NULL;
return getnode;
}

void insert_front() {
STUDENT node;
node = create();
if (start == NULL) {
start = node;
} else {
node->link = start;
start = node;
}
}

void delete_front() {
STUDENT temp;
if (start == NULL) {
printf("\nList is Empty");
} else {
temp = start;
start = temp->link;
printf("\nThe deleted student usn is %s", temp->usn);
free(temp);
}
}

void create_list() {
int n, i;
printf("\nEnter the number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
insert_front();
}
}

void status() {
STUDENT temp;
int count = 0;
if (start == NULL) {
printf("\nList is Empty");
return;
}
temp = start;
printf("\nThe Student details are: ");
while (temp != NULL) {
printf("\n%s\n%s\n%s\n%d\n%ld\n", temp->usn, temp->name, temp->program, temp-
>sem, temp->phno);
temp = temp->link;
count++;
}
printf("\nThe number of nodes are: %d", count);
}

void insert_end() {
STUDENT node, temp;
node = create();
if (start == NULL) {
start = node;
} else {
temp = start;
while (temp->link != NULL) {
temp = temp->link;
}
temp->link = node;
}
}

void delete_end() {
STUDENT temp, prev;
temp = start;
if (temp == NULL) {
printf("\nList is Empty");
} else if (temp->link == NULL) {
printf("\nThe deleted student usn is %s", temp->usn);
free(temp);
start = NULL;
} else {
while (temp->link != NULL) {
prev = temp;
temp = temp->link;
}
printf("\nThe deleted student usn is %s", temp->usn);
free(temp);
prev->link = NULL;
}
}

void stack_demo() {
int ch;
for (;;) {
printf("\n---------------------------------------------");
printf("\nSTACK OPERATIONS");
printf("\n1: Insert End \n2: Delete End \n3: Status of Stack \n4: Exit");
printf("\n---------------------------------------------");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
insert_end();
break;
case 2:
delete_end();
break;
case 3:
status();
break;
case 4:
return;
default:
printf("\nInvalid Choice!!!");
}
}
}

int main() {
int ch;
for (;;) {
printf("\n---------------------------------------------");
printf("\nSINGLY LINKED LIST OPERATIONS");
printf("\n 1: Create List \n 2: Status of List \n 3: Insert End \n 4: Delete End \n 5: Insert
Front \n 6: Delete Front \n 7: Stack Demo \n 8: Exit");
printf("\n---------------------------------------------");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
create_list();
break;
case 2:
status();
break;
case 3:
insert_end();
break;
case 4:
delete_end();
break;
case 5:
insert_front();
break;
case 6:
delete_front();
break;
case 7:
stack_demo();
break;
case 8:
return 0;
default:
printf("\nInvalid Choice!!!");
}
}
return 0;
}
SAMPLE OUTPUT:
PROGRAM 8

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>

struct employee {
char ssn[20];
char name[20];
char dept[20];
char designation[20];
int salary;
long int phno;
struct employee *llink, *rlink;
};

typedef struct employee* EMPLOYEE;

EMPLOYEE start = NULL;

EMPLOYEE create() {
EMPLOYEE getnode;
getnode = (EMPLOYEE)malloc(sizeof(struct employee));
if (getnode == NULL) {
printf("\nMemory couldn't be allocated!!!");
return 0;
}
printf("\nEnter the details of Employee");
printf("\nEnter the ssn: ");
scanf("%s", getnode->ssn);
printf("\nEnter the name: ");
scanf("%s", getnode->name);
printf("\nEnter the department: ");
scanf("%s", getnode->dept);
printf("\nEnter the designation: ");
scanf("%s", getnode->designation);
printf("\nEnter the salary: ");
scanf("%d", &getnode->salary);
printf("\nEnter the phno: ");
scanf("%ld", &getnode->phno);
getnode->llink = NULL;
getnode->rlink = NULL;
return getnode;
}

void insert_end() {
EMPLOYEE node, temp;
node = create();
if (start == NULL) {
start = node;
} else {
temp = start;
while (temp->rlink != NULL) {
temp = temp->rlink;
}
temp->rlink = node;
node->llink = temp;
}
}

void delete_end() {
EMPLOYEE temp, prev;
temp = start;
if (temp == NULL) {
printf("\nList is Empty");
} else if (temp->rlink == NULL) {
printf("\nThe deleted employee ssn is %s", temp->ssn);
free(temp);
start = NULL;
} else {
while (temp->rlink != NULL) {
prev = temp;
temp = temp->rlink;
}
prev->rlink = NULL;
printf("\nThe deleted employee ssn is %s", temp->ssn);
free(temp);
}
}

void create_list() {
int n, i;
printf("\nEnter the number of employees: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
insert_end();
}
}

void status() {
EMPLOYEE temp;
int count = 0;
if (start == NULL) {
printf("\nList is Empty");
return;
}
temp = start;
printf("\nThe Employee details are: ");
while (temp != NULL) {
printf("\n%s\n%s\n%s\n%s\n%d\n%ld\n", temp->ssn, temp->name, temp->dept, temp-
>designation, temp->salary, temp->phno);
temp = temp->rlink;
count++;
}
printf("\nThe number of nodes are: %d", count);
}

void insert_front() {
EMPLOYEE node;
node = create();
if (start == NULL) {
start = node;
} else {
node->rlink = start;
start->llink = node;
start = node;
}
}

void delete_front() {
EMPLOYEE temp;
temp = start;
if (temp == NULL) {
printf("\nList is Empty");
} else if (temp->rlink == NULL) {
printf("\nThe deleted employee ssn is %s", temp->ssn);
free(temp);
start = NULL;
} else {
start = temp->rlink;
start->llink = NULL;
printf("\nThe deleted employee ssn is %s", temp->ssn);
free(temp);
}
}

void double_ended_queue() {
int ch;
for (;;) {
printf("\n---------------------------------------------");
printf("\nDOUBLE ENDED QUEUE OPERATIONS");
printf("\n1: Insert Rear \n2: Delete Rear \n3: Insert Front \n4: Delete Front \n5: Display
\n6: Exit");
printf("\n---------------------------------------------");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
insert_end();
break;
case 2:
delete_end();
break;
case 3:
insert_front();
break;
case 4:
delete_front();
break;
case 5:
status();
break;
case 6:
return;
default:
printf("\nInvalid Choice!!!");
}
}
}

int main() {
int ch;
for (;;) {
printf("\n---------------------------------------------");
printf("\nDOUBLY LINKED LIST OPERATIONS");
printf("\n1: Create List \n2: Status of List \n3: Insert End \n4: Delete End \n5: Insert
Front \n6: Delete Front \n7: Double Ended Queue Demo \n8: Exit");
printf("\n---------------------------------------------");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
create_list();
break;
case 2:
status();
break;
case 3:
insert_end();
break;
case 4:
delete_end();
break;
case 5:
insert_front();
break;
case 6:
delete_front();
break;
case 7:
double_ended_queue();
break;
case 8:
return 0;
default:
printf("\nInvalid Choice!!!");
}
}
return 0;
}

SAMPLE OUTPUT:
PROGRAM 9

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) = 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)
Support the program with appropriate functions for each of the above operations.

9A.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

struct polynomial {
int coeff, x, y, z;
struct polynomial *link;
};

typedef struct polynomial *POLYNOMIAL;

POLYNOMIAL create() {
POLYNOMIAL getnode;
getnode = (POLYNOMIAL)malloc(sizeof(struct polynomial));
if (getnode == NULL) {
printf("\nMemory couldn't be allocated!!!");
return 0;
}
return getnode;
}

POLYNOMIAL insert(POLYNOMIAL head, int c, int px, int py, int pz) {
POLYNOMIAL node, temp;
node = create();
node->coeff = c;
node->x = px;
node->y = py;
node->z = pz;
node->link = NULL;
temp = head->link;
while (temp->link != head) { /* Traverse till the end of the list. */
temp = temp->link;
}
temp->link = node; /* Attach the node to the end of the list. */
node->link = head; /* Assign the address of the head to node's link. */
return head;
}

POLYNOMIAL input_polynomial(POLYNOMIAL head) {


int i, c, px, py, pz;
printf("\nEnter 999 to end the polynomial!!!");
for (i = 1;; i++) {
printf("\nEnter the coefficient %d: ", i);
scanf("%d", &c);
if (c == 999) /* Breaks the loop when 999 is entered indicating end of input. */
break;

printf("\nEnter the power of x: ");


scanf("%d", &px);
printf("\nEnter the power of y: ");
scanf("%d", &py);
printf("\nEnter the power of z: ");
scanf("%d", &pz);
head = insert(head, c, px, py, pz);
}
return head;
}

void display(POLYNOMIAL head) {


POLYNOMIAL temp;
if (head->link == head) {
printf("\nPolynomial doesn't exist!!!");
} else {
temp = head->link;
while (temp != head) {
printf("%dx^%dy^%dz^%d + ", temp->coeff, temp->x, temp->y, temp->z);
temp = temp->link;
}
printf("999");
}
}

int evaluate_polynomial(POLYNOMIAL head) {


int vx, vy, vz, sum = 0;
POLYNOMIAL temp;
printf("\n\nEnter the value of x, y and z: ");
scanf("%d%d%d", &vx, &vy, &vz);
temp = head->link;
while (temp != head) {
sum = sum + (temp->coeff * pow(vx, temp->x) * pow(vy, temp->y) * pow(vz, temp-
>z));
temp = temp->link;
}
return sum;
}

int main() {
POLYNOMIAL head;
int res;
head = create();
head->link = head;
printf("\nEnter the polynomial to be evaluated: ");
head = input_polynomial(head);
printf("\nThe given polynomial is: ");
display(head);
res = evaluate_polynomial(head);
printf("\nThe result after evaluation is: %d", res);

return 0;
}

Sample output:
9B.

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

struct polynomial {
int coeff, x, y, z;
struct polynomial *link;
};

typedef struct polynomial *POLYNOMIAL;

/* Function definition to allocate memory. */


POLYNOMIAL create() {
POLYNOMIAL getnode;
getnode = (POLYNOMIAL)malloc(sizeof(struct polynomial));
if (getnode == NULL) {
printf("\nMemory couldn't be allocated!!!");
return 0;
}
return getnode;
}

/* Function definition to insert a polynomial in a singly circular linked list with header node. */
POLYNOMIAL insert_end(POLYNOMIAL head, int c, int px, int py, int pz) {
POLYNOMIAL node, temp;

node = create();
node->coeff = c;
node->x = px;
node->y = py;
node->z = pz;
node->link = NULL;
temp = head->link;
while (temp->link != head) { /* Traverse till the end of the list. */
temp = temp->link;
}
temp->link = node; /* Attach the node to the end of the list. */
node->link = head; /* Assign the address of the head to node's link. */
return head;
}

/* Function definition to read the polynomial. */


POLYNOMIAL input_polynomial(POLYNOMIAL head) {
int i, c, px, py, pz;
printf("\nEnter 999 to end the polynomial!!!");
for (i = 1;; i++) {
printf("\nEnter the coefficient %d: ", i);
scanf("%d", &c);
if (c == 999) /* Breaks the loop when 999 is entered indicating end of input. */
break;
printf("\nEnter the power of x: ");
scanf("%d", &px);
printf("\nEnter the power of y: ");
scanf("%d", &py);
printf("\nEnter the power of z: ");
scanf("%d", &pz);
head = insert_end(head, c, px, py, pz);
}
return head;
}

/* Function definition to display the list. */


void display(POLYNOMIAL head) {
POLYNOMIAL temp;
if (head->link == head) {
printf("\nPolynomial doesn't exist!!!");
return;
}
temp = head->link;
while (temp != head) {
printf("%dx^%dy^%dz^%d + ", temp->coeff, temp->x, temp->y, temp->z);
temp = temp->link;
}
printf("999");
}

/* Function definition to sum the two polynomials. */


POLYNOMIAL sum_polynomial(POLYNOMIAL head1, POLYNOMIAL head2, POLYNOMIAL
head3) {
POLYNOMIAL p1, p2;
int c, c1, c2, x1, y1, z1, x2, y2, z2, flag;
p1 = head1->link;
while (p1 != head1) {
c1 = p1->coeff;
x1 = p1->x;
y1 = p1->y;
z1 = p1->z;
p2 = head2->link;
flag = 0; /* No matching polynomial. */
while (p2 != head2) {
c2 = p2->coeff;
x2 = p2->x;
y2 = p2->y;
z2 = p2->z;
if ((x1 == x2) && (y1 == y2) && (z1 == z2)) { /* Check if the power of x, y, and z of both of
the polynomials are equal or not. */
head3 = insert_end(head3, c1 + c2, x1, y1, z1); /* Sum the coefficients and insert into the
final polynomial. */
p2->coeff = 0; /* Assign 0 to the coeff of polynomial to indicate that we have finished
evaluating it. */
flag = 1; /* Matching polynomial. */
break;
} else {
p2 = p2->link;
}
}
if (flag == 0) /* No matching polynomial. */
head3 = insert_end(head3, c1, x1, y1, z1); /* Insert polynomial1 into final polynomial. */
p1 = p1->link;
}
p2 = head2->link;
while (p2 != head2) {
if (p2->coeff != 0) /* Check for left out polynomials in polynomial2. */
head3 = insert_end(head3, p2->coeff, p2->x, p2->y, p2->z); /* Insert polynomial2 into final
polynomial. */
p2 = p2->link;
}
return head3;
}

int main() {
POLYNOMIAL head1, head2, head3;
/* Create a header node for polynomial1 whose link field points to the address of itself initially. */
head1 = create();
head1->link = head1;

/* Create a header node for polynomial2 whose link field points to the address of itself initially. */
head2 = create();
head2->link = head2;

/* Create a header node for sum of polynomial whose link field points to the address of itself
initially. */
head3 = create();
head3->link = head3;

printf("\nEnter the first polynomial: ");


head1 = input_polynomial(head1);
display(head1);

printf("\n\nEnter the second polynomial: ");


head2 = input_polynomial(head2);
display(head2);

head3 = sum_polynomial(head1, head2, head3);


printf("\n\nThe sum of two polynomials is: ");
display(head3);

return 0;
}
SAMPLE OUTPUT:
PROGRAM 10

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 *lchild;
struct BST *rchild;
};

typedef struct BST *NODE;

NODE create() {
NODE temp;
temp = (NODE)malloc(sizeof(struct BST));
printf("Enter The value: ");
scanf("%d", &temp->data);
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}

void insert(NODE root, NODE newnode) {


if (newnode->data < root->data) {
if (root->lchild == NULL)
root->lchild = newnode;
else
insert(root->lchild, newnode);
}
if (newnode->data > root->data) {
if (root->rchild == NULL)
root->rchild = newnode;
else
insert(root->rchild, newnode);
}
}

void search(NODE root) {


int key;
NODE cur;
if (root == NULL) {
printf("\nBST is empty.");
return;
}
printf("\nEnter Element to be searched: ");
scanf("%d", &key);
cur = root;
while (cur != NULL) {
if (cur->data == key) {
printf("\nKey element is present in BST");
return;
}
if (key < cur->data)
cur = cur->lchild;
else
cur = cur->rchild;
}
printf("\nKey element is not found in the BST");
}

void inorder(NODE root) {


if (root != NULL) {
inorder(root->lchild);
printf("%d ", root->data);
inorder(root->rchild);
}
}

void preorder(NODE root) {


if (root != NULL) {
printf("%d ", root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}

void postorder(NODE root) {


if (root != NULL) {
postorder(root->lchild);
postorder(root->rchild);
printf("%d ", root->data);
}
}

void main() {
int ch, i, n;
NODE root = NULL, newnode;
while (1) {
printf("\n\n~~~~BST MENU~~~~");
printf("\n1. Create a BST");
printf("\n2. Search");
printf("\n3. BST Traversals");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter the number of elements: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
newnode = create();
if (root == NULL)
root = newnode;
else
insert(root, newnode);
}
break;
case 2:
search(root);
break;
case 3:
if (root == NULL)
printf("\nTree Is Not Created");
else {
printf("\nThe Preorder display: ");
preorder(root);
printf("\nThe Inorder display: ");
inorder(root);
printf("\nThe Postorder display: ");
postorder(root);
}
break;
case 4:
exit(0);
}
}
}
SAMPLE OUTPUT:
PROGRAM 11

11. Develop 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.

#include <stdio.h>
#include <stdlib.h>

int a[50][50], n, bvisited[50], dvisited[50];


int q[20], front = -1, rear = -1;
int s[20], top = -1, count = 0;

void bfs(int v) {
int i, cur;
bvisited[v] = 1;
q[++rear] = v;

while (front != rear) {


cur = q[++front];
for (i = 1; i <= n; i++) {
if (a[cur][i] == 1 && bvisited[i] == 0) {
q[++rear] = i;
bvisited[i] = 1;
printf("%d ", i);
}
}
}
}

void dfs(int v) {
int i;
dvisited[v] = 1;
s[++top] = v;

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


if (a[v][i] == 1 && dvisited[i] == 0) {
printf("%d ", i);
dfs(i);
}
}
}

int main() {
int ch, start, i, j;
printf("\nEnter the number of vertices in graph: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix:\n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
}

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


bvisited[i] = 0;
dvisited[i] = 0;
}

printf("\nEnter the starting vertex: ");


scanf("%d", &start);

while (1) {
printf("\nMENU");
printf("\n==> 1. BFS");
printf("\n==> 2. DFS");
printf("\n==> 3. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);

switch (ch) {
case 1:
printf("\nNodes reachable from starting vertex %d are: ", start);
bfs(start);
break;

case 2:
printf("\nNodes reachable from starting vertex %d are: ", start);
dfs(start);
break;

case 3:
exit(0);

default:
printf("\nPlease enter a valid choice:");
}
}

return 0;
}
SAMPLE OUTPUT:
PROGRAM 12

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

#include <stdio.h>
#include <stdlib.h>

int key[20], n, m;
int *ht, index;
int count = 0;

void insert(int key) {


index = key % m;
while (ht[index] != -1) {
index = (index + 1) % m;
}
ht[index] = key;
count++;
}

void display() {
int i;
if (count == 0) {
printf("\nHash Table is empty");
return;
}

printf("\nHash Table contents are:\n");


for (i = 0; i < m; i++)
printf("\n T[%d] --> %d", i, ht[i]);
}

void main() {
int i;

printf("\nEnter the number of employee records (N): ");


scanf("%d", &n);

printf("\nEnter the two-digit memory locations (m) for hash table: ");
scanf("%d", &m);

ht = (int *)malloc(m * sizeof(int));


for (i = 0; i < m; i++)
ht[i] = -1;

printf("\nEnter the four-digit key values (K) for N Employee Records:\n");


for (i = 0; i < n; i++)
scanf("%d", &key[i]);

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


if (count == m) {
printf("\n~~~ Hash table is full. Cannot insert the record %d key ~~~", i + 1);
break;
}
insert(key[i]);
}

// Displaying keys inserted into the hash table


display();
}

Sample output:

You might also like