數據結構(二)棧

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


一、棧

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

順序棧

// 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;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384384.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384384.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384384.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

如何在linux上安裝sqlite數據庫

如何在linux上安裝sqlite數據庫一、下載二、解壓三、配置&#xff08;configure&#xff09;四、編譯和安裝五、執行sqlite3程序六、測試代碼一、下載 首先要先下載sqlite3源碼包 鏈接&#xff1a;https://pan.baidu.com/s/1_70342ZLlPjLlqGzpy5IHw 提取碼&#xff1a;84ne …

數據結構(三)隊列

數據結構&#xff08;三&#xff09;隊列隊列隊列&#xff08;順序存儲&#xff09;循環隊列&#xff08;順序存儲&#xff09;隊列&#xff08;鏈式存儲&#xff09;隊列 隊列是一種受限制的線性表&#xff0c;只允許表的一端插入&#xff0c;在表的另一端刪除 隊列&#xf…

Linux fcntl函數詳解

轉載&#xff1a;http://www.cnblogs.com/xuyh/p/3273082.html 功能描述&#xff1a;根據文件描述詞來操作文件的特性。 文件控制函數 fcntl -- file control 頭文件&#xff1a; #include <unistd.h> #include <fcntl.h> 函數原型&#xff1a; …

vs2019使用sqlite數據庫遠程連接linux

vs2019使用sqlite數據庫遠程連接linux一、sqlite3添加到目錄二、添加依賴庫三、測試一、sqlite3添加到目錄 將兩個sqlite3頭文件放入目錄中 二、添加依賴庫 打開項目屬性 添加完成 三、測試 #include <stdio.h> #include <sqlite3.h>int main(int argc, cha…

linux網絡編程(四)線程池

linux網絡編程&#xff08;四&#xff09;線程池為什么會有線程池&#xff1f;實現簡單的線程池為什么會有線程池&#xff1f; 大多數的服務器可能都有這樣一種情況&#xff0c;就是會在單位時間內接收到大量客戶端請求&#xff0c;我們可以采取接受到客戶端請求創建一個線程的…

AIGC:大語言模型LLM的幻覺問題

引言 在使用ChatGPT或者其他大模型時&#xff0c;我們經常會遇到模型答非所問、知識錯誤、甚至自相矛盾的問題。 雖然大語言模型&#xff08;LLMs&#xff09;在各種下游任務中展示出了卓越的能力&#xff0c;在多個領域有廣泛應用&#xff0c;但存在著幻覺的問題&#xff1a…

關于C++子類父類成員函數的覆蓋和隱藏

轉載&#xff1a;http://blog.csdn.net/worldmakewayfordream/article/details/46827161 函數的覆蓋 覆蓋發生的條件&#xff1a; &#xff08;1&#xff09; 基類必須是虛函數&#xff08;使用virtual 關鍵字來進行聲明&#xff09; &#xff08;2&#xff09;發生覆蓋的兩個函…

數據結構(四)串的順序存儲

#include <stdio.h> #include <stdlib.h>#define MAXLEN 255 //定長順序存儲 typedef struct {char ch[MAXLEN]; //每個分量存儲一個字符int length; //串的實際長度 }SString;//串的初始化 bool StrAssign(SString& T, char* chars) {int i 0, len;char* …

數據結構(四)串的動態數組存儲

#include <stdio.h> #include <stdlib.h>#define MAXLEN 255 //定長順序存儲 typedef struct {char* ch; //每個分量存儲一個字符int length; //串的實際長度 }SString;//串的初始化 bool StrAssign(SString& T, char* chars) {int i 0, len;T.ch (char*)m…

C++名字隱藏

轉載&#xff1a;http://www.weixueyuan.net/view/6361.html 如果派生類中新增一個成員變量&#xff0c;該成員變量與基類中的成員變量同名&#xff0c;則新增的成員變量就會遮蔽從基類中繼承過來的成員變量。同理&#xff0c;如果派生類中新增的成員函數與基類中的成員函數同…

c語言深入淺出(一)strcpy和memcpy的區別

c語言深入淺出&#xff08;一&#xff09;strcpy和memcpy的區別strcpy和memcpy都是c語言的庫函數 strcpy:只用于字符串的復制&#xff0c;當碰到‘\0’就停止了 memcpy:用于這個內存的拷貝&#xff0c;適用于結構體、字符數組、類等 char * strcpy(char * dest, const char * s…

C++ dynamic_cast操作符

轉載&#xff1a;http://www.weixueyuan.net/view/6377.html 在C中&#xff0c;編譯期的類型轉換有可能會在運行時出現錯誤&#xff0c;特別是涉及到類對象的指針或引用操作時&#xff0c;更容易產生錯誤。Dynamic_cast操作符則可以在運行期對可能產生問題的類型轉換進行測試。…

數據結構(五)樹

數據結構&#xff08;五&#xff09;樹一、基本操作樹是n個節點的有限集&#xff0c;它是一種遞歸的數據結構 一、基本操作 #include <stdio.h> #include <stdlib.h> #include <iostream>#define Elemtype charusing namespace std; typedef struct BiTNod…

C++ typeid操作符

轉載&#xff1a;http://www.weixueyuan.net/view/6378.html typeid操作符用于判斷表達式的類型&#xff0c;注意它和sizeof一樣是一個操作符而不是函數。如果需要使用typeid操作符&#xff0c;最好加上typeinfo頭文件。 給出以下定義 int a;double b;char * c;long d; 下表列…

數據結構(五)層次遍歷

數據結構&#xff08;五&#xff09;層次遍歷// linear_listqueue.cpp : This file contains the main function. Program execution begins and ends there. //#include <iostream> #include <stdlib.h> #include <stdio.h> #define ElemType BiTree using …

C++成員函數指針的應用

轉載&#xff1a;http://www.cppblog.com/colys/archive/2009/08/18/25785.html C中&#xff0c;成員指針是最為復雜的語法結構。但在事件驅動和多線程應用中被廣泛用于調用回叫函數。在多線程應用中&#xff0c;每個線程都通過指向成員函數的指針來調用該函數。在這樣的應用中…

cv2.VideoCapture()無法打開視頻解決方法

cv2.VideoCapture無法打開視頻解決方法問題解決方法問題 cv2.VideoCapture打開mp4文件&#xff0c;直接報錯 解決方法 我們打開D:\opencv_3.4.2_Qt\opencv_3.4.2_Qt\x86\bin\&#xff08;opencv的dll動態庫中找到&#xff09; 找到opencv_ffmpeg342.dll文件&#xff0c;放入…

函數指針指向類的靜態成員函數

轉載&#xff1a;http://www.cnblogs.com/dongyanxia1000/p/4906592.html 1. 代碼 1 #include<iostream>2 #include<stdio.h>3 using namespace std;4 class Point5 {6 public:7 Point(int x0,int y0):x(x),y(y)8 { 9 count; 10 } 11 P…

Qt+OpenCV打開視頻文件并在窗口界面上顯示

QtOpenCV打開視頻文件并在窗口界面上顯示1、新建一個Qt Widgets Application&#xff0c;工程配置文件&#xff08;.pro文件&#xff09;內容如下&#xff1a;#------------------------------------------------- # # Project created by QtCreator 2021-03-19T09:06:07 # #--…

OpenCV Mat的數據類型

OpenCV Mat的數據類型Mattype類型內存拷貝簡單實現Mat Mat類(Matrix的縮寫)是OpenCV用于處理圖像而引入的-一個封裝類。他是一個自動內存管理工具。 Mat:本質上是由兩個數據部分組成的類:(包含信息有矩陣的大小&#xff0c;用于存儲的方法&#xff0c;矩陣存儲的地址等)矩陣頭…