Question

5. Youre given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delec++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C++ program to create a function to delete the duplicates from the list so that the list does not contain any value more than once

#include <iostream>

using namespace std;

// struct to represent a node in the doubly linked list

struct DNode

{

       int data;

       DNode *next;

       DNode *prev;

};

// function declaration

DNode* insert(DNode *head, int data);

void display(DNode *head);

void destroy(DNode *head);

void deleteDuplicate(DNode *&head);

int main() {

       DNode *head = NULL; // pointer pointing to start of doubly linked list

       int data;

       // input the data(in ascending order) and insert into doubly linked list

       cout<<"Enter numbers ending with -999 for the linked list : "<<endl;

       cin>>data;

       while(data != -999)

       {

             head = insert(head,data);

             cin>>data;

       }

       // display the list

       cout<<"Input List : "<<endl;

       display(head);

       // delete the duplicates from the list so that the list contains value only once

       deleteDuplicate(head);

       // display the modified list

       cout<<"Output List : "<<endl;

       display(head);

       // delete the list

       destroy(head);

       return 0;

}

// function to insert a data into the doubly linked list

DNode* insert(DNode* head, int data)

{

       DNode *node = new DNode;

       node->data = data;

       node->next = NULL;

       node->prev = NULL;

       if(head == NULL)

       {

             head = node;

       }else

       {

             DNode *curr = head;

             while(curr->next != NULL)

                    curr = curr->next;

             curr->next = node;

             node->prev = node;

       }

       return head;

}

// function to display the list

void display(DNode *head)

{

       DNode *curr = head;

       while(curr != NULL)

       {

             cout<<curr->data<<" ";

             curr = curr->next;

       }

       cout<<endl;

}

// function to delete the nodes in the list

void destroy(DNode *head)

{

       while(head != NULL)

       {

             DNode *temp = head;

             head = head->next;

             delete(temp);

       }

}

// function to remove duplicate elements in the list

// Input : head is the pointer pointing to the head node of the sorted linked list, sorted in ascending order

void deleteDuplicate(DNode *&head)

{

       // check if the list is empty or contains only one element, then do nothing

       if(head != NULL && head->next != NULL)

       {

             DNode *currNode = head;

             // loop over the list starting from the first node

             while(currNode != NULL)

             {

                    DNode *nextNode = currNode->next;

                    // loop over the list starting from the list next to currNode

                    // loop that continues till we get the data of nextNode is same as currNode, deleting the nextNode

                    while((nextNode != NULL) && (nextNode->data == currNode->data))

                    {

                           DNode *temp = nextNode;

                           currNode->next = nextNode->next;

                           if(nextNode->next != NULL)

                                 nextNode->next->prev = currNode;

                           nextNode = nextNode->next;

                           delete(temp);

                    }

                    currNode = currNode->next;

             }

       }

}

//end of program

Output:

Enter numbers ending with -999 for the linked list : 3 7 11 11 11 24 27 30 42 42 47 55 55 78 89 -999 Input List : 3 7 11 11 1

Add a comment
Know the answer?
Add Answer to:
c++ 5. You're given the pointer to the head node of a sorted linked list, where...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are given a pointer head to the first node in a linked list. Each node...

    You are given a pointer head to the first node in a linked list. Each node points to the next node. We call the list a snail if the last node points to some node v in the list. We call the list a snake if the last node points to NULL. The list has n nodes. You are not allowed to change the list (permanently or temperately). You are allowed to use O(1) extra memory. The value of n...

  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

  • Given the node structure and the head pointer (headptr) of a linked list write a code...

    Given the node structure and the head pointer (headptr) of a linked list write a code to move the last element to the head of the list. struct node {    int value;    struct node *next; };

  • C++ You're given the pointer to the head nodes of two linked lists. Compare the data...

    C++ You're given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. The lists are equal only if they have the same number of nodes and corresponding nodes contain the same data. Either head pointer given may be null meaning that the corresponding list is empty. Input Format You have to complete the int CompareLists (Node headA, Node* head B) method which takes...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • Problem Statement This problem provides you with a linked list composed of node objects chained together...

    Problem Statement This problem provides you with a linked list composed of node objects chained together via node pointers. Each node has a next pointer which points to the next node in the chain. The last node is identified by having a NULL (or nullptr) next pointer. The linked lists for this problem store string data. Your task: identify the longest string stored in the linked list. If two strings are of the same length, return the string that occurred...

  • In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...

    In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class (50 pts): Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation. Part 2 Add Method (30 pts): Create an add method in your linked list...

  • 16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below,...

    16 and 18 C++ 11. Given the dynamic linked implementation of a linked list shown below, write expressions that do the following, assuming that currPtr is somewhere in the middle of the list: a. Access the component member of the first list element. b. Advance currPtr to point to the next element. c. Access the component member of the next element (the one that follows the current element). d. Access the component member of the element that follows the next...

  • Extend Linked List in C // Exercise 5 /* Parameter head points to the first node in a linked list, or is * NULL if the l...

    Extend Linked List in C // Exercise 5 /* Parameter head points to the first node in a linked list, or is * NULL if the list is empty. * * Parameter other points to the first node in a linked list, or is * NULL if the list is empty. * * Extend the linked list pointed to by head so that it contains * copies of the values stored in the linked list pointed to by other. *...

  • Write a C++ function to add a node to the beginning of a linked list. Your...

    Write a C++ function to add a node to the beginning of a linked list. Your function takes two arguments - the head of the linked list and the value num to be added. Note that the list may be empty! Your function should modify the head of the linked list to point to the new node, and set the new node to point to the rest of the list (if not empty). Example: Initial Array: 4->2->3, key = 5...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT