单链表创建

示例代码:

#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 << "\nSingleList display:" << endl;
    LNode *q=head;
    while((q=q->next) != NULL)
    {
        cout << q->data << " ";
    }

    return 0;
}

 

图示:

 

调试输出:

 

posted @ 2021-05-05 11:44  纸船  阅读(8)  评论(0编辑  收藏  举报