6 7 在乙個陣列中實現兩個堆疊 20分

2021-10-02 04:56:50 字數 1736 閱讀 7583

本題要求在乙個陣列中實現兩個堆疊。

函式介面定義:

stack createstack( int maxsize );

bool push( stack s, elementtype x, int tag );

elementtype pop( stack s, int tag );

其中tag是堆疊編號,取1或2;maxsize堆疊陣列的規模;stack結構定義如下:

typedef int position;

struct snode ;

typedef struct snode *stack;

注意:如果堆疊已滿,push函式必須輸出「stack full」並且返回false;如果某堆疊是空的,則pop函式必須輸出「stack tag empty」(其中tag是該堆疊的編號),並且返回error。

裁判測試程式樣例:

#include

#include

#define error 1e8

typedef int elementtype;

typedef enum operation;

typedef enum bool;

typedef int position;

struct snode ;

typedef struct snode *stack;

stack createstack( int maxsize );

bool push( stack s, elementtype x, int tag );

elementtype pop( stack s, int tag );

operation getop(); /* details omitted /

void printstack( stack s, int tag ); / details omitted */

int main()

}return 0;/* 你的**將被嵌在這裡 */

輸入樣例:

5push 1 1

pop 2

push 2 11

push 1 2

push 2 12

pop 1

push 2 13

push 2 14

push 1 3

pop 2

end輸出樣例:

stack 2 empty

stack 2 is empty!

stack full

stack 1 is full!

pop from stack 1: 1

pop from stack 2: 13 12 11

思路就是兩個棧從陣列的中間向兩頭生長。當兩個棧頂指標相遇,就是棧滿。

stack createstack

(int maxsize)

bool

isfull

(stack s)

bool

push

(stack s, elementtype x,

int tag)

else

else

if(tag==2)

return

true;}

}elementtype pop

(stack s,

int tag)

else

}else

if(tag ==2)

else

}}

6 7 在乙個陣列中實現兩個堆疊

6 7 在乙個陣列中實現兩個堆疊 20 分 本題要求在乙個陣列中實現兩個堆疊。stack createstack int maxsize bool push stack s,elementtype x,int tag elementtype pop stack s,int tag 其中tag是堆疊編...

6 7 在乙個陣列中實現兩個堆疊

本題要求在乙個陣列中實現兩個堆疊。stack createstack int maxsize bool push stack s,elementtype x,int tag elementtype pop stack s,int tag 其中tag是堆疊編號,取1或2 maxsize堆疊陣列的規模 ...

6 7 在乙個陣列中實現兩個堆疊 20分

本題要求在乙個陣列中實現兩個堆疊。函式介面定義 stack createstack int maxsize bool push stack s,elementtype x,int tag elementtype pop stack s,int tag 其中tag是堆疊編號,取1或2 maxsize堆...