C++实现链表的进本操作及测试用例
来源:互联网 发布:爱奇艺去广告优化版 编辑:程序博客网 时间:2024/05/29 14:30
今天实现了下链表的基本操作,包括节点的创建,头插尾插,头删尾删,一次遍历寻找链表的中间节点,寻找链表的倒数第x个节点,删除无头链表的非尾节点,链表的逆置,代码如下:
#include "SLinkList.h"#include <stdlib.h>#include <stdio.h>#include <assert.h>void PrintList(SListNode* pHead)//从指针位置打印链表{while (pHead){printf("->%d", pHead->data);pHead = pHead->next;}printf("\n");}static SListNode* _BuyNode(DataType x)//创建新的节点{SListNode*ret = (SListNode*)malloc(sizeof(SListNode));ret->next = NULL;ret->data = x;if (ret){return ret;}printf("创建节点失败\n");//创建失败给出提示return NULL;}void PushBack(SListNode* & pHead, DataType x)//尾插{if (pHead == NULL){pHead = _BuyNode(x);}else{SListNode*tail = pHead;while (tail->next){tail = tail->next;}tail->next = _BuyNode(x);}}void PopBack(SListNode* & pHead)//尾删{if (pHead == NULL){return;}else if (pHead->next->next == NULL){free(pHead->next);pHead = NULL;}else{SListNode* tail = pHead;while (tail->next->next){tail = tail->next;}free(tail->next);tail->next = NULL;}}void PushFront(SListNode* & pHead, DataType x)//头插{if (pHead == NULL){pHead = _BuyNode(x);}else{SListNode*tmp = _BuyNode(x);tmp->next = pHead;pHead = tmp;}}void PopFront(SListNode* & pHead)//t头删{if (pHead == NULL){return;}else{SListNode*tail = pHead;pHead = pHead->next;tail->next = NULL;free(tail);}}SListNode* Find(SListNode *pHead, DataType x)//以x查找节点,若存在返回给节点,若不存在返回空{if (pHead == NULL){return NULL;}SListNode* tail = pHead;while (tail){if (tail->data == x){return tail;}tail = tail->next;}return NULL;}void Insert(SListNode* & pos, DataType x)//所给位置后插入一节点{SListNode*tmp = _BuyNode(x);if (pos == NULL){pos = tmp;}else{tmp->next = pos->next;pos->next = tmp;}}void Erase(SListNode * &pos)//删除无头链表的非尾节点{if (pos == NULL){return;}else if (pos->next == NULL){printf("该节点为尾节点,无法删除\n");}else{SListNode*tmp = pos->next;pos->data = pos->next->data;pos->next = pos->next->next;free(tmp);tmp = NULL;}}void ReveseList(SListNode * &pHead)//链表的逆置{SListNode*tail = pHead;while (tail->next){SListNode*tmp=NULL;tmp = tail->next;tail->next = tail->next->next;tmp->next = pHead;pHead = tmp;}}SListNode* FindminList(SListNode*pHead)//一次遍历,寻找链表的中间节点{assert(pHead);SListNode *quick=pHead;SListNode *slow=pHead;while (quick){slow = slow->next;if (quick->next)quick = quick->next->next;elsereturn slow;}return slow;}SListNode* FindListPosList(SListNode*pHead, int lastpos)//寻找链表的倒数第lastpos个节点{SListNode *quick = pHead;SListNode *slow = pHead;while (quick&&lastpos--){quick = quick->next;}if (quick == NULL){return slow;}while (quick){quick = quick->next;slow = slow->next;}return slow;}
测试用例如下:
#include <stdio.h>#include <stdlib.h>#include "SLinkList.h"void Test1()//PushBack,PrintList,PopBack{SListNode*pHead=NULL;PushBack(pHead, 1);PushBack(pHead, 2);PushBack(pHead, 3);PushBack(pHead, 4);PushBack(pHead, 5);PrintList(pHead);PopBack(pHead);PopBack(pHead);PrintList(pHead);PopBack(pHead);PopBack(pHead);PopBack(pHead);PrintList(pHead);}void Test2()//PushFront,Popfront{SListNode*pHead = NULL;PushFront(pHead, 5);PushFront(pHead, 4);PushFront(pHead, 3);PushFront(pHead, 2);PushFront(pHead, 1);PrintList(pHead);PopFront(pHead);PrintList(pHead);PopFront(pHead);PrintList(pHead);PopFront(pHead);PrintList(pHead);PopFront(pHead);PrintList(pHead);PopFront(pHead);PrintList(pHead);PopFront(pHead);PrintList(pHead);}void Test3()//Find,Insert{SListNode*pHead = NULL;PushFront(pHead, 5);PushFront(pHead, 4);PushFront(pHead, 3);PushFront(pHead, 2);PushFront(pHead, 1);Insert(pHead, 0);pHead = Find(pHead, 3);PrintList(pHead);Insert(pHead, 4);PrintList(pHead);}void Test4()//Erase{SListNode*pHead = NULL;SListNode*tmp = NULL;PushFront(pHead, 5);PushFront(pHead, 4);PushFront(pHead, 3);PushFront(pHead, 2);PushFront(pHead, 1);tmp = Find(pHead, 5);Erase(tmp);PrintList(pHead);}void Test5()//ReveseList{SListNode*pHead = NULL;PushBack(pHead, 1);PushBack(pHead, 2);PushBack(pHead, 3);PushBack(pHead, 4);PushBack(pHead, 5); ReveseList(pHead);PrintList(pHead);}void Test6()//FindLastposList{SListNode*pHead = NULL;SListNode*ret = NULL;PushBack(pHead, 1);PushBack(pHead, 2);PushBack(pHead, 3);PushBack(pHead, 4);PushBack(pHead, 5); ret=FindListPosList(pHead, 2);printf("%d\n", ret->data);ret = FindListPosList(pHead, 6);printf("%d\n", ret->data);}int main(){Test1();Test2();Test3();Test4();Test5();Test6();system("pause");return 0;}
如有不足希望指正,有疑问希望提出
本文出自 “pawnsir的IT之路” 博客,请务必保留此出处http://10743407.blog.51cto.com/10733407/1730962
0 0
- C++实现链表的进本操作及测试用例
- (2010计本3班-杨蒙)面向对象的C--实现链表操作
- 用C语言实现Prim算法及测试用例
- 【c++】实现双向链表List类及测试
- 【数据结构】链表及相关操作(C语言实现)
- 卷积操作的GPU粗粒度并行实现及测试
- 本例主要实现任意删除及增1和补缺序联动操作,实际应用与数据库,这里用新建表做实例。
- C++:操作单向链表的实现
- MD5算法的C代码实现及测试
- 顺序线性表的实现及操作(C语言实现)
- 【代码】C++实现二叉树基本操作及测试用例
- 链表的实现与操作(C语言实现)
- 静态链表的实现与操作(C语言实现)
- 循环链表的实现与操作(C语言实现)
- 双向链表的实现与操作(C语言实现)
- C语言字符串操作函数及常用的实现
- AVL平衡树及插入操作的C语言实现
- 二叉树的实现及相关操作C/C++
- C语言模拟实现memset.memcmp函数
- C语言用结构体写一个通讯录
- 提交Android代码的格式
- C语言实现C到C++的注释转换
- C语言实现顺序表的增删查改以及排序
- C++实现链表的进本操作及测试用例
- Android中的储存
- c++日期类的实现级运算符的重载
- C++复数类的运算符重载
- 用C语言实现二分查找算法
- 求最大公约数的设计与C语言实现
- String类的实现与深浅拷贝问题
- 如何限制类仅能创建一个对象
- C++单链表的设计与实现