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

Linked List and Polynomial Operations

The document contains three sections of C code: the first reverses a singly linked list, the second merges two linked lists, and the third implements polynomial operations such as addition and subtraction. Each section includes function definitions for creating, printing, and manipulating linked lists. The main function in each section demonstrates how to use these functions with user input.

Uploaded by

Aritra Das
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)
6 views12 pages

Linked List and Polynomial Operations

The document contains three sections of C code: the first reverses a singly linked list, the second merges two linked lists, and the third implements polynomial operations such as addition and subtraction. Each section includes function definitions for creating, printing, and manipulating linked lists. The main function in each section demonstrates how to use these functions with user input.

Uploaded by

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

Q1.

WACP to reverse a singly linked list

Ans: The code:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

// Function to reverse the linked list

struct Node* reverse(struct Node* head) {

struct Node* prev = NULL;

struct Node* current = head;

struct Node* next = NULL;

while (current != NULL) {

next = current->next; // Store the next node

current->next = prev; // Reverse the current node's pointer

prev = current; // Move pointers one position ahead

current = next;

head = prev;

return head;

// Function to print the linked list

void printList(struct Node* node) {

while (node != NULL) {

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

node = node->next;

}
}

// Function to push a new node at the beginning

void push(struct Node** head_ref, int new_data) {

struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;

int main() {

struct Node* head = NULL;

int n, data;

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

scanf("%d", &n);

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

printf("Enter element %d: ", i + 1);

scanf("%d", &data);

push(&head, data);

printf("Original Linked List:\n");

printList(head);

head = reverse(head);

printf("\nReversed Linked List:\n");

printList(head);
return 0;

Output:

Q2. WACP to merge two linked lists

Ans: The code:

#include <stdio.h>

#include <stdlib.h>

// Node structure

struct Node {

int data;

struct Node* next;

};

// Function to insert a new node at the end of the list

void append(struct Node** head_ref, int new_data) {

struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

struct Node* last = *head_ref;

new_node->data = new_data;

new_node->next = NULL;

if (*head_ref == NULL) {

*head_ref = new_node;

return;

}
while (last->next != NULL)

last = last->next;

last->next = new_node;

return;

// Function to merge two linked lists

struct Node* mergeLists(struct Node* head1, struct Node* head2) {

if (head1 == NULL) return head2;

if (head2 == NULL) return head1;

struct Node* result = NULL;

if (head1->data <= head2->data) {

result = head1;

result->next = mergeLists(head1->next, head2);

} else {

result = head2;

result->next = mergeLists(head1, head2->next);

return result;

// Function to print the linked list

void printList(struct Node* node) {

while (node != NULL) {

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

node = node->next;
}

int main() {

struct Node* head1 = NULL;

struct Node* head2 = NULL;

int n1, n2, data;

printf("Enter the number of elements in the first list: ");

scanf("%d", &n1);

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

printf("Enter element %d: ", i + 1);

scanf("%d", &data);

append(&head1, data);

printf("Enter the number of elements in the second list: ");

scanf("%d", &n2);

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

printf("Enter element %d: ", i + 1);

scanf("%d", &data);

append(&head2, data);

printf("First Linked List:\n");

printList(head1);

printf("\nSecond Linked List:\n");

printList(head2);

struct Node* mergedHead = mergeLists(head1, head2);


printf("\nMerged Linked List:\n");

printList(mergedHead);

return 0;

Q3. Implement polynomial

Ans: The code:

#include <stdio.h>

#include <stdlib.h>

// Node structure

struct Node {

int coeff;

int pow;

struct Node* next;

};

// Function prototypes

void createPoly(struct Node** poly);

void displayPoly(struct Node* poly);

struct Node* addPoly(struct Node* poly1, struct Node* poly2);


struct Node* subPoly(struct Node* poly1, struct Node* poly2);

int main() {

struct Node *poly1 = NULL, *poly2 = NULL, *sum = NULL, *diff = NULL;

printf("Enter the first polynomial:\n");

createPoly(&poly1);

displayPoly(poly1);

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

createPoly(&poly2);

displayPoly(poly2);

sum = addPoly(poly1, poly2);

printf("Sum of polynomials:\n");

displayPoly(sum);

diff = subPoly(poly1, poly2);

printf("Difference of polynomials:\n");

displayPoly(diff);

return 0;

// Function to create a polynomial

void createPoly(struct Node** poly) {

int n, coeff, pow;

struct Node* temp = *poly;

printf("Enter number of terms: ");

scanf("%d", &n);

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


printf("Enter coefficient and power: ");

scanf("%d %d", &coeff, &pow);

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->coeff = coeff;

newNode->pow = pow;

newNode->next = NULL;

if (*poly == NULL) {

*poly = newNode;

temp = newNode;

} else {

temp->next = newNode;

temp = newNode;

// Function to display a polynomial

void displayPoly(struct Node* poly) {

struct Node* temp = poly;

while (temp != NULL) {

printf("%dx^%d", temp->coeff, temp->pow);

temp = temp->next;

if (temp != NULL) {

printf(" + ");

printf("\n");

// Function to add two polynomials

struct Node* addPoly(struct Node* poly1, struct Node* poly2) {


struct Node* sum = NULL;

struct Node* temp = NULL;

while (poly1 != NULL && poly2 != NULL) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

if (poly1->pow > poly2->pow) {

newNode->coeff = poly1->coeff;

newNode->pow = poly1->pow;

poly1 = poly1->next;

} else if (poly1->pow < poly2->pow) {

newNode->coeff = poly2->coeff;

newNode->pow = poly2->pow;

poly2 = poly2->next;

} else {

newNode->coeff = poly1->coeff + poly2->coeff;

newNode->pow = poly1->pow;

poly1 = poly1->next;

poly2 = poly2->next;

newNode->next = NULL;

if (sum == NULL) {

sum = newNode;

temp = newNode;

} else {

temp->next = newNode;

temp = newNode;

while (poly1 != NULL) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->coeff = poly1->coeff;

newNode->pow = poly1->pow;
newNode->next = NULL;

temp->next = newNode;

temp = newNode;

poly1 = poly1->next;

while (poly2 != NULL) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->coeff = poly2->coeff;

newNode->pow = poly2->pow;

newNode->next = NULL;

temp->next = newNode;

temp = newNode;

poly2 = poly2->next;

return sum;

// Function to subtract two polynomials

struct Node* subPoly(struct Node* poly1, struct Node* poly2) {

struct Node* diff = NULL;

struct Node* temp = NULL;

while (poly1 != NULL && poly2 != NULL) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

if (poly1->pow > poly2->pow) {

newNode->coeff = poly1->coeff;

newNode->pow = poly1->pow;

poly1 = poly1->next;

} else if (poly1->pow < poly2->pow) {

newNode->coeff = -poly2->coeff;

newNode->pow = poly2->pow;

poly2 = poly2->next;
} else {

newNode->coeff = poly1->coeff - poly2->coeff;

newNode->pow = poly1->pow;

poly1 = poly1->next;

poly2 = poly2->next;

newNode->next = NULL;

if (diff == NULL) {

diff = newNode;

temp = newNode;

} else {

temp->next = newNode;

temp = newNode;

while (poly1 != NULL) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->coeff = poly1->coeff;

newNode->pow = poly1->pow;

newNode->next = NULL;

temp->next = newNode;

temp = newNode;

poly1 = poly1->next;

while (poly2 != NULL) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->coeff = -poly2->coeff;

newNode->pow = poly2->pow;

newNode->next = NULL;

temp->next = newNode;

temp = newNode;
poly2 = poly2->next;

return diff;

The output:

You might also like