添加到链接列表的前面

时间:2016-12-08 01:01:55

标签: c++

我能够在列表末尾成功添加名称,但我无法将其添加到前面。 我想弄清楚如何添加到节点的前面,谢谢。 我以为我理解为了添加到后面,你使用最后一个代码将cout的变量,所以我试图操纵它并使用head从头开始。

#include <iostream>
#include <string>

using namespace std;

struct node
{
    string name;
    string name1;
    node *next;

};

bool isEmpty(node *head);
char menu();
void insert(node *&head, node *&last, string name);
void insert_front(node *&head, node*&start, string name1);
void insert_back(node *&head, node *&last, string name);

void print(node *current);
bool isEmpty(node *head)
{
    if (head == NULL)
        return true;
    else
        return false;
}


char menu()
{
    char choice;
    cout << "Menu\n";
    cout << "1. Add a name to the front of the list." << endl;
    cout << "2. Add a name to the back of the list." << endl;
    cout << "3. Print the list." << endl;
    cout << "4. Exit." << endl;
    cin >> choice;
    return choice;
}

void insert(node *&head, node *&last, string name)
{
    node *temp = new node;
    temp->name = name;
    temp->next = NULL;
    head = temp;
    last = temp;
}

void insert_back(node *&head, node *&last, string name)
{
    if (isEmpty(head))
        insert(head, last, name);
    else
    {
        node *temp = new node;
        temp->name = name;
        temp->next = NULL;
        last->next = temp;
        last = temp;
    }
}

void insert_front(node *&head, node *& start, string name1)
{
    node *temp = new node;
    temp->name1 = name1;
    temp->next = head;
    head = temp;
}
void print(node *current)
{
    if (isEmpty(current))
        cout << "The list is emtpy." << endl;
    else
    {
        cout << "List of names: \n";
        while (current != NULL)
        {
            cout << current->name << endl;
            current = current->next;
        }
    }

}

int main()
{
    node *head = NULL;
    node *last = NULL;
    node *start = NULL;
    char choice;
    string name, name1;
    do
    {
        choice = menu();
        switch (choice)
        {
        case '1':
            cout << "Enter first name to the front of the list: " << endl;
            cin >> name1;
            insert_front(head, start, name1);
            break;
        case '2':
            cout << "Enter first name to the end of the list:" << endl;
            cin >> name;
            insert_back(head, last, name);
            break;
        case '3': print(head);
            break;
        case '4':
            return 0;
            break;
        }
    } while (choice != 4);

}

2 个答案:

答案 0 :(得分:0)

你有很多不相干的东西&#34;与代码混合插入列表的头部。你的insert_front显然不是错的;如果有问题,最有可能在其他地方。

我将代码缩小到更简单的范围,删除了大多数其他问题。我还使用ctor正确初始化节点,以获得此订单上的代码:

#include <iostream>

class linked_list {

    struct node {
        int value;
        node *next;

        node(int value, node *next) : value(value), next(next) {}

        friend std::ostream &operator<<(std::ostream &os, node const &n) {
            return os << n.value;
        }
    } *head = nullptr;

public:

    void add_front(int i) {
        head = new node(i, head);
    }

    friend std::ostream &operator<<(std::ostream &os, linked_list const &ll) {
        for (node *n = ll.head; n != nullptr; n = n->next)
            os << *n << ' ';
        return os;
    }

};

int main() {
    linked_list data;

    data.add_front(1);
    data.add_front(2);
    data.add_front(5);
    std::cout << data;
}

答案 1 :(得分:0)

我已将您的代码整理成最小的工作集,请在必要时进行学习和重写。请注意列表,注意当你想改变main中定义的head或tail值时,你必须传递变量的地址,而变量本身就是一个指针。

PRICE
相关问题