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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

数据结构(二)栈

發布時間:2023/11/30 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构(二)栈 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  • 一、棧
    • 順序棧
    • 線性棧(不帶頭結點)
    • 線性棧(帶頭結點)
    • 共享棧


一、棧

棧是只允許在一端進行插入或刪除操作的線性表。
棧頂:線性表允許進行插入刪除的那一端
棧底:固定的,不允許進行插入和刪除的那一端
空棧:不含如何元素的空表

順序棧

// linear_stack.cpp : This file contains the 'main' function. Program execution begins and ends there. //#include <iostream> #include <stdio.h> #include <stdlib.h>#define Maxsize 50 #define Elemtype int typedef struct {Elemtype data[Maxsize]; //存放棧中元素int top; //棧頂指針 }SqStack;//初始化棧 void InitStack(SqStack &S) {S.top = -1; }//判空 bool StackEmpty(SqStack& S) {if (S.top == -1){return true; //判空}else{return false; //不空} }//進棧 bool Push(SqStack& S, Elemtype x) {if (S.top == Maxsize - 1){return false; //棧滿了}S.data[++S.top] = x;return true; }//出棧bool Pop(SqStack& S, Elemtype &x) {if (S.top == -1){return false; //棧尾了}x = S.data[S.top--];return true; }//讀棧頂數據 bool GetTop(SqStack& S, Elemtype& x) {if (S.top == -1){return false; //棧尾了}x = S.data[S.top];return true; }int main() {SqStack S;int x = 0;InitStack(S);Push(S,2);Push(S, 3);Pop(S, x);for (int i = 0; i <= S.top; i++){printf("%d \t", S.data[i]);} }// Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

線性棧(不帶頭結點)

// linear_stack.cpp : This file contains the 'main' function. Program execution begins and ends there. //#include <iostream> #include <stdio.h> #include <stdlib.h>#define Maxsize 50 #define Elemtype int typedef struct Linknode {Elemtype data; //數據域struct Linknode* next; //指針域 }*LiStack;//初始化棧 不帶頭結點 void InitStack(LiStack & S) {S = NULL; }//判空 bool StackEmpty(LiStack& S) {if (S == NULL){return true; //判空}else{return false;} }//進棧 bool Push(LiStack& S, Elemtype x) {Linknode* p = (Linknode*)malloc(sizeof(Linknode));p->next = NULL;p->data = x;p->next = S;S = p;return true; }//出棧bool Pop(LiStack& S) {Linknode* p;if (S == NULL){return false;}p = S;S = S->next;free(p);return true;}//讀棧頂數據 bool GetTop(LiStack& S, Elemtype& x) {if (S == NULL){return false;}x = S->data;return true; }int main() {LiStack S;InitStack(S);int x = 0;Push(S, 2);Push(S, 3);Pop(S);while (S != NULL){printf("%d \t ",S->data);S = S->next;}}// Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

線性棧(帶頭結點)

// linear_stack.cpp : This file contains the 'main' function. Program execution begins and ends there. //#include <iostream> #include <stdio.h> #include <stdlib.h>#define Maxsize 50 #define Elemtype int typedef struct Linknode {Elemtype data; //數據域struct Linknode* next; //指針域 }*LiStack;//初始化棧 帶頭結點 void InitStack(LiStack& S) {//初始化S = (LiStack)malloc(sizeof(Linknode));S->data = 0;S->next = NULL; }//判空 bool StackEmpty(LiStack& S) {if (S->next == NULL){return true; //判空}else{return false;} }//進棧 bool Push(LiStack& S, Elemtype x) {Linknode* p = (Linknode*)malloc(sizeof(Linknode));p->data = x;p->next = S->next;S->next = p;return true; }//出棧bool Pop(LiStack& S) {Linknode* p;if (S->next == NULL){return false;}p = S->next;S->next = p->next;free(p);return true;}//讀棧頂數據 bool GetTop(LiStack& S, Elemtype& x) {if (S->next == NULL){return false;}x = S->next->data;return true; }int main() {LiStack S;InitStack(S);int x = 0;Linknode* p;Push(S, 2);Push(S, 3);Pop(S);p = S->next;while (p != NULL){printf("%d \t ", p->data);p = p->next;}}// Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file

共享棧

#include <cstdio> #include <stdio.h> #include <stdlib.h> #define maxsize 3#define elemtype inttypedef struct {elemtype data[maxsize];int top[2]; }stk;stk s;int push(int i, elemtype x) {if (i > 1 || i < 0){printf("棧編號錯誤!\n");exit(0);}if ((s.top[1] - 1) == s.top[0]){printf("棧滿了!");return -1;}switch (i){case 1:s.data[--s.top[1]] = x;break;case 0:s.data[++s.top[0]] = x;break;}//添加成功return 1;}int pop(int i) {if (i > 1 || i < 0){printf("棧編號錯誤!\n");exit(0);}switch (i){case 1:if (s.top[1] == maxsize){printf("棧底了!\n");exit(0);}elsereturn s.data[s.top[1]++];break;case 0:if (s.top[0] == -1){printf("棧底了!\n");exit(0);}elsereturn s.data[s.top[1]--];break;}//添加成功return 1;}int main() {s.top[0] = -1;s.top[1] = maxsize;push(0, 2);push(1, 5);push(1, 7);push(1, 9);for (int i = 0; i < maxsize; i++){printf("%d\n", s.data[i]);}return 0; }

總結

以上是生活随笔為你收集整理的数据结构(二)栈的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 国产手机在线观看 | 波多野结衣视频免费在线观看 | 综合黄色 | 亚洲精选一区二区三区 | 亚洲精品白虎 | 日本不卡视频一区 | 欧美另类精品 | 日本午夜在线 | 国产黄色在线播放 | 欧美日韩在线中文字幕 | 少妇裸体性生交 | 国产精品网页 | 中文字幕 自拍偷拍 | 亚洲一区二区在线视频 | 亚洲色综合 | 综合一区在线 | 青青伊人精品 | www.亚洲免费 | 第四色成人网 | 国产内谢| 久久国产精品精品国产色婷婷 | 蜜桃臀aⅴ精品一区二区三区 | 香蕉茄子视频 | 三级视频黄色 | 日韩一级片免费 | 日本在线视频一区 | 中文字幕在线免费看线人 | 午夜免费福利影院 | 日韩sese| 黄色成人在线视频 | 亚洲欧美中文字幕5发布 | 国产午夜精品久久久 | 国产性av | 黄色www| 欧美色综合网 | 亚洲欧美bt | 国内精品嫩模av私拍在线观看 | 天堂在线中文资源 | youjizz.com中国| wwwxxx在线观看 | 美女娇喘 | 国产福利小视频在线 | 欧美在线一区二区视频 | 日韩欧美精品在线观看 | 丝袜美腿中文字幕 | 成人性生活免费看 | 对白超刺激精彩粗话av | 中文一区二区在线 | 熟女一区二区三区四区 | 国产深喉视频一区二区 | 爱插网| 久久无码视频网站 | 亚洲三级视频 | aa毛片视频| 狠狠干青青草 | 香蕉久久夜色精品 | 91网站免费 | 三级视频在线观看 | 国语播放老妇呻吟对白 | 国产馆视频 | 精品成人一区 | 午夜精品视频一区二区三区在线看 | 久久久久亚洲av成人毛片韩 | 黄色电影在线视频 | av黄色天堂| 国产调教av| 一区二区传媒有限公司 | 亚洲欧美日韩专区 | 男男play视频| 99久久久国产精品无码免费 | 天天干狠狠爱 | 亚洲欧美日韩精品在线观看 | jiizzyou欧美2| 波多野结衣一区二区三区在线 | 激情久| 国产传媒视频在线观看 | 中字幕视频在线永久在线观看免费 | 欧美日韩三区 | 老司机一区 | 伊人久久大香网 | 国产美女久久久久久 | 亚洲成人少妇 | 午夜毛片电影 | 日韩精品视频播放 | 欧美韩国一区 | 午夜不卡福利视频 | 日韩免费视频观看 | 人与拘一级a毛片 | aa片在线观看视频在线播放 | 亚洲乱码少妇 | 青青草视频在线观看免费 | 国产一级不卡毛片 | 99精品视频在线看 | 成人网免费看 | 韩国成人免费视频 | 91福利视频网 | 中文字幕在线播放一区 | 久草国产在线观看 | 午夜a区|