C语言实现单链表
来源:互联网 发布:泰州网络教育学历报名 编辑:程序博客网 时间:2024/04/29 03:12
头文件
#ifndef __LIST_H__#define __LIST_H__#include<stdio.h>#include<stdlib.h>#include<assert.h>typedef int DataType;typedef struct Node//创建链表节点模型{ DataType data; struct Node *next;}Node,*pNode,*pList;void Initlist(pList *pplist);pNode BuyNode(DataType data);void PushBack(pList *pplist,DataType data);void PushFront(pList *pplist, DataType data);void Print(pList plist);void Destroylist(pList* pplist);void PopBack(pList* pplist,DataType data);void PopFront(pList* pplist,DataType data);DataTypeFind(pList* pplist, DataType data);void Remove(pList*pplist, DataType data);void RemoveAll(pList* pplist, DataType data);void Insert(pList* pplist,pNode pos,DataType data);void Erase(pList*pplist,pNode pos, DataType data);#endif//__LIST_H__
测试文件
#define _CRT_SECURE_NO_WARNINGS 1#include"list.h"void test(){ pList plist; Initlist(&plist); PushBack(&plist, 1); Print(plist); Destroylist(&plist); Print(plist); PopBack(&plist, 1); Print(plist); PopFront(&plist, 2); Print(plist); find(&plist, 1); /*Insert(&plist,3)*/ Remove(&plist, 1);}int main(){ test(); system("pause"); return 0;}
函数实现
#define _CRT_SECURE_NO_WARNINGS 1#include "list.h"void Initlist(pList *pplist){ assert(pplist != NULL); *pplist = NULL;}pNode BuyNode(DataType data){ pNode pnode = NULL; pnode = malloc(sizeof(Node)); if (pnode == NULL) { perror("malloc"); return NULL; } else { pnode->data = data; pnode->next = NULL; return pnode; }}void PushBack(pList *pplist, DataType data)//尾插{ assert(pplist != NULL); pNode pnewnode = NULL; pnewnode = BuyNode(data); if (*pplist == NULL)//没有节点的情况 { *pplist = pnewnode; } else { pNode cur = NULL; cur = *pplist; while (cur->next != NULL) { cur = cur->next; } cur->next = pnewnode; }}void Print(pList plist){ if (plist == NULL) { printf("链表为空\n"); } else { pList cur = NULL; cur = plist; printf("list:"); while (cur != NULL) { printf("%d->", cur->data); cur = cur->next; } printf("NULL"); }}void PushFront(pList *pplist, DataType data)//头插{ pNode pnewnode = NULL; assert(pplist!=NULL); pnewnode = BuyNode(data); if (*pplist == NULL)//没有节点的情况 { *pplist = pnewnode; pnewnode->next = NULL; } else { pNode cur = *pplist; *pplist = pnewnode; pnewnode->next =cur; }}void Destroylist(pList* pplist)//销毁链表{ pNode cur = *pplist; assert(pplist != NULL); while (cur) { pNode del = cur; cur = cur->next; free(del); } *pplist = NULL;}void PopBack(pList* pplist, DataType data)//尾删{ assert(pplist != NULL); pNode cur = *pplist; if (cur == NULL) { printf("链表为空\n"); } else if (cur->next == NULL) { free(cur); *pplist = NULL; } else { while (cur->next->next != NULL) { cur = cur->next; } free(cur->next->next); cur->next = NULL; }}void PopFront(pList* pplist, DataType data)//头删{ assert(pplist != NULL); pNode cur = NULL; cur = *pplist; if (cur == NULL) { printf("链表为空\n"); } else if (cur->next == NULL) { free(cur); *pplist = NULL; } else { pNode cur = NULL; cur = *pplist; *pplist = cur->next; free(cur); }}DataType Find(pList* pplist, DataType data)//查找指定元素{ assert(pplist != NULL); pNode cur = *pplist; if (cur == NULL) { printf("链表为空\n"); } else if (cur->next==NULL) { if (cur->data == data) { return cur->data; } else { return -1; } } else { while (cur) { if (cur->data == data) { return cur->data; } else { cur = cur->next; } } printf("查找元素不存在\n"); return -1; }}void Insert(pList* pplist, DataType data){}void Remove(pList*pplist, DataType data)//删除指定元素{ assert(pplist != NULL); pNode cur = *pplist; if (cur == NULL) { printf("链表为空\n"); } else if (cur->next == NULL) { if (cur->data == data) { free(cur); *pplist == NULL; } else { printf("指定删除的元素不存在\n"); } } else //>=2节点 { pNode prev = NULL; while (cur) { if (cur->data == data) { if (cur==*pplist) //如果删除的是有多个节点的链表的第一个节点 { *pplist = cur->next; free(cur); } else //删除的不是第一个节点,利用prev始终保存当前cur的前一个节点,在释放当前节点之前让当前节点的前一个节点的指针指向当前节点的后一个节点即cur->next,然后再释放当前节点cur, { prev->next = cur->next; free(cur); } } else { prev = cur; cur = cur->next; } } }}
0 0
- C语言实现单链表
- C语言实现单链表
- C语言单链表实现
- C语言实现单链表
- 单链表C语言实现
- c语言单链表实现
- 单链表 C语言实现
- C语言实现单链表
- c语言实现单链表
- C语言实现单链表
- C语言实现单链表
- C语言单链表实现
- C语言实现单链表
- C语言实现单链表
- c语言实现单链表
- C语言实现单链表
- c语言实现单链表
- 单链表C语言实现
- 在iOS开发中,给项目添加新的.framework
- MySQL日常总结
- 如何用Consul打造弹性可扩展的PaaS平台
- 你所不知的 CSS ::before 和 ::after 伪元素用法
- 理解JAVA程序逻辑及面向对象编程思想简单总结(15)
- C语言实现单链表
- 物流行业响起一声春雷,未来所有的购物或都能朝订夕达?
- Unity3D占用内存太大的解决方法
- Ajax--进阶(能认真的都答对了,你就真的会ajax了)
- Inverse和Cascade的比较
- 数组和链表的区别
- SSH-key 在Windows下如何生成公钥和私钥
- 视图
- SpringMVC过滤器