0% found this document useful (0 votes)
1 views5 pages

Expe 3 (I) DS

The document outlines two linked list applications: one for detecting and removing duplicates from a linked list, and another for representing and adding polynomials. It provides an algorithm and code for the duplicate removal function, demonstrating how to traverse the list and remove duplicates. Additionally, it describes the process for adding polynomial terms and provides steps for displaying the resulting polynomial.

Uploaded by

kanusha4304
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)
1 views5 pages

Expe 3 (I) DS

The document outlines two linked list applications: one for detecting and removing duplicates from a linked list, and another for representing and adding polynomials. It provides an algorithm and code for the duplicate removal function, demonstrating how to traverse the list and remove duplicates. Additionally, it describes the process for adding polynomial terms and provides steps for displaying the resulting polynomial.

Uploaded by

kanusha4304
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/ 5

EXPERIMENT:3

LINKED LIST APPLICATIONS:

3A. Create a program to detect and remove duplicates from a linked list.

ALGORITHM STEPS:

REMOVE DUPLICATES FUNCTIONS:

1.Input parameter: Accepts the head of the linked list(head).

2.Initialization: Initialize a pointer current to iterate through the list.

3.Outer loop: Loop through the list until current is NULL or its next is NULL.

4.Inner loop: For each node, iterate through the rest of the list with another pointer runner.

5.Duplicate detection: If a duplicate is found (i.e., runner → next →data == current→data),remove


the duplicate node.

6.Update pointers: Update runner→next to skip the duplicate node.

7.Move to next node: Move current to the next node in the list.

8.Return head: Return the head pointer (head).

CODE:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *next;

};

// Function to remove duplicates

struct Node *removeDuplicates(struct Node *head) {

struct Node *curr = head;

// Traverse the list

while (curr != NULL && curr->next != NULL) {

// Check if next value is the same as curr

if (curr->data == curr->next->data) {

struct Node *next_next = curr->next->next;

curr->next = next_next;
}

else

curr = curr->next;

return head;

void printList(struct Node *node) {

while (node != NULL) {

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

node = node->next;

printf("\n");

struct Node *createNode(int new_data){

struct Node *new_node =

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

new_node->data = new_data;

new_node->next = NULL;

return new_node;

int main() {

// Create a sorted linked list:

// 36->36->36->42->42->20

struct Node *head = createNode(36);

head->next = createNode(36);

head->next->next = createNode(36);

head->next->next->next = createNode(42);

head->next->next->next->next = createNode(42);

head->next->next->next->next->next = createNode(20);

printf("Linked list before duplicate removal:\n");

printList(head);
head = removeDuplicates(head);

printf("Linked list after duplicate removal:\n");

printList(head);

return 0;

OUTPUT:

Linked list before duplicate removal:

36 36 36 42 42 20

Linked list after duplicate removal:

36 42 20
3B. Implement a linked list to represent polynomials and perform addition.

Algorithm Steps:

addTerm Function:

* Input Parameters: Accepts the head of the polynomial (head), the coefficient of the new term
(coefficient), and the exponent of the new term (exponent).

* Allocate Memory: Allocates memory for a new term (temp) using malloc.

* If memory allocation fails, it prints an error message and exits the program.

* Initialize Term: Sets the coefficient and exponent of the new term to the given values and its next
pointer to NULL.

Insertion in Empty Polynomial:

* If the polynomial is empty (i.e., head is NULL), makes this term the head of the polynomial.

* Insertion in Non-empty Polynomial:

® Otherwise, traverses the polynomial to the end and appends the new term to the end.

* Return Head: Returns the head pointer of the polynomial (head).

addPolynomials Function:

* Input Parameters: Accepts the heads of two polynomials (poly1 and poly2) to add them

* Initialization: Initializes a pointer result to NULL to store the resultant polynomial.

Iterative Addition:

* lterates over both polynomials simultaneously until one of them ends

* Adds the corresponding terms based on their exponents:

* If the exponents are equal, adds their coefficients and creates a new term with the sum.

* If the exponent of one polynomial is greater than the other, adds that polynomial's term to

the result.

* Continues this process until both polynomials are exhausted.


Add Remaining Terms:

* Adds any remaining terms from both polynomials (if any) to the result.

* Return Result: Returns the head pointer of the resultant polynomial.

Display Polynomial Function:

* Input Parameter: Accepts the head of the polynomial (poly) to display.

* Traversal and Display: Traverses the polynomial from the head to the end and displays each term
along with its coefficient and exponent.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy