using the single linked list code written in the class or in

using the single linked list code written in the class or in the lab, write and test the following function: Remove Duplicates () function which takes a list sorted in increasing order and deletes any duplicate nodes from the list. Ideally, the list should only be traversed once. Merge () function that takes two lists, each of which is sorted in increasing order, and merges the two together into one list which is in increasing order.

Solution

// C code to remove duplicate and merge sorted linked list

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

// list node
struct node
{
int data;
struct node* next;
};

// merge two sorted
struct node* Merge(struct node* a, struct node* b)
{
struct node tempNode;
struct node* end = &tempNode;
  
tempNode.next = NULL;
while (1)
{
if (a == NULL)
{
  
end->next = b;
break;
}
else if (b == NULL)
{
end->next = a;
break;
}
if (a->data <= b->data)
{ struct node* new = a;
a = new->next;
new->next = end->next;
end->next = new;
}
else
{ struct node* new = b;
b = new->next;
new->next = end->next;
end->next = new;
}

end = end->next;
}
return(tempNode.next);
}

// remove duplicates from list
void RemoveDuplicates(struct node* head)
{
struct node* currNode = head;
struct node* next;

if (currNode == NULL)
return;

while (currNode->next != NULL)
{
if (currNode->data == currNode->next->data)
{
next = currNode->next->next;
free(currNode->next);
currNode->next = next;
}
else
{
currNode = currNode->next;
}
}
}
  

// push element in list
void push(struct node** head, int value)
{
struct node* new = (struct node*) malloc(sizeof(struct node));
new->data = value;
new->next = (*head);
(*head) = new;
}

// print list
void print(struct node *n)
{
while (n!=NULL)
{
printf(\"%d \", n->data);
n = n->next;
}
}

// main function
int main()
{
struct node* result = NULL;
struct node* a = NULL;
struct node* b = NULL;

push(&a, 3);
push(&a, 3);
push(&a, 2);
push(&a, 1);
printf(\"List a: \");
print(a);
printf(\"\ \");
push(&b, 10);
push(&b, 9);
push(&b, 9);
push(&b, 9);
push(&b, 9);
push(&b, 8);
printf(\"List b: \");
print(b);
printf(\"\ \");

// remove duplicates
RemoveDuplicates(a);
RemoveDuplicates(b);

printf(\"\ List a after removind duplicates: \");
print(a);
printf(\"\ List b after removind duplicates: \");
print(b);

// merge sorted list
result = Merge(a, b);

printf(\"\ \ Merged List: \");
print(result);
printf(\"\ \");
return 0;
}


/*
output:

List a: 1 2 3 3
List b: 8 9 9 9 9 10

List a after removind duplicates: 1 2 3
List b after removind duplicates: 8 9 10

Merged List: 1 2 3 8 9 10

*/

 using the single linked list code written in the class or in the lab, write and test the following function: Remove Duplicates () function which takes a list s
 using the single linked list code written in the class or in the lab, write and test the following function: Remove Duplicates () function which takes a list s
 using the single linked list code written in the class or in the lab, write and test the following function: Remove Duplicates () function which takes a list s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site