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

奇偶栈!



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;
}


猜你喜欢

转载自blog.csdn.net/w_j_f_/article/details/80362910