多栈共享操作

/*多栈共享操作*/
#include<stdio.h>
#define stacksize 50
#define TRUE 1
#define FALSE 0

typedef struct{
	char elem[stacksize];
	int top[2];
}doublesequencestack;

/*双端顺序栈的初始化*/
void initial(doublesequencestack * S)
{
	S->top[0]=-1;//构造一个双端顺序空栈
	S->top[1]=stacksize;
	printf("initial double-sequence-stack success!\n");
	fflush(stdout);
}

/*双端顺序栈进栈操作*/
int doublesequencepushstack(doublesequencestack *S,int i)
{
	char c;
	int flag=1;
	if(S->top[0]==S->top[1])
	{
		printf("The stack is full !");
		fflush(stdout);
		return FALSE;
	}
	else
	{
		printf("Please enter the stack[%d] number you want to push elements !",i);
		fflush(stdout);
		switch(i)
		{
		case 1:

		   while(flag)
		    {

		     c=getchar();
		     if(c!='$')
		      {
			  S->top[0]++;
			  S->elem[S->top[0]]=c;
		      }
		      else
		      {

		    flag=0;
		    printf("Stack success !\n");
		    fflush(stdout);

		      }
		    }break;

		case 2:
			while(flag)
					{

					c=getchar();
					if(c!='$')
					{
						S->top[1]--;
						S->elem[S->top[1]]=c;
					}
					else
					{

					flag=0;
					printf("Stack success !\n");
					fflush(stdout);

					}
					}break;
		}
	}
	return TRUE;
}
 /*双端顺序栈弹出栈顶元素*/
int doublesequencepopstack(doublesequencestack *S,int i)
{
	switch(i)
	{
	case 1:
	if(S->top[0]==-1)
	{
		printf("stack empty!\n");
	    fflush(stdout);
	}
	else
	{
		printf("The value of the stack top element is %c\n",S->elem[S->top[0]]);
		fflush(stdout);
		S->top[0]--;
	}break;
	case 2:
		if(S->top[1]==stacksize)
		{
				printf("stack empty!\n");
			    fflush(stdout);
			}
			else
			{
				printf("The value of the stack top element is %c\n",S->elem[S->top[1]]);
				fflush(stdout);
				S->top[1]++;
			}break;
	}
	return TRUE;
}
/*双端顺序栈所有元素出栈*/
int popallelement(doublesequencestack *S,int i)
{
	switch(i)
	{

	case 1:
	if(S->top[0]==-1)
		{
			printf("\n stack empty!\n");
		    fflush(stdout);
		    return TRUE;
		}
	else
	{
	    printf("%2c",S->elem[S->top[0]]);
	    fflush(stdout);
		S->top[0]--;
		popallelement(S,i);
	}break;
	case 2:
		if(S->top[1]==stacksize)
				{
					printf("\n stack empty!\n");
				    fflush(stdout);
				    return TRUE;
				}
			else
			{
			    printf("%2c",S->elem[S->top[1]]);
			    fflush(stdout);
				S->top[1]++;
				popallelement(S,i);
			}break;
	}
	return FALSE;
}


int main(void)
{

	doublesequencestack *S;
	/*形参的星号是指指针,就是结构体类型的指针,函数形参是一个结构体指针,
	 * 实参也需要一个该结构体类型的指针,也就是要实参是一个地址传给形参
	 * 故在函数调用是使用的是&符号,就是区地址,将结构体变量的地址传过去
	 * 这样在函数里对于形参的修改就是对实参结构体变量的修改。*/
	initial(&S);//初始化
	printf("Please input the element to enter the stack[1] and end with $ \n");
	fflush(stdout);
	doublesequencepushstack(&S,1);//左端入栈
	printf("Please input the element to enter the stack[2] and end with $ \n");
	fflush(stdout);
	doublesequencepushstack(&S,2);//右端入栈
	doublesequencepopstack(&S,1);//左端栈顶元素出
	doublesequencepopstack(&S,2);//右端栈顶元素出
	printf("The out-of-stack[1] elements are as follows...  \n");
	fflush(stdout);
	popallelement(&S,1);//左端元素全部出栈
	printf("The out-of-stack[2] elements are as follows...  \n");
	fflush(stdout);
	popallelement(&S,2);//右端元素全部出栈
	return 0;
}

调试结果

initial double-sequence-stack success!
Please input the element to enter the stack[1] and end with $ 
Please enter the stack[1] number you want to push elements !123456789$
Stack success !
Please input the element to enter the stack[2] and end with $ 
Please enter the stack[2] number you want to push elements !qawsedrftgyh$
Stack success !
The value of the stack top element is 9
The value of the stack top element is h
The out-of-stack[1] elements are as follows...  
 8 7 6 5 4 3 2 1
 stack empty!
The out-of-stack[2] elements are as follows...  
 y g t f r d e s w a q 
 stack empty!

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值