0% found this document useful (0 votes)
15 views12 pages

Practical No.15: Code

Uploaded by

pratikbhat2002
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)
15 views12 pages

Practical No.15: Code

Uploaded by

pratikbhat2002
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/ 12

Practical No.

15
CODE:
#include<stdio.h>
#include<stdlib.h>
struct PolyNode
{
int coeff;
int exp;
struct PolyNode* next;
};
struct PolyNode* createNode(int coeff, int exp)
{
struct PolyNode* newNode = (struct PolyNode*)malloc(sizeof(struct PolyNode));
newNode->coeff = coeff;
newNode->exp = exp;
newNode->next = NULL;
return newNode;
}
struct PolyNode* insertTerm(struct PolyNode* head, int coeff, int exp)
{
struct PolyNode* newNode = createNode(coeff, exp);
if (!head || exp > head->exp)
{
newNode->next = head;
return newNode;
}
struct PolyNode* current = head;
while (current->next && current->next->exp >= exp)
{
current = current->next;
}
if (current->exp == exp)
{
current->coeff += coeff;
free(newNode);
}
else
{
newNode->next = current->next;
current->next = newNode;
}
return head;
}
struct PolyNode* addPolynomials(struct PolyNode* p1, struct PolyNode* p2)
{
struct PolyNode* result = NULL;
while (p1 || p2)
{
if (!p1)
{
result = insertTerm(result, p2->coeff, p2->exp);
p2 = p2->next;
}
else if (!p2)
{
result = insertTerm(result, p1->coeff, p1->exp);
p1 = p1->next;
}
else if (p1->exp > p2->exp)
{
result = insertTerm(result, p1->coeff, p1->exp);
p1 = p1->next;
}
else if (p1->exp < p2->exp)
{
result = insertTerm(result, p2->coeff, p2->exp);
p2 = p2->next;
}
else
{
result = insertTerm(result, p1->coeff + p2->coeff, p1->exp);
p1 = p1->next;
p2 = p2->next;
}
}
return result;
}
void printPolynomial(struct PolyNode* poly)
{
if (!poly)
{
printf("0");
return;
}
struct PolyNode* current = poly;
while (current)
{
printf("%dx^%d", current->coeff, current->exp);
current = current->next;
if (current)
{
printf(" + ");
}
}
printf("\n");
}
int main()
{
struct PolyNode* poly1 = NULL;
struct PolyNode* poly2 = NULL;
poly1 = insertTerm(poly1, 4, 3);
poly1 = insertTerm(poly1, 3, 2);
poly1 = insertTerm(poly1, 2, 1);
poly1 = insertTerm(poly1, 1, 0);
poly2 = insertTerm(poly2, 5, 2);
poly2 = insertTerm(poly2, 3, 1);
poly2 = insertTerm(poly2, 2, 0);
printf("Polynomial 1: ");
printPolynomial(poly1);
printf("Polynomial 2: ");
printPolynomial(poly2);
struct PolyNode* result = addPolynomials(poly1, poly2);
printf("Resultant Polynomial: ");
printPolynomial(result);
return 0;
}
OUTPUT:
Practical No.16
CODE:
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
#define MAX 5
int top=-1,stack[MAX];
void push();
void pop();
void display();
void main()
{
int ch;
setbuf(stdout,NULL);
printf("\n*** Stack Menu ***");
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit");
while(1)
{
printf("\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("\nWrong Choice!!");
}
}
getch();
}
void push()
{
int val;
if(top==MAX-1)
printf("\nStack is full!!");
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}
void pop()
{
if(top==-1)
printf("\nStack is Empty!!");
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
printf("\nStack is Empty!!");
else
{
printf("\n**** Elements in Stack ****.\n");
for(i=top;i>=0;i--)
printf("%d\n",stack[i]);
}
}

OUTPUT:
Practical No.17
CODE:
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Stack
{
struct Node* top;
};
struct Stack* createStack()
{
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = NULL;
return stack;
}
int isEmpty(struct Stack* stack)
{
return stack->top == NULL;
}
void push(struct Stack* stack, int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = stack->top;
stack->top = newNode;
printf("Pushed %d onto stack.\n", data);
}
int pop(struct Stack* stack)
{
if (isEmpty(stack))
{
printf("Stack is empty. Cannot pop.\n");
return -1;
}
struct Node* temp = stack->top;
int poppedData = temp->data;
stack->top = stack->top->next;
free(temp);
printf("Popped %d from stack.\n", poppedData);
return poppedData;
}
int peek(struct Stack* stack)
{
if (isEmpty(stack))
{
printf("Stack is empty. Cannot peek.\n");
return -1;
}
return stack->top->data;
}
void freeStack(struct Stack* stack)
{
while (!isEmpty(stack))
{
pop(stack);
}
free(stack);
}
int main()
{
struct Stack* stack = createStack();
push(stack, 10);
push(stack, 20);
push(stack, 30);
printf("Top element is %d\n", peek(stack));
pop(stack);
pop(stack);
pop(stack);
pop(stack);
freeStack(stack);
return 0;
}
OUTPUT:
Practical No.18
CODE:
#include<stdio.h>
int multiply(int a,int b)
{
if(b==0)
return 0;
if(b<0)
return-multiply(a,-b);
return a+multiply(a,b-1);
}
void main()
{
int num1,num2,result;
printf("Enter two numbers to multiply:");
scanf("%d%d",&num1,&num2);
result=multiply(num1,num2);
printf("Result of %d*%d=%d\n",num1,num2,result);
getch();
}

OUTPUT:
Practical No.23
CODE:
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
struct CircularQueue
{
int items[MAX];
int front, rear;
};
struct CircularQueue* createQueue()
{
struct CircularQueue* queue = (struct CircularQueue*)malloc(sizeof(struct CircularQueue));
queue->front = -1;
queue->rear = -1;
return queue;
}
int isFull(struct CircularQueue* queue)
{
return (queue->front == (queue->rear + 1) % MAX);
}
int isEmpty(struct CircularQueue* queue)
{
return (queue->front == -1);
}
void enqueue(struct CircularQueue* queue, int value)
{
if (isFull(queue))
{
printf("Queue is full. Cannot enqueue %d\n", value);
return;
}
if (isEmpty(queue))
{
queue->front = 0;
}
queue->rear = (queue->rear + 1) % MAX;
queue->items[queue->rear] = value;
printf("Enqueued %d\n", value);
}
int dequeue(struct CircularQueue* queue)
{
if (isEmpty(queue))
{
printf("Queue is empty. Cannot dequeue.\n");
return -1;
}
int value = queue->items[queue->front];
if (queue->front == queue->rear)
{
queue->front = -1;
queue->rear = -1;
}
else
{
queue->front = (queue->front + 1) % MAX;
}
printf("Dequeued %d\n", value);
return value;
}
void display(struct CircularQueue* queue)
{
if (isEmpty(queue))
{
printf("Queue is empty.\n");
return;
}
printf("Queue elements: ");
int i = queue->front;
while (1)
{
printf("%d ", queue->items[i]);
if (i == queue->rear)
break;
i = (i + 1) % MAX;
}
printf("\n");
}
int main()
{
struct CircularQueue* queue = createQueue();
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
display(queue);
dequeue(queue);
display(queue);
enqueue(queue, 40);
enqueue(queue, 50);
display(queue);
enqueue(queue, 60);
dequeue(queue);
dequeue(queue);
display(queue);
enqueue(queue, 70);
enqueue(queue, 80);
display(queue);
free(queue);
return 0;
}
OUTPUT:

You might also like