您的位置:首页 > 编程语言 > C语言/C++

c语言创建单链表

2017-05-13 15:53 211 查看
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int num;
char sex;
struct node* next;
}Node;
Node* init_list(int);
int main(int argc, char const *argv[])
{
Node* head;
head = init_list(10);
head = head->next;
//遍历节点
while(head->next != NULL)
{
printf("%d %c\n",head->num,head->sex);
head = head->next;
}
printf("%d %c\n",head->num,head->sex);
return 0;
}
Node* init_list(int size)
{
int i = 0;
Node* head;
Node* p1;
Node* p2;
head = (Node *) malloc(sizeof(Node));//创建头节点
p1 = head;
for(i = 0; i < size;i++)
{
p2 = (Node *) malloc(sizeof(Node)); //创建节点
p2->num=i;
p2->sex='f';
p1->next = p2; //连接各个节点
p1 = p1->next;
}
p1->next = NULL;
return head;
}


输出:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 单链表