實訓C 語言設計 Stack 棧運算

2021-04-15 06:21:40 字數 1778 閱讀 8167

#include

#include

using namespace std;

//---------------抽象資料型別順序棧的定義------------------//

#define stack_init_size 100

#define stack_increment 10

typedef char selemtype,qelemtype;

typedef struct sqstack;

//---------------------順序棧的介面函式宣告-----------------------//

//構造乙個空棧

bool initstack(sqstack &s);

//銷毀棧,釋放棧所佔記憶體空間

void destroystack(sqstack &s);

//把s置為空棧

bool clearstack(sqstack &s);

//若棧s為空棧,返回true,否則返回false

bool stackempty(sqstack &s);

//返回棧s的元素個數

int stacklength(sqstack &s);

//若棧不空,則用e返回s的棧頂元素,並返回ok;否則返回error

bool gettop(sqstack &s,selemtype &e);

//插入新元素e為新的棧頂元素

bool push(sqstack &s,selemtype e);

//若棧不空,刪除s的棧頂元素,並用e返回其值,並返回true,否則返回false

bool pop(sqstack &s,selemtype& e);

//-----------介面函式的實現部分-------------------//

bool initstack(sqstack &s)

//initstack

void destroystack(sqstack &s)

//destroystack

bool clearstack(sqstack &s)

//clearstack

bool stackempty(sqstack &s)

//stackempty

int stacklength(sqstack &s)

//stacklength

bool gettop(sqstack &s, selemtype &e)

//gettop

bool push(sqstack &s, selemtype e)

*s.top++ = e;

return true;

}//push

bool pop(sqstack &s,selemtype& e)

//pop

// sqstack.cpp : 定義控制台應用程式的入口點。

//#include "stdafx.h"

#include "sqstack.h"

int _tmain(int argc, _tchar* argv)

;cout << str << endl;        //print the string

int i = 0;

while (str[i])

//無需對棧滿進行判斷,sqstack在棧滿時

//會自動增加棧空間

push(s, str[i++]);

char c;

while (!stackempty(s))

return 0;

}

實訓C 語言設計 異常處理

目的 正確理解c 的異常處理機制 學習異常處理的定義及執行過程 內容 定義乙個異常類cexception 有成員函式reason 用來顯示異常的型別,在子函式中觸發異常,在主程式中處理異常,觀察程式的執行流程。三 實驗任務 在類cexception的成員函式reason 中用cout顯示異常的型別,...

實訓C 語言設計 檔案和流

熟悉流類庫中常用的類及其成員函式的用法,學習到了標準的輸入輸出及其格式的控制,學習到了檔案的應用方式,有二進位制檔案和文字檔案.演算法 無 方法 無 程式 lab11 1.cpp include using namespace std define d a t ad t fill char d t....

實訓C 語言設計 檔案和流

熟悉流類庫中常用的類及其成員函式的用法,學習到了標準的輸入輸出及其格式的控制,學習到了檔案的應用方式,有二進位制檔案和文字檔案.演算法 無 方法 無 程式 lab11 1.cpp include using namespace std define d a t ad t fill char d t....