带头节点的单链表
2015-09-20 16:10
253 查看
#pragma once | |
#include <stdio.h> | |
#include <assert.h> | |
#include <malloc.h> | |
typedef int DataType; | |
typedef struct Node | |
{ | |
DataType data; | |
struct Node* next; | |
}Node; | |
typedef struct List | |
{ | |
Node* head; // 指向单链表的头 | |
size_t size; | |
}List; | |
Node* _CreateNode(DataType x) | |
{ | |
Node* tmp = (Node*)malloc(sizeof(Node)); | |
tmp->data = x; | |
tmp->next = NULL; | |
return tmp; | |
} | |
void InitList(List* pList) | |
{ | |
assert(pList); | |
pList->head = NULL; | |
pList->size = 0; | |
} | |
void PushBack(List* pList, DataType x) | |
{ | |
assert(pList); | |
if (pList->head == NULL) | |
{ | |
pList->head = _CreateNode(x); | |
} | |
else | |
{ | |
Node* end = pList->head; | |
while(end->next != NULL) | |
{ | |
end = end->next; | |
} | |
end->next = _CreateNode(x); | |
} | |
++pList->size; | |
} | |
void PrintList(List* pList) | |
{ | |
Node* begin = pList->head; | |
assert(pList); | |
while (begin) | |
{ | |
printf("%d->", begin->data); | |
begin = begin->next; | |
} | |
printf("NULL\n"); | |
} |
相关文章推荐
- 0长度数组
- hdu 5428 质因子
- iOS开发之UILabel
- composer常用命令
- 应聘C语言知识点复习 二
- axure学习总结
- MYSQL数据库学习----查询
- brctl命令
- php Hash Table(一) Hash Table的结构
- 端口号
- Holding Bin-Laden Captive!(母函数)
- TouchSlide.1.1.js 参数
- 《Java编程思想》学习记录仪一 -- java中参数传递方式(第二章一切都是对象)
- Activiti入门教程六(详解Activiti中的查询API)
- Java 随机生成中文姓名,手机号,邮编,住址
- Android Studio常用快捷键
- iOS学习 --多线程和GCD的理解
- Android之viewstub用法详解及实现延迟加载
- SQLHlper意识
- 我对git的认识