要求:链表的创建,要求创建一个链表并打印
//链表的创建,要求创建一个链表并打印
#include<iostream>
using namespace std;
typedef struct Lnode
{
int n;//数据域
Lnode* next;//指针域
};
void main()
{
int num;
printf("请输入链表长度:\n");
scanf_s("%d", &num);
//创建链表
Lnode * head,* p;
head = (Lnode*)malloc(sizeof(Lnode));
p = (Lnode*)malloc(sizeof(Lnode));
head->next = NULL;//头结点初始化
p = head;
for (int i = 0; i < num; i++)
{
Lnode* s;
s= (Lnode*)malloc(sizeof(Lnode));
scanf_s("%d",& s->n);
p->next = s;
s->next = NULL;
p = s;
}
printf("结果为:\n");
p = head->next;
while (p)
{
printf("%d\n", p->n);
p = p->next;//p结点后移
}
printf("\n");
getchar();
}