单链表的增删查改等基本操作C++实现
生活随笔
收集整理的這篇文章主要介紹了
单链表的增删查改等基本操作C++实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
單鏈表的初始化、增刪查改、遍歷一次找中間結點、刪除一個無頭單鏈表的非尾結點(不給頭結點)
#include<stdio.h> #include<stdlib.h> #include<malloc.h> typedef?int?DataType; typedef?struct?ListNode {struct?ListNode*?_next;DataType?_data; }ListNode; void?InitList(ListNode*?&pHead) {pHead=NULL; } ListNode*?CreatNode(DataType?x) {ListNode*?tmp;tmp=(ListNode*)malloc(sizeof(ListNode));tmp->_data=x;tmp->_next=NULL;return?tmp; } void?PushFront(ListNode*?&pHead,DataType?x) {ListNode*?NewNode;if(pHead==NULL){pHead=CreatNode(x);return;}else{NewNode=CreatNode(x);NewNode->_next=pHead;pHead=NewNode;} } void?PushBack(ListNode*?&pHead,DataType?x) {ListNode*?tmp=CreatNode(x);if(pHead==NULL){pHead=tmp;return;}else{ListNode*?p=pHead;while(p->_next){p=p->_next;}p->_next=tmp;} } void?PopFront(ListNode*?&pHead) {if(pHead==NULL){printf("鏈表為空");return;}else{pHead=pHead->_next;} } void?PopBack(ListNode*?&pHead) {if(pHead==NULL){printf("鏈表為空\n");system("pause");exit(0);}else?if(pHead->_next==NULL){pHead=NULL;}else{ListNode*?tmp=pHead;while(tmp->_next->_next){tmp=tmp->_next;}tmp->_next=NULL;} } ListNode*?Find(ListNode*?pHead,DataType?x) {if(pHead==NULL){printf("鏈表為空\n");system("pause");exit(0);}else{ListNode*?tmp=pHead;while((tmp->_data!=x)&&(tmp->_next!=NULL)){tmp=tmp->_next;}if(tmp->_data==x){return?tmp;}else{printf("想找的數據不存在\n");system("pause");exit(0);}} } void?Insert(ListNode*?pos,DataType?x) {ListNode*?NewNode=CreatNode(x);NewNode->_next=pos->_next;pos->_next=NewNode; } void?Erase(ListNode*?&pHead,ListNode*?pos) {while(pHead->_next!=pos){pHead=pHead->_next;}pHead->_next=pos->_next; } //遍歷一次找中間結點 ListNode*?Cheak_MidNode(ListNode*?pHead) {ListNode*?p1=pHead;ListNode*?p2=pHead;int?count=0;while(p1->_next){p1=p1->_next;count++;if(count%2==0){p2=p2->_next;}}return?p2; } //刪除一個無頭單鏈表的非尾節點(不給頭節點) void?Delete_NoHeadNode(ListNode*?pos) {pos->_data=pos->_next->_data;pos->_next=pos->_next->_next; } void?Print(ListNode*?pHead) {while(pHead){printf("%d->",pHead->_data);pHead=pHead->_next;}printf("NULL\n"); } void?Print_Node(ListNode*?pHead,ListNode*?pos) {while(pHead!=pos){pHead=pHead->_next;}printf("中間結點為:%d\n",pos->_data); } void?Test1() {ListNode*?pHead;InitList(pHead);/*PushFront(pHead,1);PushFront(pHead,2);PushFront(pHead,3);*/PushBack(pHead,1);PushBack(pHead,2);PushBack(pHead,3);/*PopFront(pHead);PopFront(pHead);PopFront(pHead);PopFront(pHead);*//*PopBack(pHead);PopBack(pHead);PopBack(pHead);PopBack(pHead);*/Print(pHead); } void?Test2() {ListNode*?pHead;InitList(pHead);PushBack(pHead,1);PushBack(pHead,2);PushBack(pHead,3);ListNode*?pos=Find(pHead,2);//ListNode*?pos=Find(pHead,5);Insert(pos,7);Print(pHead);Erase(pHead,pos);Print(pHead); } void?Test3() {ListNode*?pHead;InitList(pHead);PushBack(pHead,1);PushBack(pHead,2);PushBack(pHead,3);PushBack(pHead,4);PushBack(pHead,5);ListNode*?pos=Cheak_MidNode(pHead);Print(pHead);Print_Node(pHead,pos); } void?Test4() {ListNode*?pHead;InitList(pHead);PushBack(pHead,1);PushBack(pHead,2);PushBack(pHead,3);PushBack(pHead,4);Print(pHead);ListNode*?pos=Find(pHead,3);Delete_NoHeadNode(pos);Print(pHead); } int?main() {//Test1();//Test2();//Test3();Test4();system("pause");return?0; }轉載于:https://blog.51cto.com/10707460/1754730
總結
以上是生活随笔為你收集整理的单链表的增删查改等基本操作C++实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bzoj 4446: [Scoi2015
- 下一篇: 使用transform和transiti