另類堆棧

習題3.14 另類堆棧   (15分)

在棧的順序存儲實現中,另有一種方法是將Top定義爲棧頂的上一個位置。請編寫程序實現這種定義下堆棧的入棧、出棧操作。如何判斷堆棧爲空或者滿?

函數接口定義:

bool Push( Stack S, ElementType X );
ElementType Pop( Stack S );

其中Stack結構定義如下:

typedef int Position;
typedef struct SNode *PtrToSNode;
struct SNode {
    ElementType *Data;  /* 存儲元素的數組 */
    Position Top;       /* 棧頂指針       */
    int MaxSize;        /* 堆棧最大容量   */
};
typedef PtrToSNode Stack;

注意:如果堆棧已滿,Push函數必須輸出“Stack Full”並且返回false;如果隊列是空的,則Pop函數必須輸出“Stack Empty”,並且返回ERROR。

裁判測試程序樣例:

#include <stdio.h>
#include <stdlib.h>

#define ERROR -1
typedef int ElementType;
typedef enum { push, pop, end } Operation;
typedef enum { false, true } bool;
typedef int Position;
typedef struct SNode *PtrToSNode;
struct SNode {
    ElementType *Data;  /* 存儲元素的數組 */
    Position Top;       /* 棧頂指針       */
    int MaxSize;        /* 堆棧最大容量   */
};
typedef PtrToSNode Stack;

Stack CreateStack( int MaxSize )
{
    Stack S = (Stack)malloc(sizeof(struct SNode));
    S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
    S->Top = 0;
    S->MaxSize = MaxSize;
    return S;
}

bool Push( Stack S, ElementType X );
ElementType Pop( Stack S );

Operation GetOp();          /* 裁判實現,細節不表 */
void PrintStack( Stack S ); /* 裁判實現,細節不表 */

int main()
{
    ElementType X;
    Stack S;
    int N, done = 0;

    scanf("%d", &N);
    S = CreateStack(N);
    while ( !done ) {
        switch( GetOp() ) {
        case push: 
            scanf("%d", &X);
            Push(S, X);
            break;
        case pop:
            X = Pop(S);
            if ( X!=ERROR ) printf("%d is out\n", X);
            break;
        case end:
            PrintStack(S);
            done = 1;
            break;
        }
    }
    return 0;
}

/* 你的代碼將被嵌在這裏 */

輸入樣例:

4
Pop
Push 5
Push 4
Push 3
Pop
Pop
Push 2
Push 1
Push 0
Push 10
End

輸出樣例:

Stack Empty
3 is out
4 is out
Stack Full
0 1 2 5

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ERROR -1

typedef int ElementType;
typedef enum {push, pop, end} Operation;
typedef enum {false, true} bool;
typedef int Position;
typedef struct SNode *PtrToSNode;

struct SNode{
	ElementType *Data;
	Position Top;
	int MaxSize;
};

typedef PtrToSNode Stack;

Stack CreateStack(int MaxSize){
	Stack S = (Stack)malloc(sizeof(struct SNode));
	S->Data = (ElementType *)malloc(MaxSize*sizeof(ElementType));
	S->Top=0;
	S->MaxSize = MaxSize;

	return S;
}

bool Push(Stack S, ElementType X);
ElementType Pop(Stack S);

Operation GetOp();

void PrintStack(Stack S);

int main(){
	ElementType X;
	Stack S;
	int N, done=0;

	scanf("%d", &N);
	S = CreateStack(N);

	while(!done){
		switch(GetOp()){
		case push:
			scanf("%d", &X);
			Push(S, X);
			break;
		case pop:
			X = Pop(S);
			if(X!=ERROR)
				printf("%d is out\n", X);
			break;
		case end:
			PrintStack(S);
			done = 1;
			break;
		}
	}
	return 0;
}

Operation GetOp(){  
    char a[5];  
    scanf("%s",a);  
    if(strcmp(a,"Push")==0)   
        return push;  
    else if(strcmp(a,"Pop")==0)   
        return pop;  
    else if(strcmp(a,"End")==0)   
        return end;  
    else  
        return -1;  
}  


bool Push(Stack S, ElementType X){
	
	if(S->Top-1==S->MaxSize-1){
		printf("Stack Full\n");
		return false;
	}
	
	S->Data[S->Top]=X;
	S->Top++;
	return true;
}

ElementType Pop(Stack S){
	ElementType X;

	if(S->Top==0){
		printf("Stack Empty\n");
		return ERROR;
	}
	S->Top--;
	X = S->Data[S->Top];

	return X;
}


void PrintStack(Stack S){
	int i,n;

	n = S->Top-1;
	for(i=n;i>=0;i--)
		printf("%d ", S->Data[i]);
	printf("\n");
}




發佈了94 篇原創文章 · 獲贊 80 · 訪問量 49萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章