数据结构-单链表
来源:互联网 发布:机械工艺设计软件 编辑:程序博客网 时间:2024/06/07 16:16
/*---------------------------------1.创建过程2.操作过程3.显示过程---------------------------------创建链表 (创建表头)创建节点插入节点 (表头,表尾,指定位置)判断链表是否为空删除节点 (表头,表尾,指定位置)打印链表----------------------------------*/#include<iostream>using namespace std;typedef struct SingleList{//数据域int data;//指针域struct SingleList* next;}SINGLELIST,*LPSINGLELIST;
/*-------------------------------------------------功能独立----函数功能划分明确-------------------------------------------------*/
//创建表LPSINGLELIST CreateList(){//创建过程----初始化基本数据成员// 申请内存LPSINGLELIST List = new SINGLELIST;// 初始化基本数据成员List->next = nullptr;//表头不需要初始化数据域return List;}
//创建节点
LPSINGLELIST CreateNode(int data){// 申请内存LPSINGLELIST Node = new SINGLELIST;// 初始化基本数据成员Node->data = data;Node->next = nullptr;//表头不需要初始化数据域return Node;}
//判断为空---和创建过程相比较bool IsEmptyList(LPSINGLELIST List){if (List->next == nullptr)return 1;return 0;}
void InsertHeadNode(LPSINGLELIST List, int data){//插入操作 ---创建插入的结点LPSINGLELIST newNode = CreateNode(data);//先连后断newNode->next = List->next;List->next = newNode;}
void InsertTailNode(LPSINGLELIST List, int data){LPSINGLELIST tailNode = List;while (tailNode->next != NULL){tailNode = tailNode->next;}LPSINGLELIST newNode = CreateNode(data);tailNode->next = newNode;}
void InsertAppoinNode(LPSINGLELIST List, int data, int item){LPSINGLELIST frontNode = List;LPSINGLELIST tailNode = List->next;while (tailNode->data != item){frontNode = tailNode;tailNode = frontNode->next;if (tailNode== nullptr){cout << "为找指定位置" << endl;return;}}LPSINGLELIST newNode = CreateNode(data);frontNode->next = newNode;newNode->next = tailNode;}
void DeleteHeadNode(LPSINGLELIST List){if (IsEmptyList(List)){cout << "链表为空无法删除" << endl;return;}LPSINGLELIST headNode = List->next;List->next = headNode->next;delete headNode;}
void DeleteAppoinNode(LPSINGLELIST List, int item){LPSINGLELIST frontNode = List;LPSINGLELIST tailNode = List->next;while (tailNode->data != item){frontNode = tailNode;tailNode = frontNode->next;if (tailNode == nullptr){cout << "为找指定位置" << endl;return;}}frontNode->next = tailNode->next;delete tailNode;}
void DeleteTailNode(LPSINGLELIST List){//找到表尾if (IsEmptyList(List)){cout << "链表为空无法删除" << endl;return;}LPSINGLELIST frontNode = List;LPSINGLELIST tailNode = List->next;while (tailNode->next != nullptr){frontNode = tailNode;tailNode = frontNode->next;}frontNode->next = nullptr;delete tailNode;}
void PrintList(LPSINGLELIST List){LPSINGLELIST nextNode = List->next;while (nextNode){cout << nextNode->data<<" ";nextNode = nextNode->next;}}int main(){LPSINGLELIST List = CreateList();//创建链表cout << "InsertHeadNode:" << endl;InsertHeadNode(List, 1);InsertHeadNode(List, 2);InsertHeadNode(List, 3);PrintList(List);cout <<endl <<"InsertTailNode:" << endl;InsertTailNode(List, 5);InsertTailNode(List, 6);PrintList(List);cout << endl << "InsertAppoinNode:" << endl;InsertAppoinNode(List, 4, 5);PrintList(List);cout << endl << "DeleteHeadNode:" << endl;DeleteHeadNode(List);PrintList(List);cout << endl << "DeleteTailNode:" << endl;DeleteTailNode(List);PrintList(List);cout << endl << "DeleteAppoinNode:" << endl;DeleteAppoinNode(List, 4);PrintList(List);system("pause");return 0;}完整版本:
/*---------------------------------1.创建过程2.操作过程3.显示过程---------------------------------创建链表 (创建表头)创建节点插入节点 (表头,表尾,指定位置)判断链表是否为空删除节点 (表头,表尾,指定位置)打印链表----------------------------------*/#include<iostream>using namespace std;typedef struct SingleList{//数据域int data;//指针域struct SingleList* next;}SINGLELIST,*LPSINGLELIST;/*定义别的名SINGLIST ------struct SingleListLPSINGLELIST ----struct SingleList**//*-------------------------------------------------功能独立----函数功能划分明确-------------------------------------------------*/LPSINGLELIST CreateList(){//创建过程----初始化基本数据成员// 申请内存LPSINGLELIST List = new SINGLELIST;// 初始化基本数据成员List->next = nullptr;//表头不需要初始化数据域return List;}LPSINGLELIST CreateNode(int data){// 申请内存LPSINGLELIST Node = new SINGLELIST;// 初始化基本数据成员Node->data = data;Node->next = nullptr;//表头不需要初始化数据域return Node;}//判断为空---和创建过程相比较bool IsEmptyList(LPSINGLELIST List){if (List->next == nullptr)return 1;return 0;}void InsertHeadNode(LPSINGLELIST List, int data){//插入操作 ---创建插入的结点LPSINGLELIST newNode = CreateNode(data);//先连后断newNode->next = List->next;List->next = newNode;}void InsertTailNode(LPSINGLELIST List, int data){LPSINGLELIST tailNode = List;while (tailNode->next != NULL){tailNode = tailNode->next;}LPSINGLELIST newNode = CreateNode(data);tailNode->next = newNode;}void InsertAppoinNode(LPSINGLELIST List, int data, int item){LPSINGLELIST frontNode = List;LPSINGLELIST tailNode = List->next;while (tailNode->data != item){frontNode = tailNode;tailNode = frontNode->next;if (tailNode== nullptr){cout << "为找指定位置" << endl;return;}}LPSINGLELIST newNode = CreateNode(data);frontNode->next = newNode;newNode->next = tailNode;}void PrintList(LPSINGLELIST List){LPSINGLELIST nextNode = List->next;while (nextNode){cout << nextNode->data<<" ";nextNode = nextNode->next;}}void DeleteHeadNode(LPSINGLELIST List){if (IsEmptyList(List)){cout << "链表为空无法删除" << endl;return;}LPSINGLELIST headNode = List->next;List->next = headNode->next;delete headNode;}void DeleteTailNode(LPSINGLELIST List){//找到表尾if (IsEmptyList(List)){cout << "链表为空无法删除" << endl;return;}LPSINGLELIST frontNode = List;LPSINGLELIST tailNode = List->next;while (tailNode->next != nullptr){frontNode = tailNode;tailNode = frontNode->next;}frontNode->next = nullptr;delete tailNode;}void DeleteAppoinNode(LPSINGLELIST List, int item){LPSINGLELIST frontNode = List;LPSINGLELIST tailNode = List->next;while (tailNode->data != item){frontNode = tailNode;tailNode = frontNode->next;if (tailNode == nullptr){cout << "为找指定位置" << endl;return;}}frontNode->next = tailNode->next;delete tailNode;}int main(){LPSINGLELIST List = CreateList();//创建链表cout << "InsertHeadNode:" << endl;InsertHeadNode(List, 1);InsertHeadNode(List, 2);InsertHeadNode(List, 3);PrintList(List);cout <<endl <<"InsertTailNode:" << endl;InsertTailNode(List, 5);InsertTailNode(List, 6);PrintList(List);cout << endl << "InsertAppoinNode:" << endl;InsertAppoinNode(List, 4, 5);PrintList(List);cout << endl << "DeleteHeadNode:" << endl;DeleteHeadNode(List);PrintList(List);cout << endl << "DeleteTailNode:" << endl;DeleteTailNode(List);PrintList(List);cout << endl << "DeleteAppoinNode:" << endl;DeleteAppoinNode(List, 4);PrintList(List);system("pause");return 0;}
阅读全文
1 0
- 数据结构---单链表
- 数据结构--单链表
- 数据结构单链表
- 数据结构单链表
- 【数据结构】单链表
- 数据结构单链表
- [数据结构]单链表
- 数据结构--单链表
- 数据结构--单链表
- 数据结构-单链表
- 数据结构 -- 单链表
- 数据结构-单链表
- 数据结构 --- 单链表
- 数据结构---单链表
- 数据结构--单链表
- 数据结构 单链表
- 数据结构-单链表
- 数据结构---单链表
- 第一章 动手试一试
- 虚拟机 网络模式简介 及桥接模式设置
- Matlab 读写文件
- 融会贯通——工厂模式全方位解析
- matlab写coe文件
- 数据结构-单链表
- nginx面试要点--持续更新中
- node.js中get和post请求
- 中兴执行副总裁熊辉回母校谈目标:相信总有路可走
- 树莓派搭建图像识别基站
- 【2-sat】和平委员会
- 例题6-10 下落的树叶(The Falling Leaves, UVa 699)
- MYSQL中的视图
- cd,ls,ln,tree,文件系统