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...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that...

    Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that holds ints instead of strings. In your main, insert 25 to 75 random integers with range (0 to 100) into a linked list of your nodes. Use a random to determine how many to make. Write a function int sum(NodePointer current) that returns the sum of the integers in the list by looping through the linked list. Write a function int count(NodePointer current) that...

  • 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...

  • Design and implement your own linked list class to hold a sorted list of integers in...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • 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...

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