日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++链栈基本操作

發布時間:2025/3/17 c/c++ 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++链栈基本操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 #include <iostream> 2 using namespace std; 3 4 struct Node 5 { 6 int data; 7 Node *next; 8 }; 9 10 /*初始化鏈棧*/ 11 void InitChainStack(Node *top) 12 { 13 top->next=0; 14 } 15 16 /*往棧里壓進數據elem*/ 17 int Push(Node *top, int elem) 18 { 19 Node *p=(Node*)malloc(sizeof(Node)); 20 if(p==0) 21 return false; 22 p->data=elem; 23 p->next = top->next; 24 top->next=p; 25 return true; 26 } 27 28 /*出棧,并用x返回出棧值*/ 29 int Pop(Node *top, int &x) 30 { 31 Node *temp = top->next; 32 if(temp==0) 33 return false; 34 top->next = temp->next; 35 x=temp->data; 36 free(temp); 37 return true; 38 } 39 40 /*返回棧頂數據*/ 41 int GetTop(Node * top, int &x) 42 { 43 if(top->next==0) 44 return false; 45 x=top->next->data; 46 return true; 47 } 48 49 /*判斷棧是否為空*/ 50 int IsEmpty(Node * top) 51 { 52 if(top->next==0) 53 return true; 54 return false; 55 } 56 57 /*打印棧所有數據元素*/ 58 void PrintStack(Node *top) 59 { 60 Node *temp = top->next; 61 while(temp!=0) 62 { 63 cout << temp->data << " "; 64 temp=temp->next; 65 } 66 cout << endl; 67 } 68 69 void main() 70 { 71 Node *node=new Node; 72 InitChainStack(node); 73 74 for(int i=0;i<10;i++) 75 Push(node,i); 76 int x=0; 77 Pop(node,x); 78 GetTop(node,x); 79 cout << x << endl; 80 cout << IsEmpty(node) << endl; 81 PrintStack(node); 82 }

?

轉載于:https://www.cnblogs.com/xxdfly/p/4381119.html

總結

以上是生活随笔為你收集整理的C++链栈基本操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。