D
D
Diamond2016-01-30 22:31:19
C++ / C#
Diamond, 2016-01-30 22:31:19

How to work with singly-directed lists?

There is a structure.

struct listNode {
  int key;
  int data;
  listNode *next;
};
listNode *head;
listNode *tail;

While it is empty and we need to add elements to it.
void addItem() {
  listNode *currItem = new listNode;
  cout << "Enter key: "; cin >> currItem->key; cout << endl;
  cout << "Enter data: "; cin >> currItem->data; cout << endl;
  currItem->next = NULL;

  if (head != NULL) {
    tail->next = currItem;
    tail = currItem;
  }
  else head = tail = currItem;
}

And then I start to misunderstand.
We have two auxiliary structures - a structure that stores the first element of the list (in order to know where to start) and a structure for storing the last element (I don’t quite understand why).
Let's say we've added the first element, and so on. this is the first element, the link to the next element is empty, we also assigned the newly created element to the auxiliary structure head (the beginning of the list), also with tail (the last element).
Next, add a new element. Because the beginning of the list already exists, touch it. We only write a pointer to the next element. In general, I can’t even explain what I don’t understand.
tail->next = currItem;
    tail = currItem;

What is the point of writing a pointer to the next element in the next variable, if then we completely replace the last element with a new one? And if so, then the first element of the list will be displayed as expected, but the subsequent ones will replace each other. I feel that the whole "trick" is in the pointers, but at the university they will talk to us about them later. And I want to understand how it works now.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RedHairOnMyHead, 2016-01-30
@ThePyzhov

Singly linked lists are well explained here.

A
abcd0x00, 2016-01-31
@abcd0x00

We have two auxiliary structures - a structure that stores the first element of the list (in order to know where to start) and a structure for storing the last element (I don’t quite understand why).

The head is needed to access the elements.
The tail is needed to add new elements.
Without a tail, if you, for example, have a million elements in the list, and you need to add three more, you will go through a million and add the first, then you will go through one million and add the second, then you will go through one million and add the third.
In order not to pass each time, this tail pointer is made.
The addItem() function is poorly designed. It would be more correct to pass the added value to it. If you enter inside, then you cannot, for example, take the elements of this list from a file or receive them over the network. So you have to write many identical lists with the same functionality. Therefore, it is necessary to enter from the outside, and then pass the entered value for addition.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question