单链表上机实验

来源:互联网 发布: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");}

原创粉丝点击