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语言——单链表创建练习题
- C语言创建单链表,输出单链表的内容。
- c语言实现单链表的操作:创建,删除,插入,反转, 排序等
- 单链表的创建和反转(C语言)
- C语言单链表的创建、插入、查找、删除、求长、排序、遍历
- C语言实现创建单链表及求长度
- c语言数据结构之顺序表单链表的创建及功能函数
- C语言之数据结构的顺序单链表创建,插入和删除操作
- C语言实现单链表的创建和添加结点
- C语言实现单链表的初始化、创建、遍历等操作
- C语言:单链表的创建,打印及销毁
- C语言单链表的创建和简单使用
- C语言实现单链表的创建、插入、删除
- c语言实现对传统单链表的创建、添加 遍历 删除 反转元素操作
- C语言单链表的创建和添加结点
- 【C语言】-单链表的创建和输出
- c语言之单链表的创建及排序
- C语言进阶-第18讲:单链表的遍历、创建、插入和删除结点
- C语言创建单链表
- 单链表的创建及输出(对刘霞同学的程序调试)