Получить элемент из связанного списка в C ++ - PullRequest
0 голосов
/ 29 сентября 2018

Я пытаюсь вернуть данные в связанном списке, чтобы сохранить их в переменной или напрямую использовать их в другой функции, но я точно не знаю, как это реализовать.

    #include <iostream>
#include <string>
#include <fstream>
using namespace std;

template <class Object>
struct node{

    Object data;
    node *next;

};
template <class Object>
class list{

private:
    node<Object> *head, *tail;
public:
    list(){
        head=NULL;
        tail=NULL;
    }

    void display(){

        node<Object> *temp=new node<Object>;
        temp=head;
        while(temp!=NULL)
        {
            cout<<temp->data<<"              ";
            temp=temp->next;
        }
    }

    void createnode(Object value){
        node<Object> *temp=new node<Object>;
        temp->data=value;
        temp->next=NULL;
        if(head==NULL){

            head=temp;
            tail=temp;
            temp=NULL;

        }else{

            tail->next=temp;
            tail=temp;

        }
    }

    void insert_start(Object value){

        node<Object> *temp=new node<Object>;
        temp->data=value;
        temp->next=head;
        head=temp;

    }

    node<Object> GetNth(){
        node<Object> *current = head;

        while(current != NULL)
            if(current->next == NULL){
                return current->data;
            }


    }

    void delete_last(){

        node<Object> *current=new node<Object>;
        node<Object> *previous=new node<Object>;
        current=head;
        while(current->next!=NULL){

            previous=current;
            current=current->next;

        }
        tail=previous;
        previous->next=NULL;
        delete current;

    }

};

int main(){

        ifstream ifile;
        ifile.open( "input.txt" );

        char word[300];
        ifile >> word;

        char* token = strtok( word, "," );
        list<string> kids;
        list<string> tasks;

        while ( token != NULL ) {
            kids.createnode(token);
            token = strtok( NULL, "," );
        }

        ifile >> word;
        token = strtok(word, ",");

        while (token != NULL) {
            tasks.createnode(token);
            token = strtok(NULL, ",");
        }

        int days;
        cout << "Enter the number of days: ";
        cin >> days;

        tasks.display();

        cout << endl;

        int first = 0;
        string nextTask;

        while(first < days){
            cout << "Day " << first + 1 << "            ";
            kids.display();
            kids.insert_start(kids.GetNth());
            kids.delete_last();
            first++;
        }

        return 0;

}

Цель этой программы - назначать разные задания каждому ребенку в зависимости от дня.В настоящее время у меня проблемы с функцией getNth, было бы здорово, если бы кто-нибудь смог мне помочь.Я ценю любую помощь.

Ответы [ 2 ]

0 голосов
/ 29 сентября 2018

Возможно, у вас возникли проблемы, потому что GetNth () не возвращает значение, если список пуст.

node<Object> GetNth(){
    node<Object> *current = head;

    while(current != NULL)
        if(current->next == NULL){
            return current->data;
        }

    return NULL;
}
0 голосов
/ 29 сентября 2018

Вы можете использовать std :: list, он уже включен в C ++.Вы можете использовать at, чтобы получить доступ к n-му элементу.

...