一,创建链表结构带有头节点的链表
/*************************************************************************
> File Name: 111单链表.c
> Author: songli
> QQ: 2734030745
> Mail: 15850774503@163.com
> 优快云: http://my.youkuaiyun.com/Poisx
> github: https://github.com/chensongpoixs
> Created Time: Thu 24 Aug 2017 05:38:09 AM PDT
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
struct LNode
{
int data;
struct LNode * next;
};
struct LNode *create(int n)
{
int i;
struct LNode *head, *p1, *p2 = NULL; //VS编译器中指针要初始化 gcc 编译器已经优化过了
int a;
head = NULL; //头节点为NULL
printf("Input the integers:\n");
for (i = n; i > 0; --i)
{
//分配空间
p1 = (struct LNode*)malloc(sizeof(struct LNode));
//输入数据
scanf("%d", &a);
p1->data = a; //数据域赋值
//指定头结点
if (head == NULL)
{
head = p1; //把p1 付给头结点
p2 = p1; //第一
}
else
{
p2->next = p1;
p2 = p1;
}
}
p2->next = NULL;
return head;
}
int main(int argc, char *argv[])
{
int n;
struct LNode *q;
printf("Input the count of the nodes you want to creat:");
scanf("%d", &n);
q = create(n);
printf("The result is :\n");
while (q)
{
printf("%d ", q->data);
q = q->next;
}
return 0;
}