C++ 循环链表练习题 报数删除【非常没有条理】
生活随笔
收集整理的這篇文章主要介紹了
C++ 循环链表练习题 报数删除【非常没有条理】
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
好長(zhǎng)時(shí)間沒碰數(shù)據(jù)結(jié)構(gòu)了 寫的挺亂的 看來要多練啊
有個(gè)輸入報(bào)數(shù)不能從1報(bào)到1的bug
運(yùn)行效果
請(qǐng)輸入總節(jié)點(diǎn)個(gè)數(shù):100
從1開始報(bào)到幾?3
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 2 7 11 16 20 25 29 34 38 43 47 52 56 61 65 70 74 79 83 88 92 97 1 8 14 22 28 35 41 49 55 62 68 76 82 89 95 4 13 23 32 44 53 64 73 85 94 5 19 37 50 67 80 98 17 40 59 86 10 46 77 26 71 31 100 58
剩余的數(shù)字:91
請(qǐng)按任意鍵繼續(xù). . .
代碼
#include<iostream> using namespace std;typedef struct LinkList {int num;LinkList* pnext; }List, *plist;void add(plist *pphead, plist *pptail, int num) {plist pnew = (plist)malloc(sizeof(List));pnew->num = num;//插入if (*pphead == NULL){*pphead = pnew;*pptail = pnew;pnew->pnext = *pphead;}else{(*pptail)->pnext = pnew;*pptail = pnew;pnew->pnext = *pphead;//成環(huán)}}int main() {plist phead = NULL;plist ptail = NULL;cout << "請(qǐng)輸入總節(jié)點(diǎn)個(gè)數(shù):";int total;cin >> total;int left = total;int i;for (i = 0; i < total; i++){add(&phead, &ptail, i + 1);}int end;cout << "從1開始報(bào)到幾?";cin >> end;int chu = end;int j, k;plist pcur = phead;plist pbefore = pcur;for (i = 0; i < total; i++){pbefore = pcur;if (left == 1){cout << endl;cout << "剩余的數(shù)字:" << pcur->num << endl;break;}//到這個(gè)刪除for (j = 0; j < end - 1; j++){pbefore = pcur;pcur = pcur->pnext;}//刪除pbeforecout << pcur->num << ' ';pbefore->pnext = pcur->pnext;free(pcur);pcur = pbefore->pnext;left--;}system("pause"); }總結(jié)
以上是生活随笔為你收集整理的C++ 循环链表练习题 报数删除【非常没有条理】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# 对get和set的理解
- 下一篇: C++ 大整数加法