算法导论学习10.1 两个栈共享一个数组 and 两个栈实现一个队列 and 两个队列实现一个栈


ArrayStack.h (顺序栈的基本操作)

#include 
#include 

#define MAXNUM 4
#define OVERFLOW -1
#define UNDERFLOW -2

typedef int elemtype;

//顺序栈
typedef struct ArrayStack
{
	elemtype data[MAXNUM];
	int top;
}ArrayStack;

//栈的置空
void InitStack(ArrayStack *s)
{
	s->top = -1;
}

//判栈空
int IsEmpty(ArrayStack *s)
{
	return (-1 == s->top);
}

//判栈满
int IsFull(ArrayStack *s)
{
	return (MAXNUM - 1 == s->top);
}

//进栈
void Push(ArrayStack *s, elemtype x)
{
	if (IsFull(s))
	{
		printf("Stack overflow!\n");
		exit(OVERFLOW);
	}
	else
	{
		s->top += 1;
		s->data[s->top] = x;
	}
}

//出栈
elemtype Pop(ArrayStack *s)
{
	if (IsEmpty(s))
	{
		printf("Stack underflow!");
		exit(UNDERFLOW);
	}
	else
	{
		return (s->data[s->top--]);
	}
}

//取栈顶
elemtype Top(ArrayStack *s)
{
	if (IsEmpty(s))
	{
		printf("Stack is Empty!");
		return NULL;
	}
	else
	{
		return (s->data[s->top]);
	}
}

//栈的长度
int Length(ArrayStack *s)
{
	return (s->top + 1);
}

//栈的遍历
void StackTraverse(ArrayStack *s)
{
	int i = 0;
	if (IsEmpty(s))
	{
		printf("Stack is Empty!\n");
	}
	else
	{
		for (i = 0; i < Length(s); i++)
		{
			printf("%d ", s->data[i]);
		}
		printf("\n");
	}
}


ArrayQueue.h (顺序队列的基本操作)
#include 
#include 

#define MAXNUM 3
#define OVERFLOW -1
#define UNDERFLOW -2

typedef int elemtype;

//顺序队列
typedef struct ArrayQueue
{
	elemtype data[MAXNUM + 1];
	int front;
	int tail;
}ArrayQueue;

//队列的初始化
void InitQueue(ArrayQueue *q)
{
	q->front = 0;
	q->tail = 0;
}

//判队满
int IsFull(ArrayQueue *q)
{
	return ( (q->tail + 1) % (MAXNUM + 1) == q->front );//应该跟MAXNUM + 1求余
}

//判队空
int IsEmpty(ArrayQueue *q)
{
	return (q->tail == q->front);
}

//入队
void EnQueue(ArrayQueue *q, elemtype x)
{
	if (IsFull(q))
	{
		printf("Queue overflow!");
		exit(OVERFLOW);
	}
	else
	{
		q->tail = (q->tail + 1) % (MAXNUM + 1);
		q->data[q->tail] = x;
	} 
}

//出队
elemtype DeQueue(ArrayQueue *q)
{
	if (IsEmpty(q))
	{
		printf("Queue underflow!");
		exit(UNDERFLOW);
	}
	else
	{
		q->front = (q->front + 1) % (MAXNUM + 1);//q->data[q->front]不属于队列
		return (q->data[q->front]);
	}
}

//取队头
elemtype Front(ArrayQueue *q)
{
	if (IsEmpty(q))
	{
		printf("ArrayQueue is Empty!");
		return NULL;
	}
	else
	{
		return q->data[q->front + 1];
	}
}

//队的长度
int Length(ArrayQueue *q)
{
	return (q->tail - q->front + MAXNUM) % MAXNUM;
}


1.  用一个数组A[1..n]来实现两个栈,使得两个栈中的元素总数不到n时, 两者都不会发生上溢.

     思路:将数组的两端分别对应两个栈的栈底,每个栈的压栈操作都向着另一个栈的栈底进行.


1Arr2Stack.c

#include 
#include 

#define MAXNUM 4
#define OVERFLOW -1
#define UNDERFLOW -2
#define FLAGERROR -3

typedef int elemtype;

typedef struct stack
{
	int top;
	int flag;
}stack;

typedef struct ShareArrStack
{
	elemtype data[MAXNUM];
	stack stack1;
	stack stack2;
}ShareArrStack;

//初始化
void InitStack(ShareArrStack *s)
{
	(s->stack1).top = -1;
	(s->stack1).flag = 1;
	(s->stack2).top = MAXNUM;
	(s->stack2).flag = 2;
}

//判栈满
int IsFull(ShareArrStack *s)
{
	return (s->stack1.top + 1 == s->stack2.top);
}

//判栈空
int IsEmpty(stack *sn)
{
	return ((sn->top == -1) || (sn->top == MAXNUM));
}


//Push
void Push(ShareArrStack *s, stack sn, elemtype x)
{
	if (IsFull(s))
	{
		printf("Stack overflow!");
		exit(OVERFLOW);
	}
	else
	{
		switch (sn.flag)
		{
		case 1:
			{
				s->stack1.top += 1;
				s->data[s->stack1.top] = x;
				break;
			}
		case 2:
			{
				s->stack2.top -= 1;
				s->data[s->stack2.top] = x;
				break;
			}
		default:
			{
				exit(FLAGERROR);
			}
		}
	}
}

//Pop
elemtype Pop(ShareArrStack *s, stack sn)
{
	if (IsEmpty(&sn))
	{
		printf("stack underflow!");
		exit(UNDERFLOW);
	}
	else
	{
		switch(sn.flag)
		{
		case 1:
			{
				return (s->data[s->stack1.top --]);
				break;
			}
		case 2:
			{
				return (s->data[s->stack2.top ++]);
				break;
			}
		default:
			{
				exit(FLAGERROR);
			}
		}
	}
}

int main()
{
	ShareArrStack *s = NULL;
	s = (ShareArrStack *)malloc(sizeof(ShareArrStack));
	InitStack(s);
	Push(s, s->stack1, 1);
	Push(s, s->stack2, 9);
	Push(s, s->stack1, 2);
	Push(s, s->stack2, 8);
	printf("%d, ", Pop(s, s->stack2));
	printf("%d", Pop(s, s->stack1));

	return 0;
}

2. 用两个栈来实现一个队列

    思路:http://www.cnblogs.com/wanghui9072229/archive/2011/11/22/2259391.html


Stack2Queue.c

#include "ArrayStack.h"

//此处假设两个栈的大小是相同的,不等又会怎样?
typedef struct Stack2Queue
{
	ArrayStack *s1;
	ArrayStack *s2;
}Stack2Queue;

//队列置空
void thisInitQueue(Stack2Queue *q)
{
	q->s1 = (ArrayStack *)malloc(sizeof(ArrayStack));
	InitStack(q->s1);
	q->s2 = (ArrayStack *)malloc(sizeof(ArrayStack));
	InitStack(q->s2);	
}

//判队空
int thisIsEmpty(Stack2Queue *q)
{
	return( IsEmpty(q->s1) && IsEmpty(q->s2) );
}

//判队满
int thisIsFull(Stack2Queue *q)
{
	return ( IsFull(q->s1) || IsFull(q->s2) );
}

//进队
void thisEnQueue(Stack2Queue *q, elemtype x)
{
	if (thisIsFull(q))
	{
		printf("Queue overflow!");
		exit(OVERFLOW);
	}
	else if (!IsEmpty(q->s2))
	{
		while(!IsEmpty(q->s2))
		{
			Push(q->s1, Pop(q->s2));
		}
	}	
	Push(q->s1, x);	
}

//出队
elemtype thisDeQueue(Stack2Queue *q)
{
	if (thisIsEmpty(q))
	{
		printf("Queue underflow!");
		exit(UNDERFLOW);
	}
	else if (IsEmpty(q->s2))
	{
		while(Length(q->s1) != 1)
		{
			Push(q->s2, Pop(q->s1));
		}
		return(Pop(q->s1));
	}
	else
	{
		return(Pop(q->s2));
	}		
}

int main()
{
	Stack2Queue *q = NULL;
	q = (Stack2Queue *)malloc(sizeof(Stack2Queue));
	thisInitQueue(q);
	thisEnQueue(q, 1);
	thisEnQueue(q, 2);
	thisEnQueue(q, 3);
	thisEnQueue(q, 4);
	thisDeQueue(q);
	thisDeQueue(q);
	thisEnQueue(q, 5);
	thisDeQueue(q);

	return 0;
}

3. 用两个队列来实现一个栈

    思路:栈的push操作:入队;

                栈的pop操作:将队列A中除了队尾外的数据 依次出队, 并入队到队列B中。最后队列A只剩下队头,将对头出队,就是pop操作。


Queue2Stack.c

#include "ArrayQueue.h"

//两个队列组成一个栈
typedef struct Queue2Stack
{
	ArrayQueue *q1;
	ArrayQueue *q2;

}Queue2Stack;

//栈置空
void thisInitStack(Queue2Stack *s)
{
	s->q1 = (ArrayQueue *)malloc(sizeof(ArrayQueue));
	InitQueue(s->q1);
	s->q2 = (ArrayQueue *)malloc(sizeof(ArrayQueue));
	InitQueue(s->q2);
}

//判栈空
int thisIsEmpty(Queue2Stack *s)
{
	return ( IsEmpty(s->q1) && IsEmpty(s->q2));
}

//判栈满
int thisIsFull(Queue2Stack *s)
{
	return ( IsFull(s->q1) || IsFull(s->q2));
}

//进栈
void thisPush(Queue2Stack *s, elemtype x)
{
	if ( thisIsFull(s) )
	{
		printf("stack overflow!");
		exit(OVERFLOW);
	}
	else if(!IsEmpty(s->q2))
	{
		EnQueue(s->q2, x);
	}
	else
	{
		EnQueue(s->q1, x);
	}
}

//出栈
elemtype thisPop(Queue2Stack *s)
{
	if (thisIsEmpty(s))
	{
		printf("stack underflow!");
		exit(UNDERFLOW);
	}
	else if (!IsEmpty(s->q2))
	{
		while ( Length(s->q2) != 1 )
		{
			EnQueue( s->q1, DeQueue(s->q2) );
		}
		return (DeQueue(s->q2));
	}
	else
	{
		while (Length(s->q1) != 1)
		{
			EnQueue(s->q2, DeQueue(s->q1));
		}
		return (DeQueue(s->q1));
	}
}

int main()
{
	Queue2Stack *s = NULL;
	s = (Queue2Stack *)malloc(sizeof(Queue2Stack));

	thisInitStack(s);
	thisPush(s, 1);
	thisPush(s, 2);
	thisPush(s, 3);
	thisPop(s);
	thisPush(s, 4);

	return 0;
}





你可能感兴趣的:(c,算法导论,算法,struct,null)