Home c++ cyclic list for people presentation

cyclic list for people presentation

Author

Date

Category

just started to understand the lists. One moment does not understand the emphasis. In the book Sedgevik “Algorithms on C++” there is an example of creating a cyclic list for the presentation of people placed in the circle:

struct node {
  ITEM;
  Node * Next;
  Node (int x, node * t) {
    Item = x;
    NEXT = T;
  }
};
INT MAIN () {
  TypeDef Node * Link;
  Link T = New Node (1, 0);
  t- & gt; next = t;
  Link X = T;
  for (int i = 2; i & lt; 5; i ++)
    x = (x- & gt; next = new node (i, t));
  While (X! = X- & GT; Next) {
    for (int i = 1; i & lt; 5; i ++)
      x = x- & gt; next;
    x- & gt; next = x- & gt; next- & gt; next;
  }
  COUT & LT; & LT; X- & GT; Item & LT; & LT; Endl;
  DELETE T;
  Return 0;
}

What makes this block:

link t = new node (1, 0);
t- & gt; next = t;
Link X = T;

1 line: A pointer to an instance of the Node instance is generated, its fields are initialized and memory is allocated;

2 Row: Similar to (* T) .next = t; In the class Node there is a field next – pointer to node , which is assigned T – pointer to an instance of a class NODE ? Can you explain why this is done?

3 Row: Similar row link x = new node (1, 0); – first line?

p.s. And what does X- & GT; Next = X- & GT; Next- & gt; next; ? The next element in the list is assigned the value stored after the next list item:

(* x) .next = (* (* x) .next)). Next;


Answer 1, Authority 100%

How you yourself wrote, a ring list is created. And this means that the last element of the list indicates the first element of the list.
So to speak, everyone got into the circle and took up hands. 🙂

In these sentences

link t = new node (1, 0);
t- & gt; next = t;

The first element of the list

is created.

link t = new node (1, 0);

which indicates itself

t- & gt; next = t;

Then the cycle creates another three elements

link x = t;
for (int i = 2; i & lt; 5; i ++)
  x = (x- & gt; next = new node (i, t));

Each time you add a new item to its Next field indicates the first element

new node (i, t)

The pointer of this created element is assigned the field next of the previous element, and the variable x gets the value of the created pointer.

You can insert debugging

# include & lt; iostream & gt;
Struct Node.
{
  ITEM;
  Node * Next;
  Node (int x, node * t) {
    Item = X;
    NEXT = T;
  }
};
INT MAIN ()
{
  TypeDef Node * Link;
  Link T = New Node (1, 0);
  t- & gt; next = t;
  Link X = T;
  for (int i = 2; i & lt; 5; i ++)
  {
    x = (x- & gt; next = new node (i, t));
  }
  do.
  {
    x = x- & gt; next;
    STD :: COUT & LT; & LT; "Current:" & lt; & lt; (void *) x
         & lt; & lt; ", Value:" & lt; & lt; X- & GT; Item
         & lt; & lt; ", Next:" & lt; & lt; (void *) x- & gt; next
         & lt; & lt; STD :: ENDL;
  } while (x- & gt; next! = T);
  Return 0;
}

and get about the following conclusion on the console

current: 0x9CC570, Value: 1, Next: 0x9cc590
Current: 0x9CC590, Value: 2, Next: 0x9CBDA0
CURRENT: 0x9CBDA0, Value: 3, Next: 0x9CBDC0
CURRENT: 0x9CBDC0, Value: 4, Next: 0x9cc570

As you can see, the last element in the list indicates the first element in the list that has address 0x9CC570

In this test program, the DO-While cycle starts with the offer

x = x- & gt; next;

Because before that in another cycle, when creating the X elements, this is a pointer to the last created element and its NEXT field indicates the first element of the list. Therefore, this proposal at the first iteration of the cycle just goes to the first element of the list.

Experiment with the program from the book and see what happens!


Answer 2, Authority 100%

If you write this code normally, it will be

node * t = new node (1, nullptr); // Create a new item with Node type,
// in its constructor transmit 1 (Item field) and zero pointer (Next field)
t- & gt; next = t; // Make it so that the next field NEXT indicates itself

We cannot write node * t = new node (1, t); , because at the time of the constructor call the variable T is not filled yet.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions