0

Menu option 6 I am trying to copy my LinkedList into a deque then display the deque list. But I cannot get it to work. Any ideas? I have figured out how to display one element but I want to display the whole list copied into the deque and then that list display the whole deque list.

Here is main

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include "LinkedList.h"
#include <deque>
using namespace std;

void MainMenu(int& menu);

int main(int argc, char** argv) {
int menu = 0;
    string name;
    LinkedList list;
    while (true)
    {
        
        MainMenu(menu);
        if (menu == 1)
        {
            cout << "Please enter the name you want to add to the front: " << endl;
            cin >> name;
            list.addFront(name);
        }
        else if (menu == 2)
        {
            cout << "Please enter the name you want to add to the back: " << endl;
            cin >> name;
            list.addBack(name);
        }
        else if (menu == 3)
        {
            cout << "Please enter the name you want to remove: " << endl;
            cin >> name;
            list.remove(name);
        }
        else if (menu == 4)
        {
            list.display();
        }
        else if (menu == 5)
        {
            cout << "\nThank you for using my program!" << endl;
            exit(0);
        }
        else if (menu == 6)
        {
            deque<LinkedList> d;
            d.push_front(list);
            cout << d[0]->current.name<< endl;
            
            
        }
    }

    
    return 0;
}

void MainMenu(int& menu)
{
    cout << '\n' << endl;
    cout << left << setw(30) << right << setw(20) << "Main Menu" << endl;
    cout << setw(36) << setfill('*') << "\n" << endl;
    cout << "1. Add node to front." << endl;
    cout << "2. Add node to back." << endl;
    cout << "3. Remove node." << endl;
    cout << "4. Display list." << endl;
    cout << "5. Exit" << endl;
    cout << "6. Deque" << endl;
    cout << setw(36) << "\n" << endl;
    cout << "Please enter a menu number to proceed: " << endl;
    cin >> menu;
    while (menu != 1 && menu != 2 && menu != 3 && menu != 4 && menu != 5 && menu != 6)
    {
        cout << "Menu options are 1 - 5" << endl;
        cout << "Please enter a menu number to proceed: " << endl;
        cin.clear();
        cin.ignore(100, '\n');
        cin >> menu;
    }
    cout << setfill(' ') << "\n" << endl;
}

class header file

#include <iostream>

using namespace std;

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

struct Node
{
    string name;
    Node * next;
};

class LinkedList {
public:
    Node * head = nullptr;
    Node * tail = nullptr;
    Node * current = nullptr;
    LinkedList();
    void addFront(string name);
    void addBack(string name);
    void remove(string name);
    void display();
    LinkedList(const LinkedList& orig);
    virtual ~LinkedList();
private:

};

#endif /* LINKEDLIST_H */

class source file

#include <deque>
#include "LinkedList.h"

LinkedList::LinkedList() {
}

void LinkedList::addFront(string name)
{
    Node * node = new Node();
    node->name = name;
    if (head != nullptr)
    {
        node->next = head;
        head = node;
    }
    else
    {
        head = node;
        tail = node;
    }
}
void LinkedList::addBack(string name)
{
    Node * node = new Node();
    node->name = name;
    node->next = nullptr;
    if(head == nullptr)
    {
        head = node;
        tail = node;
    }
    else
    {
        Node * temp = head;
        while(temp->next != nullptr)
        {
            temp = temp->next;
        }
        temp->next = node;
       
    }
}
void LinkedList::remove(string name)
{
    Node * temp = head;
        while (temp != nullptr)
        {
            if (temp->name == name)
            {
                cout << "Name found! Being removed." << endl;
                break;
            }
            temp = temp->next;
             if (temp == nullptr)
    {
        cout << "Name not found!" << endl;
        return;
    }
        }
    temp = head;
    if(head == nullptr)
    {
        cout << "The list is empty." << endl;
        return;
    }
    while(!(temp->name == name))
    {
        temp = temp->next;
    }
    if (temp == tail && temp == head)
    {
        head = nullptr;
        tail = nullptr;
        delete temp;
    }
    else if (temp == tail)
    {
        temp = head;
        while (temp->next->next != nullptr)
    {
        temp = temp->next;
    }
    delete temp->next;
    temp->next = nullptr;
   
        
    }
    else if(temp == head)
    {
        temp = head;
        head = temp->next;
 
        delete temp;
    }
    else
    {
        temp = head;
        while(!(temp->next->name == name))
        {
            temp = temp->next;
        }
        Node * next = temp->next->next;
        delete temp->next;
        temp->next = next;  
        
    }
}
void LinkedList::display()
{
    current = head;
     if (current == nullptr)
    {
        cout << "The list is empty!" << endl;
    }
    while(current != nullptr)
    {
        cout << current->name << endl;
        current = current->next;
    }
}
LinkedList::LinkedList(const LinkedList& orig) {
}

LinkedList::~LinkedList() {
}

eric1379
  • 43
  • 4
  • You create a deque containing one linked list as first element. Was this the intention? This also means that the expression `d[0]` has type `LinkedList&`, which is neither a pointer nor does `LinkedList` implement `operator->` trying to apply the `->` operator to this expression won't work... – fabian Jun 18 '21 at 17:02
  • `I cannot get it to work` isn't a useful problem description. Why do you think it doesn't work? – eerorika Jun 18 '21 at 17:08
  • 1
    `LinkedList::LinkedList(const LinkedList& orig) {}` -- Forget about using a `deque` until you implement a correct copy constructor, assignment operator, and destructor for the `LinkedList` class. A `std::deque` assumes that the type is safely copyable and assignable -- unfortunately `LinkedList` does not have those traits. Please read on the [rule of 3](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – PaulMcKenzie Jun 18 '21 at 17:08
  • 1
    Recommendation: Back up your code and remove everything not related to the problem you're fighting with. Remove the user input. Hard code in values that you know cause the problem and call functions directly rather than using the menu. If this changes the nature of the error, you learn something. If not, you have a test case that's much faster to run and easier to debug because of the reduced noise. Use [mre] for inspiration. – user4581301 Jun 18 '21 at 17:11

0 Answers0