单链表上机实验
来源:互联网 发布:mac如何移除桌面图标 编辑:程序博客网 时间:2024/06/09 22:52
#include<iostream> typedef int ElemType;struct LNode{ElemType data;LNode* next;};void InitList(LNode* &HL){HL = NULL;}void ClearList(LNode*& HL){LNode *cp, np;for (cp = HL; cp != NULL;cp=np,cp = cp->next)delete cp;HL = NULL;}int LenthList(LNode* HL){int i = 0;while (HL != NULL){i++;HL = HL->next;}return i;}bool EmptyList(LNode* HL){return HL == NULL;}ElemType GetList(LNode* HL, int pos){LNode* cp;if (pos < 1){std::cerr << "pos is out range!" << std::endl;exit(1);}int i = 0;for (cp = HL; cp != NULL; cp = cp->next){i++;if (i == pos) break;}if (cp != NULL)return cp->data;else{std::cerr << "pos is out range!" << std::endl;exit(1);}}void TraverseList(LNode* HL){LNode* cp;for (cp = HL; cp != NULL; cp = cp->next)std::cout << cp->data << " ";std::cout << std::endl;}bool FindList(LNode* HL, ElemType& item){LNode* cp;for (cp = HL; cp != NULL; cp = cp->next)if (item == cp->data){item = cp->data;return true;}return false;}bool UpdateList(LNode* HL, const ElemType& item){LNode* cp;for (cp = HL; cp != NULL; cp = cp->next)if (item == cp->data){cp->data = item;return true;}return false;}bool InsertList(LNode* &HL, ElemType item, int pos){if (pos < -1){std::cout << "pos值无效!" << std::endl;return false;}LNode* newptr;newptr = new LNode;newptr->data = item;LNode* cp = HL;LNode* ap = NULL;if (pos == 0){for (; cp != NULL; ap = cp, cp = cp->next)if (item < cp->data) break;}else if (pos == -1)for (; cp != NULL; ap = cp, cp = cp->next){;}else{int i = 0;for (; cp != NULL;ap=cp,cp = cp->next){i++;if (i == pos) break;}if (cp == NULL&&i + 1 < pos){std::cout << "pos值超出单链表长度加一!" << std::endl;return false;}}if (ap == NULL){newptr->next = HL;HL = newptr;}else{newptr->next = cp;ap->next = newptr;}return true;}bool DeleteList(LNode* &HL, ElemType& item, int pos){if (HL == NULL){std::cerr << "单链表为空,删除操作无效!" << std::endl;return false;}if (pos < -1){std::cout << "pos值无效!" << std::endl;return false;}LNode* cp = HL;LNode* ap = NULL;if (pos == 0){for (; cp != NULL; ap=cp,cp = cp->next){if (item == cp->data){item = cp->data;break;}}if (cp == NULL){std::cout << "单链表中没有相应的结点可删除!" << std::endl;return false;}}else if (pos == -1)for (; cp->next!= NULL;ap=cp, cp = cp->next){;}else{int i = 0;for (; cp != NULL; ap=cp,cp = cp->next){i++;if (i == pos) break;}if (cp == NULL){std::cout << "pos值无效!" << std::endl;return false;}}if (ap == NULL)HL = HL->next;else ap->next = cp->next;delete cp;return true;}void SortList(LNode* &HL){LNode* SL;InitList(SL);LNode* r = HL;while (r != NULL){LNode* t = r->next;LNode* cp = SL;LNode* ap = NULL;for (; cp != NULL; ap = cp, cp = cp->next)if (r->data < cp->data) break;if (ap == NULL){r->next = SL;SL = r;}else{r->next = cp;ap->next = r;}r = t;}HL = SL;}void nixu(LNode* &HL){LNode*p, *q;p = HL;HL = NULL;while (p != NULL){q = p;p = p->next;q->next = HL;HL = q;}}ElemType maxitem(LNode* HL){LNode* cp;ElemType max = HL->data;for (cp = HL; cp != NULL; cp = cp->next)if (max < cp->data)max = cp->data;return max;}int jiedian(LNode* HL,ElemType x){LNode* cp;int i = 0;for (cp = HL; cp != NULL; cp = cp->next)if (cp->data == x)i++;return i;}void main(){int a[12], i, x;int item = 5;LNode *L;InitList(L);for (i = 0; i < 5; i++){std::cin >> a[i];InsertList(L, a[i], i + 1);}TraverseList(L);std::cout << "表长为:" << LenthList(L) << std::endl;std::cout << "在表头添加一个元素56:";InsertList(L, 56, 1);TraverseList(L);std::cout << "在表尾添加一个元素77:";InsertList(L, 77, -1);TraverseList(L);std::cout << "删除表头元素:";DeleteList(L, x, 1);TraverseList(L);std::cout << "删除表尾元素:";DeleteList(L, x, -1);TraverseList(L);std::cout << "删除表中所有值为5的元素:";for (i = 0; i < 2; i++)DeleteList(L, item, 0);TraverseList(L);nixu(L);std::cout << "逆序链接后的单链表:" ;TraverseList(L);std::cout << "单链表中最大的元素为:" << maxitem(L) << std::endl;std::cout << "请输入定值x结点个数:";std::cin >> x;std::cout << "单链表中值为x结点个数为:" << jiedian(L,x) << std::endl;system("pause");}
阅读全文
0 0
- 单链表上机实验
- 数据结构上机实验:单链表操作
- <<离散数学上机实验>>集合上机实验
- 第二周上机实验
- 第一次上机实验成果
- 第二次上机实验
- 第一次上机实验报告
- 第二次上机实验
- 第二次上机实验报告
- 第一次上机实验报告
- 第二次上机实验报告
- 第二次上机实验报告
- 第二次上机实验报告
- 第二次上机实验报
- 第三次上机实验 报告
- 第三次上机实验
- 第三次上机实验
- 第三次上机实验
- 两种方式综合请求网络数据
- <转载>java(37):java中length,length(),size()区别
- Codeforces 897C(递归)
- 小白使用react---ajax请求后的数据在另一个页面显示
- mysql数据库中管理员用户(root@localhost)被误删除,怎么恢复
- 单链表上机实验
- 封装opencv的函数成dll,独立调用
- 【Scikit-Learn 中文文档】聚类
- 从最大子数组和问题详尽贪心算法策略
- N的阶乘末尾有多少个0 51Nod
- Android使用自定义控件实现无限轮播
- hdoj-1570A C
- 可持久化Treap(范浩强Treap)
- WPF下DataGrid header 居中及分隔线等属性设置