单链表创建

示例代码:

#include <iostream>

using namespace std;

typedef struct Lnode
{
    int data;
    struct Lnode * next;
}LNode;

int main()
{
    LNode *head, *p;
    head = (LNode*)malloc(sizeof(LNode));
    head->next = NULL;

    cout << "SingleList create:" << endl;
    int input;
    while(cin>>input)
    {
        p = (LNode*)malloc(sizeof(LNode));
        p->data = input;
        p->next = head->next;
        head->next = p;
    }

    cout << "
SingleList display:" << endl;
    LNode *q=head;
    while((q=q->next) != NULL)
    {
        cout << q->data << " ";
    }

    return 0;
}

图示:

调试输出:

原文地址:https://www.cnblogs.com/pmboat/p/14731338.html