Пытается заставить эту функцию push_back () работать - PullRequest
0 голосов
/ 22 апреля 2020

Я работаю над заданием для своего класса в колледже и пытаюсь выяснить, почему код для моей функции push_front () работает без сбоев, но мой push_back () всегда дает:

" Возникло исключение: нарушение прав чтения. st was nullptr. "

struct NodeDate
{
    int day, month, year; //Structure's data
    struct NodeDate* nextNode; //Pointer to point to next node

}*start; //Not sure why we do this, but all examples contain it

class LinkedListDate
{
public:
    //Prototype of member functions
    NodeDate* create_node(NodeDate);
    void push_front();
    void pop_front();
    void remove_front();
    void search();
    void display();
    LinkedListDate();
    void push_back();
    void remove_back();
    void pop_back();
};

LinkedListDate::LinkedListDate()
{
    start = NULL; //Our first created pointer will be set to NULL
}

NodeDate* LinkedListDate::create_node(struct NodeDate newDate) //Function that creates a new node and returns said node
{
    struct NodeDate* tempNode, *s;

    tempNode = new(struct NodeDate); //Reserving memory for our temp node



    if (tempNode == NULL) //If the node is empty
    {
        std::cout << "Memory not allocated " << std::endl;
        return 0;

    }
    else //Otherwise assign parameter node value to temporary node
    {
        tempNode->day = newDate.day;
        tempNode->month = newDate.month;
        tempNode->year = newDate.year;
        tempNode->nextNode = NULL; //Remember these are all values contained within the date struct

        return tempNode; //Return the node
    }
}

void LinkedListDate::push_front() //Function that will insert a node at the start of our linkedlist
{
    struct NodeDate* tempNode, *st, newNode;

    std::cout << "Enter the day: ";
    std::cin >> newNode.day;

    std::cout << "Enter the month: ";
    std::cin >> newNode.month;

    std::cout << "Enter the year: ";
    std::cin >> newNode.year;

    tempNode = create_node(newNode); //Creating a new node for date with the create_node function



    if (start == NULL) //Checks if the starting (Head) node is NULL then first node to insert
    {
        start = tempNode; //Start node points to our temporary node

        start->nextNode = NULL; //Assign null to start->next
    }
    else //Otherwise nodes are already available in the list
    {
        st = start; //Assign start to st

        start = tempNode; //Start points to temporary node

        start->nextNode = st; //Start next point to st
    }

    std::cout << "Element Inserted at beginning" << std::endl;

}

void LinkedListDate::push_back() //Function to insert a new node at the end of the linkedlist
{
    struct NodeDate* tempNode, *st, newNode;

    std::cout << "Enter the day: ";
    std::cin >> newNode.day;

    std::cout << "Enter the month: ";
    std::cin >> newNode.month;

    std::cout << "Enter the year: ";
    std::cin >> newNode.year;

    tempNode = create_node(newNode); //Creating a new node for date with the create_node function

    st = start;



    while (st->nextNode != NULL) //Move til we reach end of the list
    {
        st = st->nextNode; //Move to the next node
    }

    tempNode->nextNode = NULL; //Assign null to temporary node next



    st->nextNode = tempNode; //st next points to temporary node

    std::cout << "Element Inserted at last" << std::endl;

}

Снова push_front () работает, но push_back () не работает (программа запускается, но после ввода Для первого узла я получаю исключение.

Я много чего пробовал, но не могу понять, что именно я делаю неправильно.

1 Ответ

1 голос
/ 22 апреля 2020
struct NodeDate
{
//...
}*start;

- это то же самое, что и

struct NodeDate
{
//...
};
NodeDate *start;

push_back не будет выполнен, когда список пуст (т. Е. Начало равно нулю).

void LinkedListDate::push_back()
{
    //...
    tempNode = create_node(newNode);
    tempNode->nextNode = nullptr;
    if(start == nullptr)
    {
        start = tempNode;
    }
    else
    {
        st = start;
        while (st->nextNode != nullptr) //Move til we reach end of the list
        {
            st = st->nextNode; //Move to the next node
        }
        st->nextNode = tempNode;
    }
    std::cout << "Element Inserted at last" << std::endl;
}
...