資料結構 實驗四 c 實現 20 11 25

2021-10-10 19:59:53 字數 2708 閱讀 7184

利用順序表建立乙個棧。分別編寫入棧函式和出棧函式,對鍵盤輸入的,數量未知的整數進行出棧和入棧的操作。每次呼叫入棧函式或出棧函式時,實現乙個整數的入棧或出棧;出棧的整數需要列印出來。

利用鍊錶建立乙個棧。分別編寫入棧函式和出棧函式,對鍵盤輸入的,數量未知的整數進行出棧和入棧的操作。每次呼叫入棧函式或出棧函式時,實現乙個整數的入棧或出棧;出棧的整數需要列印出來。

利用順序棧或鏈式棧求解表示式(a+b)*(c-d)的值(可手工把中綴表示式轉換為字尾表示式)。其中的運算元a,b,c,d為0到9之間的整數。

/*linkedstack.h*/

#include

#define nodeposition node*

using

namespace std;

template

<

typename t>

struct node};

template

<

typename t>

class

linkedstack

~linkedstack()

void

push

(t d)

p->next = top;

top=p;

} t pop()

nodeposition p = top;

t d = top-

>data;

top = top-

>next;

delete p;

return d;

} t top()

bool

isempty()

};/*seqstack.h*/

#include

#define maxsize 100

using

namespace std;

template

<

typename t>

class

seqstack

~seqstack()

void

push

(t d)

data[

++top]

=d;}

t pop()

return data[top--];

} t top()

bool

isempty()

else}}

;#include

#include

#include

"seqstack.h"

#include

"linkedstack.h"

using

namespace std;

int priojud (

char a )

// 優先順序判斷

else

if( a ==

'+'|| a ==

'-')

else

if( a ==

'(')

}string infixtorpn ( string str1 )

//中綴表示式轉化為字尾表示式

else

if( s.

isempty()

)else

if( str1[i]

==')'

)

s.pop();

}else

if( str1[i]

=='('

)else

if( priojud ( str1[i]

)<= priojud ( s.

top())

)

s.push ( str1[i]);

}else

if( priojud ( str1[i]

)> priojud ( s.

top())

);}while

(!s.

isempty()

)return str2;

}int cout (

char c,

char a,

char b )

else

if( c ==

'-')

else

if( c ==

'*')

else

if( c ==

'/')

}int coutbyrpn ( string str )

//由字尾表示式計算

}return s.

top()-

'0';

}int

main()

}

cout <<

"*********************linkedstack******************"

<< endl;

flag =1;

while

( flag )

}

cout <<

"*****************caculate*************"

<< endl;

cin >> str;

cout << infixtorpn ( str )

<< endl;

cout << coutbyrpn ( infixtorpn ( str ));

return0;

}

資料結構實驗四

一 實驗目的 鞏固字串和多維陣列相關知識,學會運用靈活應用。1.回顧字串和多維陣列的邏輯結構和儲存操作特點,字元和陣列的物理儲存結構和常見操作。2.學習運用字串和和陣列的知識來解決實際問題。3.進一步鞏固程式除錯方法。4.進一步鞏固模板程式設計。二 實驗內容 1.從鍵盤輸入一些文字,統計文字單詞的個...

資料結構實驗 單鏈表 C 實現

下面是源 include define ok 1 define error 0 define elems 10 define link x static cast x using namespace std typedef struct lnode link,position typedef int...

資料結構 實驗二 c 實現 2020 11 12

任務1 編寫鍊錶的建立函式,列印函式和釋放函式。建立函式把鍵盤輸入的整數存入鍊錶,其中最新加入的數在鍊錶的頭部。建立函式每次將乙個資料存入鍊錶,如果需要存多個資料,那麼需要多次呼叫建立函式。列印函式則將鍊錶中的元素,按照從頭到尾的順序全部輸出,需要考慮鍊錶為空的情況。釋放函式主要用來將鍊錶占用的記憶...