Upload files to "DS/C/Lab/Week9"

This commit is contained in:
Aadit Agrawal 2024-10-15 10:17:05 +05:30
parent 3da1635a88
commit 972d35034c
5 changed files with 644 additions and 0 deletions

View File

@ -0,0 +1,123 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int coefficient;
int exponent;
struct Node *next;
struct Node *prev;
} Node;
Node *createNode(int coefficient, int exponent) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->coefficient =coefficient;
newNode->exponent = exponent;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
void insertEnd(Node **head, int coefficient, int exponent) {
Node *newNode = createNode(coefficient, exponent);
if (*head == NULL) {
*head = newNode;
(*head)->next = *head;
(*head)->prev = *head;
} else {
Node *last = (*head)->prev;
newNode->next = *head;
newNode->prev = last;
last->next = newNode;
(*head)->prev = newNode;
}
}
Node *addPolynomials(Node *poly1, Node *poly2) {
if (poly1 == NULL) return poly2;
if (poly2 == NULL) return poly1;
Node *result = NULL;
Node *temp1 = poly1, *temp2 = poly2;
do {
if (temp1->exponent > temp2->exponent) {
insertEnd(&result, temp1->coefficient, temp1->exponent);
temp1 = temp1->next;
} else if (temp1->exponent < temp2->exponent) {
insertEnd(&result, temp2->coefficient, temp2->exponent);
temp2 = temp2->next;
} else {
int sum = temp1->coefficient + temp2->coefficient;
if (sum != 0) {
insertEnd(&result, sum, temp1->exponent);
}
temp1 = temp1->next;
temp2 = temp2->next;
}
} while (temp1 != poly1 || temp2 != poly2);
return result;
}
void displayPolynomial(Node *head) {
if (head == NULL) {
printf("Empty polynomial.\n");
return;
}
Node *temp = head;
do {
printf("(%dx^%d) ", temp->coefficient, temp->exponent);
temp = temp->next;
if (temp != head) {
printf("+ ");
}
} while (temp != head);
printf("\n");
}
void insert(Node** poly, int coef, int exp) {
Node* temp = (Node*) malloc(sizeof(Node));
temp->coefficient = coef;
temp->exponent = exp;
temp->next = NULL;
if (*poly == NULL) { *poly = temp; return; }
Node* current = *poly;
while (current->next != NULL) current = current->next;
current->next = temp;
}
int main() {
Node *poly1 = NULL, *poly2 = NULL;
insertEnd(&poly1, 5, 3); // 5x^3
insertEnd(&poly1, 4, 1); // 4x^1
insertEnd(&poly1, 3, 0); // 3x^0
insertEnd(&poly2, 3, 2); // 3x^2
insertEnd(&poly2, 2, 1); // 2x^1
insertEnd(&poly2, 1, 0); // 1x^0
printf("First polynomial: ");
displayPolynomial(poly1);
printf("Second polynomial: ");
displayPolynomial(poly2);
Node *sum = addPolynomials(poly1, poly2);
printf("Sum of the polynomials: ");
displayPolynomial(sum);
return 0;
}

View File

@ -0,0 +1,115 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct cnode *cnptr;
typedef struct cnode
{
int data;
cnptr next;
cnptr prev;
}cnode;
cnptr last=NULL;
cnptr createCnode(int val)
{
cnptr temp=(cnptr)malloc(sizeof(cnode));
temp->data=val;
temp->next=NULL;
temp->prev=NULL;
return temp;
}
void insertBegin(int val)
{
cnptr temp=createCnode(val);
if(last==NULL)
{
last=temp;
last->next=last;
last->prev=last;
}
else
{
temp->next=last->next;//head
last->next->prev=temp;
temp->prev=last;
last->next=temp;
}
}
void deleteend()
{
cnptr current;
if(last==NULL)
{
printf("List is empty");
}
else
{
cnptr previous=last->prev;
previous->next=last->next;
last->next->prev=previous;
free(last);
last=previous;
}
}
void revtraverse()
{
cnptr current=last;
printf("%d ",current->data);
current=current->prev;
while(current!=last)
{
printf("%d ",current->data);
current=current->prev;
}
printf("\n");
}
void traverse()
{
cnptr current=last;
current=current->next;
while(current!=last)
{
printf("%d ",current->data);
current=current->next;
}
printf("%d\n",current->data);
}
int main()
{
char contin='y';
int choice;
while(contin=='y')
{
printf("What do you want to do? \n 1. For inserting at beginning \n 2. For deleting at the end. \n 3. For traversing the doubly LL\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
int val;
printf("Enter the value\n");
scanf("%d",&val);
insertBegin(val);
break;
}
case 2:
{
deleteend();
printf("Element deleted\n");
break;
}
case 3:
{
printf("elements of the list are\n");
traverse();
break;
}
}
printf("continue?(y/n)\n");
fflush(stdin);
scanf("%c",&contin);
}
}

121
DS/C/Lab/Week9/MultiPoly.c Normal file
View File

@ -0,0 +1,121 @@
#include <stdio.h>
#include <stdlib.h>
// Structure to represent a term in a polynomial
typedef struct Node {
int coeff;
int power;
struct Node* next;
struct Node* prev;
} Node;
Node* createNode(int coeff, int power) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->coeff = coeff;
newNode->power = power;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
Node* insertTerm(Node* head, int coeff, int power) {
Node* temp = createNode(coeff, power);
if (head == NULL) {
head = temp;
head->next = head;
head->prev = head;
} else {
Node* last = head->prev;
temp->next = head;
temp->prev = last;
last->next = temp;
head->prev = temp;
}
return head;
}
void displayPolynomial(Node* head) {
if (head == NULL) {
printf("Polynomial is empty\n");
return;
}
Node* temp = head;
do {
printf("%dx^%d ", temp->coeff, temp->power);
temp = temp->next;
if (temp != head) {
printf("+ ");
}
} while (temp != head);
printf("\n");
}
Node* multiplyPolynomials(Node* poly1, Node* poly2) {
if (poly1 == NULL || poly2 == NULL) {
printf("One of the polynomials is empty\n");
return NULL;
}
Node* result = NULL;
Node* temp1 = poly1;
do {
Node* temp2 = poly2;
do {
int coeff = temp1->coeff * temp2->coeff;
int power = temp1->power + temp2->power;
result = insertTerm(result, coeff, power);
temp2 = temp2->next;
} while (temp2 != poly2);
temp1 = temp1->next;
} while (temp1 != poly1);
Node* current = result;
do {
Node* next = current->next;
while (next != result) {
if (current->power == next->power) {
current->coeff += next->coeff;
Node* temp = next;
next->prev->next = next->next;
next->next->prev = next->prev;
next = next->next;
free(temp);
} else {
next = next->next;
}
}
current = current->next;
} while (current != result);
return result;
}
int main() {
Node* poly1 = NULL;
Node* poly2 = NULL;
Node* result = NULL;
// Example: Polynomial 1: 5x^2 + 2x + 10, Polynomial 2: 2x^1 + 4
poly1 = insertTerm(poly1, 5, 2);
poly1 = insertTerm(poly1, 2, 1);
poly1 = insertTerm(poly1, 10, 0);
poly2 = insertTerm(poly2, 2, 1);
poly2 = insertTerm(poly2, 4, 0);
printf("Polynomial 1: ");
displayPolynomial(poly1);
printf("Polynomial 2: ");
displayPolynomial(poly2);
result = multiplyPolynomials(poly1, poly2);
printf("Result of Multiplication: ");
displayPolynomial(result);
return 0;
}

165
DS/C/Lab/Week9/PolyAdder.c Normal file
View File

@ -0,0 +1,165 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct pnode* pptr;
typedef struct pnode
{
int coeff;
int expo;
pptr next;
} pnode;
// create node in the linked list
pptr createNode(int coeff,int expo)
{
pptr temp = (pptr)malloc(sizeof(pnode));
if (temp == NULL)
{
printf("Heap is full");
free(temp);
exit(0);
}
temp->coeff = coeff;
temp->expo = expo;
temp->next = NULL;
return temp;
}
//insert element at the end of the linked list
void insertEnd(int coeff,int expo,pptr* head)
{
pptr temp = createNode(coeff,expo);
if(*head == NULL)
{
*head = temp;
}
else
{
pptr current = *head;
while(current->next)
{
current = current->next;
}
current->next = temp;
}
}
void attach(int coeff,int expo,pptr* rear)//we do *rear as we pdate the rear and ,=move it forword otherwise we didi not need to change it if only rear ke aage add karna hota
{
pptr temp;
temp = (pptr)malloc(sizeof(pnode));
temp->coeff = coeff;
temp->expo = expo;
temp->next = NULL;
(*rear)->next = temp;
*rear = temp;
}
pptr polyAdd(pptr A,pptr B)
{
pptr C,rear,temp;
rear = (pptr)malloc(sizeof(pnode));
C = rear;
while(A && B)
{
int diff = A->expo - B->expo;
//A->expo < B->expo
if(diff<0)
{
attach(B->coeff,B->expo,&rear);
B = B->next;
break;
}
//A->expo > B->expo
else if(diff>0)
{
attach(A->coeff,A->expo,&rear);
A = A->next;
break;
}
//A->expo = B->expo
else
{
int sum = A->coeff + B->coeff;
if(sum)
{
attach(sum,A->expo,&rear);
}
A = A->next;
B = B->next;
}
}
//attach remaining nodes for A and B
for(;A;A = A->next)
{
attach(A->coeff,A->expo,&rear);
}
for(;B;B=B->next)
{
attach(B->coeff,B->expo,&rear);
}
temp = C;
C = C->next;
free(temp);
return C;
}
int main()
{
pptr A, B;
A = B = NULL;
insertEnd(3, 3, &A);
insertEnd(4, 2, &A);
insertEnd(1, 0, &A);
pptr current = A;
printf("1st Polynomial is: ");
if (current) {
printf("%dx^%d", current->coeff, current->expo);
current = current->next;
}
while (current) {
printf(" + %dx^%d", current->coeff, current->expo);
current = current->next;
}
printf("\n");
insertEnd(6, 4, &B);
insertEnd(5, 2, &B);
insertEnd(4, 1, &B);
current = B;
printf("2nd Polynomial is: ");
if (current) {
printf("%dx^%d", current->coeff, current->expo);
current = current->next;
}
while (current) {
printf(" + %dx^%d", current->coeff, current->expo);
current = current->next;
}
printf("\n");
pptr C = polyAdd(A, B);
current = C;
printf("Added Polynomial is: ");
if (current) {
printf("%dx^%d", current->coeff, current->expo);
current = current->next;
}
while (current) {
printf(" + %dx^%d", current->coeff, current->expo);
current = current->next;
}
printf("\n");
return 0;
}

View File

@ -0,0 +1,120 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct cnode *cnptr;
typedef struct cnode
{
int data;
char name[100];
cnptr next;
cnptr prev;
} cnode;
cnptr last = NULL;
cnptr createCnode(int val)
{
cnptr temp = (cnptr)malloc(sizeof(cnode));
temp->data = val;
temp->next = NULL;
temp->prev = NULL;
return temp;
}
void insertBegin(int val, char nam[])
{
cnptr temp = createCnode(val);
strcpy(temp->name, nam);
if (last == NULL)
{
last = temp;
last->next = last;
last->prev = last;
}
else
{
temp->next = last->next; // head
last->next->prev = temp;
temp->prev = last;
last->next = temp;
}
}
void deleteend()
{
if (last == NULL)
{
printf("Queue is empty\n");
}
else
{
printf("Next person is %s for %d mins\n", last->name, last->data);
cnptr previous = last->prev;
previous->next = last->next;
last->next->prev = previous;
free(last);
last = previous;
if (last->next == last) // If the list becomes empty
last = NULL;
}
}
void traverse()
{
if (last == NULL) {
printf("Queue is empty\n");
return;
}
cnptr current = last->next;
do {
printf("%s (%d mins) ", current->name, current->data); // Print name AND time
current = current->next;
} while (current != last->next);
printf("\n");
}
int main()
{
char contin = 'y';
int choice;
while (contin == 'y')
{
printf("What do you want to do? \n 1. Add person to queue \n 2. Pass the chance to next person \n 3. View the Queue\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
int val;
printf("Enter the name:\n");
fflush(stdin);
char name[100];
gets(name);
printf("Enter the time in minutes: \n");
scanf("%d", &val);
insertBegin(val, name);
break;
}
case 2:
{
deleteend();
break;
}
case 3:
{
printf("People in the Queue are\n");
traverse();
break;
}
}
printf("continue? (y/n)\n");
fflush(stdin);
scanf(" %c", &contin);
}
return 0;
}