c语言实现单链表&二级指针在单链表的应用
来源:互联网 发布:git ssh eclipse 端口 编辑:程序博客网 时间:2024/05/17 07:59
c语言实现单链表
用C语言实现链表的头插,头删,尾插,尾删,查找节点,任意节点插入,和任意节点删除等功能
此代码运用的是二级指针实现单链表,所以在写代码时要注意参数类型。
关于二级指针和一级指针:
一级指针变量指向的内容是普通变量的值,二级指针变量指向的内容是一级指针变量的地址。
当你想对一个地址做修改的时候,就要用到二级指针。
完整代码
#define __CRT_SECURE_NO_WARNRINGS 1#include "slist.h"//初始化链表void InitLinkList(ListNode** ppList){ assert(ppList); *ppList = NULL;}//打印链表void PrintList(ListNode* pList){ ListNode* cur = pList; while (cur){ printf("%d ", cur->data); cur= cur->next; } printf("\n"); return;}//申请一个新节点ListNode* Buynewnode(DataType x){ ListNode* newNode = NULL; newNode = (ListNode*)malloc(sizeof(ListNode));//开辟新节点,强制类型转换 if (newNode == NULL){ printf("out of memory \n");//溢出 } else{ newNode->data = x; newNode->next = NULL; } return newNode;}//尾插void PushBack(ListNode** ppList, DataType x){ assert(ppList); if (*ppList == NULL){ *ppList = Buynewnode(x); } else{ ListNode* ppcur = NULL; ppcur = *ppList; while (ppcur->next) { ppcur = ppcur->next; } ppcur->next = Buynewnode(x); }}//尾删void PopBack(ListNode** ppList){ assert(ppList); ListNode* cur = *ppList; if (cur == NULL){ return ; } if (cur->next==NULL){ free(cur); cur = NULL; } else{ while (cur->next->next){ cur = cur->next; } free(cur->next); cur->next = NULL; }}//头插void PushFront(ListNode** ppList, DataType x){ assert(ppList); ListNode* pFirst=NULL; if (*ppList==NULL){ *ppList = Buynewnode(x); } else{ pFirst = Buynewnode(x); pFirst->next = *ppList; *ppList = pFirst; }}//头删void PopFront(ListNode** ppList){ assert(ppList); if (*ppList == NULL){ return; } else{ ListNode* cur = *ppList; *ppList = (*ppList)->next; free(cur); cur = NULL; }}//查找表中元素xListNode* Find(ListNode* pList, DataType x){ assert(pList); ListNode* cur = pList; //if (cur == NULL){ // return ; //} while (cur){ if (cur->data == x) break; cur = cur->next; } return cur;}// 在pos的前面插入一个节点x void Insert(ListNode** ppList, ListNode* pos, DataType x){ assert(ppList); assert(pos); DataType tmp=0; ListNode* newnode = Buynewnode(x); if (*ppList == NULL){ PushBack(ppList, x); } else if (pos == NULL){ return; } else{ newnode->next =pos->next; pos->next = newnode; tmp = pos->data; pos->data = newnode->data; newnode->data = tmp; }}//删除pos前一位置节点void Erase(ListNode** ppList, ListNode* pos){ assert(ppList); assert(pos); ListNode* cur = *ppList; if (cur == NULL){ return; } else{ while (cur->next!=pos&&cur){ cur = cur->next; } cur->next = pos->next; free(pos); pos = NULL; }}
测试函数
#include "slist.h"//测试void test1(){ ListNode *list; InitLinkList(&list); printf("插入数据\n"); PushBack(&list, 1); PushBack(&list, 2); PushBack(&list, 3); PushBack(&list, 4); PushBack(&list, 5); PushBack(&list, 6); PrintList(list); printf("从尾部删除数据后\n"); PopBack(&list); PopBack(&list); PrintList(list); ListNode* _find; _find=Find(list, 2); printf("找到节点pos:%d\n", _find->data); ListNode* pos = _find; printf("在pos前插入节点:\n"); Insert(&list,pos, 7); PrintList(list); printf("删除pos前节点:\n"); Erase(&list, pos); PrintList(list);}void test2(){ ListNode* List; InitLinkList(&List); printf("头插数据\n"); PushFront(&List, 1); PushFront(&List, 2); PushFront(&List, 3); PushFront(&List, 4); PrintList(List); printf("头删数据后"); PopFront(&List); PopFront(&List); PopFront(&List); PopFront(&List); PrintList(List);}int main(){ test1(); //test2(); system("pause"); return 0;}
头文件
#define __CRT_SECURE_NO_WARNRINGS 1#ifndef __SLIST_H__#define __SLIST_H__#include <stdio.h>#include <stdlib.h>#include <assert.h>typedef int DataType;typedef struct ListNode{ DataType data; struct ListNode* next;}ListNode;void InitLinkList(ListNode** ppList);void PrintList(ListNode* pList);ListNode* Buynewnode(DataType x);void PushBack(ListNode** ppList, DataType x);void PopBack(ListNode** ppList);void PushFront(ListNode** ppList, DataType x);void PopFront(ListNode** ppList);ListNode* Find(ListNode* pList, DataType x);// 在pos的前面插入一个节点x void Insert(ListNode** ppList, ListNode* pos, DataType x);void Erase(ListNode** ppList, ListNode* pos);#endif //__SLIST_H__
测试结果:
test1:
test2:
阅读全文
0 0
- c语言实现单链表&二级指针在单链表的应用
- C语言二级指针
- C语言二级指针
- C语言 二级指针
- C语言二级指针
- C语言中二级指针的用处
- c语言的二级别指针
- C语言中二级指针的作用
- C语言二级指针的用法
- C语言二级指针(指向指针的指针)
- 98 C语言二级指针(指向指针的指针)
- C语言进阶之二级指针的应用:重置内存空间大小
- 二级指针的应用
- C语言二级指针与指针数组的结合使用.
- c语言之二级指针
- c语言--二级指针在函数间的传递和使用
- c语言中指针,二维数组,一维数组,指针数组,二级指针,应用
- C语言:一级指针,二级指针,三级指针,指针数组,数组指针的综合示例
- Redis 集群详解 环境搭建 调用案例
- CocoaPods 安装过程中的 Warning&Error 备忘
- iOS图片拉伸之神属性:resizableImageWithCapInsets
- No enclosing instance of type is accessible.
- Android Activity和Fragment的转场动画
- c语言实现单链表&二级指针在单链表的应用
- HTTP与HTTPS的区别
- opencv中Mat的数学运算和基本操作
- 链接器如何识别重复模板实例
- java与C#语法区别(持续ing)
- 解析 HTTP(HttpURLConnection getResponseCode)
- springmvc+activemq2
- 在 Laravel 5 中通过 Simple QrCode 扩展包生成二维码详解
- android发布前检查配置脚本