简单单链表的实现—C语言
来源:互联网 发布:微信html5 不用编程 编辑:程序博客网 时间:2024/04/29 05:03
本例简单实现了单链表,并附上调试(VS2013)过程,有助于深刻理解
单链表
链表是一种常见的重要的数据结构,它是动态的进行内存存储分配的一种结构。用数组存放数据时,必须事先定义固定的长度(即元素个数),但是事先难以确定有多少个元素时,则必须把数组定义的足够大,以保证成功。无疑,这会造成内存浪费,然而,链表则没有这种缺点,它可以根据需要,动态开辟内存单元。链表中的各个元素在内存中是可以不是连续存放的,但是要找到某一元素,必须知道它的地址,这就需要链表必须有一个头指针(head)。
本例包含
- 单链表的创建
- 单链表的查找
- 单链表的删除
- 单链表的打印
- 单链表的清除
实现代码
node.h
#ifndef _NODE_H_#define _NODE_H_typedef struct _node{ int value; struct _node * next;} Node;#endif
main.c
#include <stdio.h>#include <stdlib.h>#include "node.h"//typedef struct _node{// int value;// struct _node* next;//} Node;typedef struct _list{ Node* head;} List;void list_add(List* pList, int number);void list_find(List* pList, int number);void list_delete(List* pList, int number);void list_print(List* pList);void list_clear(List* pList);int main(){ List list; list.head = NULL; printf("Please input some number of int: "); int number = 0; do{ scanf("%d", &number); if (number != -1){ list_add(&list, number); } } while (number != -1); printf(" this is list your input: "); list_print(&list); printf("\n"); printf("Please input one number of int,it will add that list: "); int s = 0; scanf("%d", &s); list_add(&list, s); printf(" this is list your input: "); list_print(&list); printf("\n"); printf("Please input one number of int,we will find it at your list: "); int n = 0; scanf("%d", &n); list_find(&list, n); printf("Please input one number of int,we will delete it at your list: "); int m = 0; scanf("%d", &m); list_delete(&list, m); printf(" this is list your input: "); list_print(&list); printf("\n"); list_clear(&list); printf(" this is list is cleared! "); return 0;}void list_add(List* pList, int number){ // add to liked-list Node* p = (Node*)malloc(sizeof(Node)); p->value = number; p->next = NULL; Node* last = pList->head; if (last){ // find last while (last->next){ last = last->next; } //attach last->next = p; } else{ pList->head = p; }}void list_print(List* pList){ Node* p; for (p = pList->head; p; p = p->next){ printf("%d\t", p->value); }}void list_delete(List* pList, int number){ Node *p, *q; for (q = NULL, p = pList->head; p; q = p, p = p->next){ if (p->value == number){ if (q){ q->next = p->next; } else{ pList->head = p->next; } free(p); break; } }}void list_find(List* pList, int n){ int isFind = 0; Node* p; for (p = pList->head; p; p = p->next){ if (p->value == n){ printf("Find!\n"); isFind = 1; break; } } if (!isFind){ printf("Not Find!\n"); }}void list_clear(List* pList){ Node *p, *q; for (p = pList->head; p; p = q){ q = p->next; free(p); }}
调试
1、单链表创建完成后如下图,大家可以想想内存上的情况:
2、充分理解for(;;){}
- 第一次运行到main.c的127行
- 第二次运行到main.c的127行
0 0
- 简单单链表的实现—C语言
- 单链表的C语言简单实现
- c语言对简单单链表的实现
- C语言实现单链表的简单操作
- [数据结构]单链表C语言的简单实现
- 单链表的简单实现——C语言
- C语言实现简单单链表
- c语言实现简单单链表
- C语言实现单链表的简单操作【完整版】
- 【C语言】C语言实现简单的链表
- merker—hellman背包加密的C语言简单实现
- 一个简单的HashMap C语言实现
- c语言实现的简单二叉树
- 一个简单的HashMap C语言实现
- C语言,简单栈的实现 Stack
- BloomFilter的一个简单实现(C语言)
- C语言实现一个简单的计算器
- 泛型栈-C语言的简单实现
- PAT 1035. Password (20)
- java算法
- 高效率编辑器 VIM-操作篇
- js浏览器兼容问题总结及解决办法
- readlink使用
- 简单单链表的实现—C语言
- hadoop的第一个程序wordcount实现
- svn取消记住的用户名或Eclipse中修改svn提交代码的用户名
- Log4j日志输出详细
- Windows系统下Python与NumPy、matplotlib安装方法
- linux下mysql命令大全
- Sublime Text 2 快捷键
- Android应用的资源1
- HBase(0.96以上版本)过滤器Filter详解及实例代码