Related Lists in C ++

I am trying to teach myself linked lists with node structures and was hoping that someone could help me with this. I would take input from the command line, and that will make me a nested list, and I can output it.

Example:

Input: "1 2 3 4 5"
Output: "1 2 3 4 5"

There are two things that I encounter: 1) When I run the program, I continue to receive a warning: "typedef was ignored in this declaration [enabled by default] How can I get rid of this?

EDIT: I changed this to typedef struct Node* NodePtr;

2) My code does not work correctly. How can i fix this? I am trying to teach myself linked lists in C ++.

typedef struct Node;
typedef Node* NodePtr;
struct Node{
    int x;
    NodePtr next;
};

int main ()
{
    int n;
    NodePtr head, ptr = NULL;
    head = ptr;
    while (cin >> n){
        ptr = new Node;
        ptr->x = n;
        ptr->next = NULL;
        ptr = ptr->next;
    }

    NodePtr bling = head;
    while(bling != NULL){
        cout << bling->x << endl;
        bling = bling->next;
    }
    return 0;
}

Ideally, I want to make a linked list as follows.

1 -> 2 -> 3 -> NULL.
+5
source
4

-, typedef, , . C ++.

// declare NodePtr as a pointer to Node, currently an incomplete type
//  C and C++ both allow you to declare a pointer to damn-near anything
//  so long as there is an understanding of what it *will* be, in this
//  case, a structure called Node.
typedef struct Node *NodePtr;

// Now declare the structure type itself
struct Node
{
    int x;
    NodePtr next;
};

, . , : " !" . :

struct Node
{
    int x;
    struct Node *next; // omit the 'struct' for C++-only usage
};

, , , , NodePtr node, , . , . ( ), ( = P).

: , typedef ed ed, : . :

Node* a, b;     // declares one Node* (a), and one Node (b)

typedef struct Node *NodePtr; :

NodePtr a, b;   // declares two Node*; both (a) and (b)

C, , , , .


, , , , , . , " node". if (head){} else{}, - . , : :

NodePtr head = NULL;     // always the head of the list.
NodePtr* ptr = &head;    // will always point to the next pointer to assign.
int n;
while (cin >> n)
{
    *ptr = new Node;
    (*ptr)->x = n;
    ptr = &(*ptr)->next;
}

// note this always terminates the load with a NULL tail.
(*ptr)->next = NULL;

  • NULL
  • a Node - ( ), . , node. . : ptr.
  • while. node, , ptr ( , *ptr). head, head Node. next Node. , - , , .
  • Node next, NULL, . . ( , ), , , , "" NULL. , . : , ? : &head, , (a NULL head pointer), .

, , .

      head ===> NULL;
ptr --^

:

head ===> node(1)
          next
ptr ------^

head ===> node(1)
          next ===> node(2)
                    next
ptr ----------------^

head ===> node(1)
          next ===> node(2)
                    next ===> node(3)
                              next
ptr --------------------------^

, (*ptr = NULL;) :

head ===> node(1)
          next ===> node(2)
                    next ===> node(3)
                              next ===> NULL;
ptr --------------------------^

, head ( node). , ptr , , ( ) next Node.

, . , ( head ptr) Queue; - (ptr), (head), . -, std::queue<>, " ".


20 , , . (: , )

#include <iostream>
using namespace std;

// declare NodePtr as a pointer to Node, currently an incomplete type
//  C and C++ both allow you to declare a pointer to damn-near anything
//  so long as there is an understanding of what it *will* be, in this
//  case, a structure called Node.
typedef struct Node *NodePtr;

// Now declare the structure type itself
struct Node
{
    int x;
    NodePtr next;
};

int main()
{
    // load our list with 20 elements
    NodePtr head = NULL;
    NodePtr* ptr = &head;
    for (int n=1;n<=20;++n)
    {
        *ptr = new Node;
        (*ptr)->x = n;
        ptr = &(*ptr)->next;
    }

    // terminate the list.
    *ptr = NULL;

    // walk the list, printing each element
    NodePtr p = head;
    while (p)
    {
        cout << p->x << ' ';
        p = p->next;
    }
    cout << endl;

    // free the list
    while (head)
    {
        NodePtr victim = head;
        head = head->next;
        delete victim;
    }

    return 0;
}

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
+8

"head" NULL (head = ptr). . :

int n;
NodePtr head, ptr;
ptr = new Node;
head = ptr;
while (cin >> n){
    ptr->x = n;
    ptr->next = new Node;
    ptr = ptr->next;
}

ptr- > next 0 .

+2

. ->next NULL.

ptr = ptr->next ( NULL), ptr node. , ...

: ptr->next = head node . head = ptr, node. :.

(, LIFO, last-in-first-out):

while (cin >> n){
    ptr = new Node;
    ptr->x = n;
    ptr->next = head;
    head = ptr;
}

( FIFO):

while (cin >> n){
    if(!head) {
        head = new Node;
        ptr = head;
    }
    else
    {
        ptr->next = new Node;
        ptr = ptr->next;
    }
    ptr->next = NULL;
    ptr->x = n;
}
+1

-, typedef: typedef struct Node; . sintaxe:

struct st_Name{
    int field1;
    double field2;
};

Typedef . ( /) , , , :

typedef struct Node_st Node;

:

typedef struct node_st{
  int x;
  struct node_st *next;
}Node; 

, NodePtr struct node_st *Next : , ++ top- > down (kinda), NodePtr Node , , NodePtr , . , , , , .

.

...
while (cin >> n){
    ptr = new Node;
    ptr->x = n;
    ptr->next = NULL;  // You're making the next slot inexistent/NULL
    ptr = ptr->next;   // You're just setting ptr to NULL
}
...

, , , , , . if/else :

 while (cin >> n){
     if(head == NULL){
        ptr = new Node;
        ptr->x = n;
        ptr->next = NULL;
        head = ptr;
     }else{ 
        ptr->next = new Node;
        ptr = ptr->next;
        ptr->x = n;
        ptr->next = NULL; 
     }
}

So your insertion happens at the end, so your print cycle should work.

Hope this helps.

+1
source

All Articles