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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

顺序栈的简单操作

發布時間:2024/3/13 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 顺序栈的简单操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
關于順序棧的簡單操作如下, 由于主要介紹思想實現,讀者從代碼中可以很容易看出很多漏洞。如有錯誤之處,請大家指正!

點擊(此處)折疊或打開

  • #include<iostream>
  • using namespace std;
  • #define MAXSIZE 10

  • ?struct Stack {

  • ????int data[MAXSIZE];
  • ????int top;
  • };

  • ?Stack *CreateStack(); ? ? //該步驟函數聲明必須放在結構體后面
  • ?void?InitialStack(Stack *);
  • ?void InsertStack(Stack *, int, int);
  • ?void DeleteStack(Stack *, int);
  • ?void PrintStack(Stack *);

  • Stack *CreateStack() {

  • ????Stack *p = new Stack; ?//創建指向棧的指針p
  • ????p->top = -1; ? ? ? ? ? //因為是順序棧,存儲數據放在數組中,為了一致性,top指針置于-1,表明棧為空
  • ????return p;

  • }

  • void InitialStack(Stack *p) {

  • ????Stack *head;
  • ????head = p;

  • ????cout << "入棧的順序是:" << endl;
  • ????
  • ????int MAX_SIZE = MAXSIZE - 1;

  • ????while (head->top != MAX_SIZE - 1) {

  • ????????head->top++; ? //該步可以省略,下步改為:head->data[++head->top] = head->top;就是賦值與自加的先后順序
  • ????????head->data[head->top] = head->top;
  • ????????cout << head->data[head->top] << "--&gt";
  • ????}

  • ????cout << "輸出結束";
  • ????cout << endl;
  • ????
  • }

  • void InsertStack(Stack *p, int i, int x) {
  • ????
  • ????Stack *q;

  • ????int length = p->top; //這里一定要注意,棧只能從一端進入彈出,要保證top指針不能夠被修改
  • ????p->top += 1; ? ? ? ? //由于插入一個數據,棧長度+1(更準確的說是數組下標+1)
  • ????q = p;

  • ????int llength = q->top;
  • ????int flag = llength;

  • ????for (int n = 0; n <= flag - i; n++) {?//一定要保證變與不變,算法實現,通過圖解讀者可以清楚知道各參數意義

  • ????????q->data[llength--] = q->data[length--];?
  • ????}

  • ????q->data[i -1] = x;

  • ????
  • }

  • void DeleteStack(Stack *p, int i) {

  • ????Stack *q;

  • ????int length = p->top;
  • ????q = p;

  • ????int l = i;
  • ????int j = i - 1;

  • ????for (int n = 0; n <= length - l+1; n++) {

  • ????????q->data[j++] = q->data[i++]; ? //刪除操作,對于數組很簡單就是覆蓋操作
  • ????}

  • ????p->top = p->top - 1;

  • ? ?
  • }
  • void PrintStack(Stack *p) {

  • ????Stack *q;
  • ????q = p;

  • ????cout << "出棧的順序是:" << endl;

  • ????while (q->top >=0) {

  • ????????cout << q->data[q->top--] << "--&gt";
  • ????????
  • ????}

  • ????cout << "出棧結束" << endl;
  • }

  • int main() {

  • ????Stack *p;

  • ????p = CreateStack();
  • ???? InitialStack(p);

  • ???// InsertStack(p, 5, 11);
  • ????//PrintStack(p);

  • ????DeleteStack(p, 5);
  • ????PrintStack(p);
  • }
  • 來自 “ ITPUB博客 ” ,鏈接:http://blog.itpub.net/29876893/viewspace-1814693/,如需轉載,請注明出處,否則將追究法律責任。

    轉載于:http://blog.itpub.net/29876893/viewspace-1814693/

    總結

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

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