How do I add data to the append instead of the back in Linked List code?

Asked 2 years ago, Updated 2 years ago, 27 views

Below is part of the singly linked list code. If you type 'I 1', 'I 2', and 'I 3' after you run it, I want to make sure that the 3 entered later comes out first as the head, but on the other hand, it comes out in the order of 1, 2, and the getHead head comes out as 1. I thought about reversing the order when it was printed, but I don't think this method I thought would work because the first value of the head is still the head when I print out the head. Is there a lot of things that need to be fixed to change it as you want? I don't know because I'm a beginner, and I want to follow the school curriculum, but it's difficultcrying I would appreciate it more if you could explain it easily.

class InputData
{
public:
    int data;
    char query;
};

class Node
{
public:
    Node* next;
    int value;

public:
    Node(InputData* inputData) {
        this->value = inputData->data;
    }

    Node(int value) {
        next = NULL;
        this->value = value;
    }

    Node() {
        next = NULL;
        this->value = -1;
    }

    void print() {
        cout << value << endl;
    }
};


InputData* getInputData(string input) {
    InputData* inputData = new InputData();

    std::string delim = " ";
    std::string token;
    int pos = input.find(delim);
    if (pos != std::string::npos) {
        token = input.substr(0, pos);
        inputData->query = token[0];
        input.erase(0, pos + delim.length());
    } } else if (input.length() > 0) {
        inputData->query = input[0];
    }

    if (input.length() > 0) {
        inputData->data = atoi(input.c_str());
    }

    return inputData;
}

void insertData(Node** root, InputData* inputData) {
    if (*root == NULL) {
        *root = new Node(inputData);
        return;
    }
    Node* nextNode = *root;
    while (nextNode->next != NULL) {
        nextNode = nextNode->next;
    }
    nextNode->next = new Node(inputData);

Node* getHeadNode(Node* root) {
    if (root != NULL) {
        return root;
    }
    return NULL;
}

void printNode(Node* root) {
    Node* nextNode = root;
    while (true) {
        if (nextNode != NULL) {
            printf("%d ", nextNode->value);
            nextNode = nextNode->next;
        }
        else {
            break;
        }
    }
}
}

c++

2022-09-22 10:34

1 Answers

I think you can use it more easily if it's a double connection, but I can't help it if it's a single.

void AppendData (Node** root, Node** pos/*current location*/, InputData* inputData)
 {
    if (*root == NULL) {
        *root = new Node(inputData);
        *pos = *root;
        return;
    }

    if(*pos == NULL)
    {
        // You can have them put before or behind the root when they don't know where they are.
        return;
    }

    If ( *root!= *pos) // inserted in the middle, not root
    {
        Node* nextNode = *root;

        while (nextNode->next != *pos) {
            nextNode = nextNode->next;
        }

        nextNode->next = new Node(inputData);
        (nextNode->next)->next = *pos;
    }
    else // if inserted before root (root == pos)
    {
        *root = new Node(inputData);
        (*root)->next = *pos;
    }
 }

Inserting the front is relatively easy. If you connect to the next of the pre-created object, you insert it backwards, but if you connect to the next of the object you created later, the pre-created object is pushed back.

Therefore, the value entered first goes back, and the value entered later goes forward.


2022-09-22 10:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.