PTA Data structure and Algorithm topic set (Chinese) 4-7 implement two stacks in an array (20 points)

Source: Internet
Author: User

The subject requires two stacks to be implemented in an array. function Interface Definition:

Stack createstack (int MaxSize);
BOOL Push (Stack S, ElementType X, int Tag);
ElementType Pop (Stack S, int Tag);

Where tag is the stack number, take the size of a 1 or 2;maxsize stack array, and the stack structure is defined as follows:

typedef int Position;
struct Snode {
    ElementType *data;
    Position Top1, TOP2;
    int MaxSize;
};
typedef struct SNODE *stack;

Note: If the stack is full, the push function must output "stack" and return false; If a stack is empty, the pop function must output "stack Tag Empty" (where Tag is the number of the stack) and return the error. Sample Referee Test Procedure:

#include <stdio.h> #include <stdlib.h> #define ERROR 1e8 typedef int ElementType;
typedef enum {Push, pop, end} Operation;
typedef enum {false, true} bool;
typedef int Position;
    struct Snode {ElementType *data;
    Position Top1, TOP2;
int MaxSize;
};

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 () {int N, Tag, X;
    Stack S;

    int done = 0;
    scanf ("%d", &n);
    S = Createstack (N);
            while (!done) {switch (Getop ()) {case push:scanf ("%d%d", &tag, &x); if (!
            Push (S, X, tag) printf ("Stack%d is full!\n", tag);
        Break
            Case pop:scanf ("%d", &tag);
            X = Pop (S, Tag);
       if (x==error) printf ("Stack%d is empty!\n", Tag);     Break
            Case End:printstack (S, 1);
            Printstack (S, 2);
            Done = 1;
        Break
} return 0;
 }/* Your code will be embedded here * *
Input Sample:
5
push 1 1
Pop 2
Push 2 push 1
2
push 2
Pop 1
push 2
push 2
push 1 3
P OP 2 End

Output Sample:
Stack 2 Empty
stack 2 is empty!
Stack full
stack 1 is full!
Pop from stack 1:1
pops from stack 2:13 12 11

Program code:

Stack createstack (int MaxSize)
{
	stack sq= (struct snode*) malloc (sizeof (struct snode));
	Sq->data= (ElementType *) malloc (sizeof (ElementType) *maxsize);
	sq->maxsize=maxsize;
	sq->top1=-1;
	sq->top2=maxsize;
	return sq;
}

BOOL Push (Stack S, ElementType X, int Tag)
{
	if (S->TOP1+1==S->TOP2)
	{
		printf ("Stack full\n") ;
		return false;
	}
	if (tag==1)
	{
		s->top1++;
		s->data[s->top1]=x;
	}
	else
	{
		s->top2--;
		s->data[s->top2]=x;
	}
	return true;
}

ElementType Pop (Stack S, int Tag)
{
	if (tag==1)
	{
		if (s->top1==-1)
		{
			printf ("Stack 1 Empty\n ");
			return ERROR;
		}
		else
		{
			return s->data[s->top1--];//first returns S->DATA[S->TOP1] then s->top1 the self minus 
		}
	}
	else
	{
		if (s->top2==s->maxsize)
		{
			printf ("Stack 2 empty\n");
			return ERROR;
		}
		else
		{
			return s->data[s->top2++];//first returns S->DATA[S->TOP2] then S->top2 from} 
		}
	}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.