链表——1

创建三个空结点,通过next的指针,将三个结点的地址串联在一起,变成一个单链表

#include <iostream>

using namespace std;

struct Node {
    int data;
    Node* next;
};

void printList(Node* n)
{
    while (n != NULL)
    {
        cout << n->data << " ";
        n = n->next;
    }
}

int main()
{
    Node* head = new Node();
    Node* second = new Node();
    Node* third = new Node();

    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = NULL;

    printList(head);

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/strive-sun/p/12665262.html
今日推荐