我國自研大型水陸兩棲飛機 AG600 鐵鳥綜合試驗檯初步設計順利通過評審
阿新 • • 發佈:2021-09-11
很多資料都是帶頭結點的棧實現,感覺不帶頭結點實現也更容易啊。
// 有本句 ,下面cout 前面可以沒有 std:: using namespace std; typedef char ElemType; //元素資料型別 char #define MAXSIZE 10 //普通鏈節點結構 typedef struct LinkNode { ElemType Data; struct LinkNode* Next; }LinkNode; //鏈棧結構 關鍵成員為指向鏈節點的指標 typedef struct LinkStack { LinkNode* Top; intLen; }*LS; //初始化 鏈結構成員賦初值 void InitLS(LS &S) { S = new LinkStack; S->Top = NULL; S->Len = 0; } //入棧 申請鏈節點,修改指標 void Push(LS& S,ElemType e) { LinkNode* t = new LinkNode; t->Data = e; t->Next = S->Top; S->Top = t; S->Len++; } //出棧 取資料,釋放資源,修改指標bool Pop(LS &S,ElemType &e) { LinkNode* p = S->Top; if (p){ e= p->Data; S->Top = p->Next; delete p; S->Len--; } else{ cout << " Empty! "; e = ' '; return 0; } } void main() { LS S; InitLS(S); Push(S,'a'); Push(S, 'b'); Push(S, 'c'); Push(S, 'd'); ElemType e; Pop(S,e); cout <<"Pop " << e << " Len = "<< S->Len; cout << endl; Pop(S, e); cout << "Pop " << e << " Len = " << S->Len; cout << endl; Pop(S, e); cout << "Pop " << e << " Len = " << S->Len; cout << endl; Pop(S, e); cout << "Pop " << e << " Len = " << S->Len; cout << endl; Pop(S, e); cout << "Pop " << e << " Len = " << S->Len; cout << endl; }