Question

**HELP** Write a function that takes a linked list of items and deletes all repetitions from the ...

**HELP**

Write a function that takes a linked list of items and deletes all repetitions from the list. In your implementation, assume that items can be compared for equality using ==, that you used in the lab.

The prototype may look like:

void delete_repetitions(LinkedList& list);

** Node.h **

#ifndef Node_h

#define Node_h

class Node

{

public:

// TYPEDEF

typedef double value_type;

// CONSTRUCTOR

Node(const value_type& init_data = value_type( ), Node* init_link = NULL)

{ data_field = init_data; link_field = init_link; }

// Member functions to set the data and link fields:

void set_data(const value_type& new_data) { data_field = new_data; }

void set_link(Node* new_link) { link_field = new_link; }

// Constant member function to retrieve the current data:

value_type data() const { return data_field; }

// Two slightly different member functions to retrieve the current link:

const Node* link() const { return link_field; } // returns pointer to a const type

Node* link() { return link_field; }

private:

value_type data_field;

Node* link_field;

};

#endif /* Node_h */

**LinkedList.h**

#ifndef LinkedList_h

#define LinkedList_h

#include "Node.h"

#include <iostream>

#include <cstdlib>

class LinkedList

{

public:

typedef Node::value_type data_type;

typedef size_t size_type;

LinkedList();

size_type list_length() const {return node_count;};

bool isEmpty() const;

void head_insert(const Node::value_type& value);

void list_insert(Node* prev_ptr, const Node::value_type& value);

Node* getHeadPtr() {return head_ptr;};

const Node* getHeadPtr() const {return getHeadPtr();};

Node* getTailPtr() {return tail_ptr;};

const Node* getTailPtr() const {return getTailPtr();};

Node* list_search(const Node::value_type& target);

const Node* list_search(const Node::value_type& target) const {return list_search(target);};

void clear_list();

void head_remove(Node* temp);

void list_remove(Node* prev_ptr);

~LinkedList();

LinkedList(const LinkedList & l);

void list_copy(const LinkedList& source_list, LinkedList& destination_list );

LinkedList( LinkedList & l);

bool operator == (LinkedList &a);

void operator=(LinkedList &a);

private:

Node *head_ptr;

Node *tail_ptr;

size_type node_count;

};

void display(Node* head);

#endif /* LinkedList_h */

**LinkedList.cpp**

#include <iostream>

#include <cstdlib>

#include "Node.h"

#include "LinkedList.h"

using namespace std;

LinkedList::LinkedList()

{

node_count = 0;

head_ptr = NULL;

tail_ptr = NULL;

}

//Inserts a node at the beginning of the list

void LinkedList::head_insert(const Node::value_type& value)

{

Node* newNode = new Node(value, head_ptr);

head_ptr = newNode;

if(tail_ptr == NULL)

{

tail_ptr = head_ptr;

}

node_count++;

}

//Inserts new node into list

void LinkedList::list_insert(Node* prev_ptr, const Node::value_type& value)

{

if(prev_ptr != NULL)

{

Node* newNode = new Node(value, prev_ptr->link());

if(prev_ptr->link() == NULL)

{

tail_ptr = newNode;

}

prev_ptr->set_link(newNode);

}

else if(head_ptr == NULL)

{

head_insert(value);

}

node_count++;

}

//Searches for a certain number in a node

Node* LinkedList::list_search(const Node::value_type& target)

{

Node *temp = head_ptr;

while(temp != NULL)

{

if(target == temp->data())

{

return temp->link();

}

temp = temp->link();

}

return NULL;

}

bool LinkedList::isEmpty() const

{

return (node_count == 0);

}

//displays final list

void display(Node* head)

{

while(head != NULL)

{

cout << head->data() << " ";

head = head->link();

}

cout << endl;

}

void LinkedList::list_copy(const LinkedList& source_list, LinkedList& destination_list )

{

   const Node *head_source = source_list.getHeadPtr();

Node *tail_dest = destination_list.getTailPtr();

while(head_source != NULL)

{

destination_list.list_insert(tail_dest,head_source->data());

head_source = head_source->link();

tail_dest = tail_dest->link();

}

}

void LinkedList::head_remove(Node* temp)

{

Node* temp2 = temp;

temp2 = head_ptr;

head_ptr = head_ptr -> link();

   // delete temp2;

}

void LinkedList::list_remove(Node* prev_ptr)

{

Node* temp = prev_ptr -> link();

//temp - > link();

prev_ptr -> set_link(temp -> link());

delete temp;

node_count--;

}

void LinkedList::clear_list()

{

Node* temp;

while (head_ptr != NULL)

{

head_remove(temp);

}

}

LinkedList::~LinkedList()

{

clear_list();

}

bool LinkedList::operator==( LinkedList &dest)

{

if(this->list_length() != dest.list_length() )

{

return false;

}

Node* a = this -> getHeadPtr();

Node* b = dest.getHeadPtr();

while( a != NULL)

{

if(a -> data() != b -> data() )

{

return false;

}

a = a ->link();

b = b ->link();

}

return true;

}

void LinkedList::operator= (LinkedList &dest)

{

if(dest.list_length() == 0)

return;

  

Node* b = dest.getHeadPtr();

this-> head_insert(b -> data());

b = b -> link();

while(b != NULL)

{

Node* a = this -> getTailPtr();

this -> list_insert(a,b->data());

b = b->link();

  

}

  

}

LinkedList::LinkedList( LinkedList &l)

{

head_ptr = NULL;

tail_ptr = NULL;

node_count = 0;

  

//creates a deep copy

*this=l;

  

}

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

LinkedList is assumed to be unsorted and to remove repetitions a set is used. To use set you have to include unordered_set using the following in the LinkedList.cpp:

#include <unordered_set>

Below is the code of the delete_repetitions method:

void delete_repetitions(LinkedList &list)

{

// Hash to store seen values

unordered_set<int> seen;

/* Pick elements one by one */

Node *curr = list.getHeadPtr();

Node *prev = NULL;

while (curr != NULL)

{

// If current value is seen before

if (seen.find(curr->data()) != seen.end())

{

prev->set_link(curr->link());

delete (curr);

}

else

{

seen.insert(curr->data());

prev = curr;

}

curr = prev->link();

}

}

Add a comment
Know the answer?
Add Answer to:
**HELP** Write a function that takes a linked list of items and deletes all repetitions from the ...
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
  • there show an error in sample.cpp file that more than one instance of overloaded function find...

    there show an error in sample.cpp file that more than one instance of overloaded function find matches the argument list. can you please fix it. and rewrite the program and debug. thanks. I also wrote error below on which line in sample.cpp. it shows on find. #include #include #include "node1.cpp" using namespace main_savitch_5; // node1.h #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <string> namespace main_savitch_5 {    template<class item>    class node    {    public:        typedef item value_type;   ...

  • The goal of this task is to reinforce the implementation of container class concepts using linked...

    The goal of this task is to reinforce the implementation of container class concepts using linked lists. Specifically, the task is to create an implementation file using a linked list. You need to use the header files, set3.h and node1.h, and the test program, test_set3.cpp. Your documentation must include the efficiency of each function. Please make your program as efficient and reusable as possible. set3.h #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type;...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

  • I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node...

    I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node pointer p steps through the bag For each Item, define a new pointer q equal to p While the q is not the last Item in the bag If the next Item has data equal to the data in p, remove the next Item Otherwise move q to the next Item in the bag. I also need help creating a test program _____________________________________________________________________________________________________________________________________________________ #ifndef...

  • C++ programming language: In this program you will create a simplified bag that acts like a...

    C++ programming language: In this program you will create a simplified bag that acts like a stack meaning that the Last item inserted is the First Item that comes out. Your backend implementation must use a linked list. The code should pass the test (there's only 1) and there should be no memory leaks. Note that passing the test does not ensure full credit! The functions are listed in the suggested order of implementation but you may implement them in...

  • The purpose of this lab is to help reinforce container class concepts and linked list concepts...

    The purpose of this lab is to help reinforce container class concepts and linked list concepts in C++. Specifically, the lab to repeat the sequence lab from last week except to use a linked list. You need to use the author's files sequence3.h and sequence_exam3.cpp. Author - Michael Main, Book - Data Structures and other objects using c++, 4th edition // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is the header file for the...

  • The purpose of this program is to help reinforce container class concepts and linked list concepts...

    The purpose of this program is to help reinforce container class concepts and linked list concepts in C++. Specifically, the task is to implement the sequence class using a linked list. You need to use the author's file sequence3.h to create your implementation file named sequence3.cpp. Please make your code as efficient and reusable as possible. Please make sure code can pass any tests. sequence3.h // FILE: sequence3.h // CLASS PROVIDED: sequence (part of the namespace main_savitch_5) // This is...

  • Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k...

    Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k = 0, 1, 2 private void delete(x, k) // returns position of x in sorted list k if exist otherwise, -1. k = 0, 1, 2 private int search(x, k) import java.util.Scanner; class xxxxxp3{ private node[] head = new node[3]; private class node{ int num; node link; node(int x){ num=x; link = null; } } public void prtlst(int k){ System.out.printf("\nContents of List-%d:",k); for(node cur...

  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

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