PTA複習 習題3 13 雙端佇列

2021-10-12 17:29:22 字數 2689 閱讀 9768

bool

push

( elementtype x, deque d )

;elementtype pop

( deque d )

;bool

inject

( elementtype x, deque d )

;elementtype eject

( deque d )

;

其中deque結構定義如下:

typedef

int position;

typedef

struct qnode *ptrtoqnode;

struct qnode

;typedef ptrtoqnode deque;

注意:pushinject應該在正常執行完操作後返回true,或者在出現非正常情況時返回false。當frontrear相等時隊列為空,popeject必須返回由裁判程式定義的error

#include

#include

#define error -1

typedef

int elementtype;

typedef

enum

operation;

typedef

enum

bool

;typedef

int position;

typedef

struct qnode *ptrtoqnode;

struct qnode

;typedef ptrtoqnode deque;

deque createdeque

(int maxsize )

bool

push

( elementtype x, deque d )

;elementtype pop

( deque d )

;bool

inject

( elementtype x, deque d )

;elementtype eject

( deque d )

;operation getop()

;/* 裁判實現,細節不表 */

void

printdeque

( deque d )

;/* 裁判實現,細節不表 */

intmain()

}return0;

}/* 你的**將被嵌在這裡 */

3

popinject 1

popeject

push 2

push 3

eject

inject 4

inject 5

inject 6

push 7

popend

deque is empty!

1 is out

deque is empty!

2 is out

deque is full!

deque is full!

3 is out

inside deque: 4 5

解題**:
// 此處理解錯了題意 兩個指標應該是都可以前後移動的   push pop 主管front   inject eject  主管rear

//但是還沒有理解這樣做的意義···

bool

push

( elementtype x, deque d )

d->front =

(d->front -

1+ d-

>maxsize)

% d-

>maxsize;

d->data[d-

>front]

= x;

return

true;}

elementtype pop

( deque d )

temp = d-

>data[d-

>front]

; d-

>front =

(d->front +1)

% d-

>maxsize;

return temp;

}bool

inject

( elementtype x, deque d )

d->data[d-

>rear]

= x;

d->rear =

(d->rear +1)

% d-

>maxsize;

return

true;}

elementtype eject

( deque d )

d->rear =

(d->rear -

1+ d-

>maxsize)

% d-

>maxsize;

return d-

>data[d-

>rear]

;}

習題3 13 雙端佇列

bool push elementtype x,deque d 將元素x插入到雙端佇列d的頭 d front d front 1 d maxsize d maxsize d data d front x return true elementtype pop deque d 刪除雙端佇列d的頭元素,...

習題3 13 雙端佇列 25分

bool push elementtype x,deque d elementtype pop deque d bool inject elementtype x,deque d elementtype eject deque d 其中deque結構定義如下 typedef int position...

習題3 13 雙端佇列(迴圈佇列 迴圈陣列)

題目 push x,d 將元素x插入到雙端佇列d的頭 pop d 刪除雙端佇列d的頭元素,並返回 inject x,d 將元素x插入到雙端佇列d的尾部 eject d 刪除雙端佇列d的尾部元素,並返回。函式介面定義 bool push elementtype x,deque d elementtyp...