数据结构:(两种方式)一个数组实现两个栈(共享栈)

奇偶栈!



stack.h

#pragma once

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

#define MAX_SIZE 10

typedef int DataType;
typedef struct stack
{
	DataType _arr[MAX_SIZE];
	int _top1;
	int _top2;
}stack;

void stackinit(stack *s);
void stackpush(stack *s,int data,int which);
void stackpop(stack *s,int which);
int stackempty(stack *s,int which);
void stacksize(stack *s);
void stackprint(stack *s);

stack.c

#include"stack.h"

void stackinit(stack *s)
{
	assert(s);
	s->_top1 = 0;
	s->_top2 = 1;
}
void stackpush(stack *s,int data,int which)
{
	assert(s);
	assert(which ==1||which == 2);

	if (1 == which)
	{
		if (MAX_SIZE - 1 < s->_top1)
		{
			printf("栈1以满!\n");
			return;
		}
		s->_arr[s->_top1] = data;
		s->_top1 += 2;
	}
	else
	{
		if (MAX_SIZE < s->_top2)
		{
			printf("栈2以满!\n");
			return;
	    }
		s->_arr[s->_top2] = data;
		s->_top2 += 2;
	}

}
void stackpop(stack *s,int which)
{
	assert(s);
	assert(which == 1 || which == 2);

	if (1 == which)
	{
		if (0 == s->_top1)
		{
			printf("栈1为空!\n");
			return;
		}
		s->_top1 -= 2;
	}
	else
	{
		if (0 == s->_top2)
		{
			printf("栈2为空!\n");
			return;
		}
		s->_top2 -= 2;
	}
}
int stackempty(stack *s,int which)
{
	assert(s);
	assert(which == 1 || which == 2);
	
	if (0 == s->_top1)
	{
		printf("栈1为空\n");
		return 1;
	}
	if (1 == s->_top2)
	{
		printf("栈2为空\n");
		return 1;
	}
	return 0;
}
void stacksize(stack *s)
{
	assert(s);
	if (stackempty(s, 1))
		return;
	else
	{
		printf("栈1的元素个数为:%d\n",(s->_top1 + 1) / 2);
	}
	if (stackempty(s, 2))
		return;
	else
	{
		printf("栈2的元素个数为:%d\n", s->_top2 / 2);
	}
}
void stackprint(stack *s)
{
	int i = 0;
	assert(s);
	if (stackempty(s, 1))
		return;
	else
	{
		printf("栈1:");
		for (i = 0;i < s->_top1;i += 2)
		{
			printf("%d->",s->_arr[i]);
		}
		printf("\n");
	}
	if (stackempty(s, 2))
		return;
	else
	{
		printf("栈2:");
		for (i = 1; i < s->_top2; i += 2)
		{
			printf("%d->", s->_arr[i]);
		}
		printf("\n");
	}
	for (i = 0; i < s->_top1; i++)
	{
		printf("%d->", s->_arr[i]);
	}
}
test()
{
	stack s;
	stackinit(&s);

	stackpush(&s, 1, 1);
	stackpush(&s, 3, 1);
	stackpush(&s, 5, 1);
	stackpush(&s, 2, 2);
	stackpush(&s, 4, 2);
	stackpush(&s, 6, 2);
	stacksize(&s);
	stackprint(&s);

}

int main()
{
	test();
	system("pause");
	return 0;
}

相向栈!



stack.h

#pragma once

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

#define MAX_SIZE 10

typedef int DataType;
typedef struct stack
{
	DataType _arr[MAX_SIZE];
	int _top1;
	int _top2;
}stack;

void stackinit(stack *s);
void stackpush(stack *s,int data,int which);
void stackpop(stack *s,int which);
int stackempty(stack *s,int which);
void stacksize(stack *s);
void stackprint(stack *s);

stack.c

#include"stack.h"

void stackinit(stack *s)
{
	assert(s);
	s->_top1 = 0;
	s->_top2 = MAX_SIZE - 1;
}
void stackpush(stack *s,int data,int which)
{
	assert(s);
	assert(which ==1||which == 2);

	if (s->_top1 == s->_top2)
	{
			printf("栈1以满!\n");
			return;
	}
	if (1 == which)
	{
		s->_arr[s->_top1] = data;
		s->_top1 += 1;
	}
	else
	{
		s->_arr[s->_top2] = data;
		s->_top2 -= 1;
	}


}
void stackpop(stack *s,int which)
{
	assert(s);
	assert(which == 1 || which == 2);

	if (1 == which)
	{
		if (0 == s->_top1)
		{
			printf("栈1为空!\n");
			return;
		}
		s->_top1 -= 1;
	}
	else
	{
		if (MAX_SIZE - 1 == s->_top2)
		{
			printf("栈2为空!\n");
			return;
		}
		s->_top2 += 1;
	}
}
int stackempty(stack *s,int which)
{
	assert(s);
	assert(which == 1 || which == 2);
	
	if (0 == s->_top1)
	{
		printf("栈1为空\n");
		return 1;
	}
	if (MAX_SIZE - 1 == s->_top2)
	{
		printf("栈2为空\n");
		return 1;
	}
	return 0;
}
void stacksize(stack *s)
{
	assert(s);
	if (stackempty(s, 1))
		return;
	else
	{
		printf("栈1的元素个数为:%d\n",s->_top1 );
	}
	if (stackempty(s, 2))
		return;
	else
	{
		printf("栈2的元素个数为:%d\n",MAX_SIZE - 1 - s->_top2 );
	}
}
void stackprint(stack *s)
{
	int i = 0;
	assert(s);
	if (stackempty(s, 1))
		return;
	else
	{
		printf("栈1:");
		for (i = 0;i < s->_top1;i++)
		{
			printf("%d->",s->_arr[i]);
		}
		printf("\n");
	}
	if (stackempty(s, 2))
		return;
	else
	{
		printf("栈2:");
		for (i = MAX_SIZE - 1; i > s->_top2; i--)
		{
			printf("%d->", s->_arr[i]);
		}
		printf("\n");
	}
}
test()
{
	stack s;
	stackinit(&s);

	stackpush(&s, 1, 1);
	stackpush(&s, 3, 1);
	stackpush(&s, 5, 1);
	stackpush(&s, 2, 2);
	stackpush(&s, 4, 2);
	stackpush(&s, 6, 2);
	stacksize(&s);
	stackprint(&s);

}

int main()
{
	test();
	system("pause");
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值