栈和队列面试题

Stack.h

#pragma once
#include <malloc.h>
#include <assert.h>
#include<stdio.h>
#include <stdlib.h>

typedef int STDataType;
#define DEFAULT_SZ 5

typedef struct STNode
{
	STDataType * data;
	struct Node* next;

}NODE, *PNODE;
typedef struct Stack
{
	STDataType * data;
	PNODE _top;		// 栈顶
	PNODE _end;    //栈底
	int _capacity;  // //栈的总大小 
	int _size;//当前栈的大小

}Stack;

void StackInit(Stack* ps);
void StackDestory(Stack* ps);

void StackPush(Stack* ps, STDataType x);
void StackPop(Stack* ps);
STDataType StackTop(Stack* ps);
int StackEmpty(Stack* ps);
int StackSize(Stack* ps);
void StackPrint(Stack *ps);

void TestStack();

Queue.h

#pragma once

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



typedef int QUDataType;
#define DEFAULT_SZ 5
typedef struct QueueNode
{
	struct QueueNode* _next;
	QUDataType _data;
}QueueNode, *pQueueNode;

typedef struct QUQueue
{
	QueueNode* _front; // 队头
	QueueNode* _back;  // 队尾
	int _size; //队列当前长度
	int _capacity; //队列总长度
	QUDataType _data;
}Queue, *pQueue;

void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);

QueueNode* BuyQueueNode(QUDataType );
void QueuePush(Queue* pq,QUDataType x);
void QueuePop(Queue* pq);
QUDataType QueueFront(Queue* pq);
QUDataType QueueBack(Queue* pq);
int QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);

void TestQueue();

Topic.h

#pragma once

#include "Queue.h"
#include "Stack.h"


////////////////////////////////////////////////////////////////
// 1.两个栈实现一个队列
typedef struct QueueByTwoStack
{
	Stack s1;
	Stack s2;
}QueueByTwoStack;

void QueueByTwoStackInit(QueueByTwoStack* qts);
void QueueByTwoStackDestory(QueueByTwoStack* qts);

void QueueByTwoStackPush(QueueByTwoStack* qts, STDataType x);
void QueueByTwoStackPop(QueueByTwoStack* qts);
STDataType QueueByTwoStackFront(QueueByTwoStack* qts);

int QueueByTwoStackSize(QueueByTwoStack* qts);
int QueueByTwoStackEmpty(QueueByTwoStack* qts);

void TestQueueByTwoStack();

////////////////////////////////////////////////
// 2.两个队列实现一个栈

typedef struct StackByTwoQueue
{
	Queue q1;
	Queue q2;
}StackByTwoQueue;


void StackByTwoQueueInit(StackByTwoQueue* stq);
void StackByTwoQueueDestory(StackByTwoQueue* stq);
QUDataType StackByTwoQueueTop(StackByTwoQueue* stq);
int StackByTwoQueueEmpty(StackByTwoQueue* stq);
int StackByTwoQueueSize(StackByTwoQueue* stq);

void StackByTwoQueuePush(StackByTwoQueue* stq, QUDataType x);
void StackByTwoQueuePop(StackByTwoQueue* stq);

void TestStackByTwoQueue();

//////////////////////////////////////////////////////////////
// Min Stack

typedef int MSTDataType;

typedef struct MinStack
{
	Stack _st;
	Stack _minst;
}MinStack;

void MinStackInit(MinStack* pms);
void MinStackDestory(MinStack* pms);

void MinStackPush(MinStack* pms, MSTDataType x);
void MinStackPop(MinStack* pms);
MSTDataType MinStackMin(MinStack* pms);
void TestMinStack();

void TestIsLegalStackOrder();
//////////////////////////////////////////////////////
#define N 100
typedef int SSDataType;
typedef struct ShareStack
{
	SSDataType _a[N];
	int _top1;
	int _top2;
	int max_size;
}ShareStack;

void ShareStackInit(ShareStack* pss);
void ShareStackInit_01(ShareStack* pss);
void ShareStackPush1(ShareStack* pss, SSDataType x);//左边入栈
void ShareStackPush2(ShareStack* pss, SSDataType x);//右边入栈

// which 1、2
void ShareStackPush(ShareStack* pss, SSDataType x, int which);
void ShareStackPop(ShareStack* pss, int which);
SSDataType ShareStackTop(ShareStack* pss, int which);
void TestShareStack();

Stack.c


#include"Stack.h"



void StackInit(Stack* ps)
{
	assert(ps);
	ps->data = (STDataType *)malloc(sizeof(STDataType)* DEFAULT_SZ);
	ps->_capacity = DEFAULT_SZ;
	ps->_top = ps->_end;
	ps->_size = 0;

}
void StackDestory(Stack* ps)//销毁栈
{
	assert(ps);
	int i, len;
	len = ps->_size;
	if (ps->_top == ps->_end)
	{
		printf("栈为空,无法销毁\n");
		return;
	}
	else
	{
		for (i = 0; i < len; i++)
		{
			free(ps->_end);
			ps->_end++;
		}
		ps->_size = 0;
		ps->_capacity = 0;
		ps->_top = ps->_end = NULL;
	}
}
void StackPush(Stack* ps,STDataType x)
{
	PNODE newNode = (PNODE)malloc(sizeof(NODE));
	newNode->data = x;
	newNode->next = ps->_top;
	ps->_top = newNode;
}
void StackPop(Stack* ps)
{
	PNODE tmp = (PNODE)malloc(sizeof(NODE));
	assert(ps != NULL&&ps->_top != NULL&&ps->_end != NULL);
	tmp = ps->_top->next;
	free(ps->_top);
	ps->_top = NULL;
	ps->_top = tmp;
	ps->_size--;
}
STDataType StackTop(Stack* ps)
{
	assert(ps != NULL);
	return ps->_top - 1;
}
int StackEmpty(Stack* ps)
{
	assert(ps != NULL);
	if (ps->_top == ps->_end)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
int StackSize(Stack* ps)
{
	assert(ps != NULL);
	while (ps)
	{
		ps->_top++;
		ps->_size++;
	}
	return ps->_size;
	//return ps->_top;
}

void StackPrint(Stack *ps)
{
	PNODE tmp = ps->_top;
	while (tmp != ps->_end)
	{
		printf("%d", tmp->data);
		tmp = tmp->next;
	}
	printf("\n");
}

Queue.c

#include"Queue.h"
void QueueInit(Queue* pq)
{
	assert(pq != NULL);
	pq->_front = pq->_back = (QUDataType*)malloc(sizeof(QUDataType)*DEFAULT_SZ);
	if (!pq->_front)
	{
		exit(0);
	}
	pq->_front->_next = NULL;
	pq->_size = 0;
	pq->_capacity = DEFAULT_SZ;
}
void QueueDestory(Queue* pq)
{
	assert(pq != NULL);
	if (pq == NULL)
	{
		printf("队列为空,无法销毁\n");
	}
	pQueueNode node = pq->_front;
	while (node)
	{
		pQueueNode cur = node->_next;
		free(node);
		node = cur;
	}
	pq->_front = pq->_back = NULL;
}

QueueNode* BuyQueueNode(QUDataType x)
{
	pQueueNode node = (pQueueNode)malloc(sizeof(QueueNode));
	node->_data = x;
	node->_next = NULL;
	return node;
}
void QueuePush(Queue* pq,QUDataType x)
{
	if(pq->_size == pq->_capacity)
	{
		QUDataType * ptr = NULL;
		ptr = (QUDataType *)realloc(pq->_data, sizeof(QUDataType)*(pq->_capacity) + 1);
		pq->_back->_data = x;
		pq->_back->_next = pq->_back;
	}
}
void QueuePop(Queue* pq)
{
	assert(pq != NULL);
	
	if (pq->_front == NULL)//空队列
	{
		pq->_front = pq->_back = NULL;
	}
	else
	{
		pQueueNode node;
		node = pq->_front->_next;
		free(pq->_front);
		pq->_front = node;
		if (node == NULL)
		{
			pq->_back =NULL;
		}
	}
}
QUDataType QueueBack(Queue* pq)
{
	assert(pq != NULL);
	return pq->_back->_data;
}
QUDataType QueueFront(Queue* pq)
{
	assert(pq != NULL);
	if (pq->_size == 0)
	{
		//printf("队列为空,无法取出\n");
		return 0;
	}
	return pq->_front->_data;
}
int QueueEmpty(Queue* pq)
{
	assert(pq != NULL);
	if (pq->_front == pq->_back)
	{
		
		return 1;
	}
	else
	{
		return 0;
	}
}
int QueueSize(Queue* pq)
{
	assert(pq != NULL);
	if (QueueEmpty(pq))
	{
		return 0;
	}
	else
	{
		return (pq->_back - pq->_front);
	}
}

Topic.c

#include"Queue.h"
#include"Stack.h"
#include"Topic.h"

////////////////////////////////////////////////////////////////
// 1.两个栈实现一个队列
void QueueByTwoStackInit(QueueByTwoStack* qts)
{
	StackEmpty(&qts->s1);
	StackEmpty(&qts->s2);
}
void QueueByTwoStackDestory(QueueByTwoStack* qts)

{
	StackDestory(&qts->s1);
	StackDestory(&qts->s2);
}

void QueueByTwoStackPush(QueueByTwoStack* qts, STDataType x)
{
	assert(qts != NULL);
	StackPush(&(qts->s1), x);
}
void QueueByTwoStackPop(QueueByTwoStack* qts)
{
	assert(qts != NULL);
	//如果s2有数据直接出
	//如果s2没有数据,就把s1的数据倒过来
	if (StackEmpty(&qts->s2) == 0)
	{
		while (StackEmpty(&qts->s1))
		{
			StackPush(&qts->s2, StackTop(&qts->s1));
			StackPop(&qts->s1);
		}
	}
}
STDataType QueueByTwoStackFront(QueueByTwoStack* qts)
{
	assert(qts != NULL);
	return StackTop(&qts->s2);
}
int QueueByTwoStackSize(QueueByTwoStack* qts)
{
	return StackSize(&qts->s1) + StackSize(&qts->s2);
}
int QueueByTwoStackEmpty(QueueByTwoStack* qts)
{

	return StackEmpty(&qts->s1);//StackEmpty(&qts->s2);
}

////////////////////////////////////////////////
// 2.两个队列实现一个栈
void StackByTwoQueueInit(StackByTwoQueue* stq)
{
	QueueInit(&stq->q1);
	QueueInit(&stq->q2);
}
void StackByTwoQueueDestory(StackByTwoQueue* stq)
{
	QueueDestory(&stq->q1);
	QueueDestory(&stq->q2);
}
QUDataType StackByTwoQueueTop(StackByTwoQueue* stq)
{
	if (QueueEmpty(&stq->q1))
	{
		return QueueBack(&stq->q1);
	}
	else
	{
		return QueueFront(&stq->q2);
	}
}
int StackByTwoQueueEmpty(StackByTwoQueue* stq)
{
	return QueueEmpty(&stq->q1)|QueueEmpty(&stq->q2);
}
int StackByTwoQueueSize(StackByTwoQueue* stq)
{
	return QueueSize(&stq->q1);
}

void StackByTwoQueuePush(StackByTwoQueue* stq, QUDataType x)
{
	assert(stq != NULL);
	if (QueueEmpty(&stq->q1) != 0)
	{
		QueuePush(&stq->q1, x);
	}
	else
	{
		QueuePush(&stq->q2, x);
	}
}
void StackByTwoQueuePop(StackByTwoQueue* stq)
{
	Queue* empty, *nonempty;
	assert(stq != NULL);
	empty = &stq->q1;
	nonempty = &stq->q2;
	if (QueueEmpty(&stq->q1) != 0)
	{
		empty = &stq->q2;
		nonempty = &stq->q1;
	}
	while (QueueSize(nonempty) > 1)
	{
		QueuePush(empty, QueueFront(nonempty));
		QueuePop(nonempty);
	}
	QueuePop(nonempty);
}

//////////////////////////////////////////////////////////////
// Min Stack
void MinStackInit(MinStack* pms)
{
	assert(pms != NULL);
	StackInit(&pms->_st);
	StackInit(&pms->_minst);
}
void MinStackDestory(MinStack* pms)
{
	assert(pms != NULL);
	StackDestory(&pms->_st);
	StackDestory(&pms->_minst);
}

void MinStackPush(MinStack* pms, MSTDataType x)
{
	assert(pms != NULL);
	StackPush(&pms->_st,x);
	if (StackEmpty(&pms->_minst) == 0
		||StackTop(&pms->_minst)>=x)
	{
		StackPush(&pms->_minst, x);
	}
}
void MinStackPop(MinStack* pms)
{
	assert(pms != NULL);
	if (StackTop(&pms->_st) == StackTop(&pms->_minst))
	{
		StackPop(&pms->_minst);
	}
		StackTop(&pms->_st);
}
MSTDataType MinStackMin(MinStack* pms)
{
	assert(pms != NULL);
	return StackTop(&pms->_minst);
}
//不合法 0
//合法 1
int IsLegalStackOrder(int* in, int* insize, int* out, int *outsize)
{
	assert(in&&out&&insize == outsize);
	Stack st;
	StackInit(&st);
	int inindex = 0;
	int outindex = 0;
	while (inindex < insize)
	{
		StackPush(&st, in[inindex]);
		++inindex;
		while(!StackEmpty(&st)
			&& StackTop(&st) == out[outindex])
		{
			StackPop(&st);
			++outindex;

		}
	}
	if (StackEmpty(&st) == 0)

	{
		return 1;
	}
	else
	{
		StackDestory(&st);
		return 0;
	}
}
//////////////////////////////////////////////////////
void ShareStackInit(ShareStack* pss)
{
	assert(pss != NULL);
	pss->_top1 = 0;
	pss->_top2 = 1;
}
void ShareStackInit_01(ShareStack* pss)
{
	assert(pss != NULL);
	pss->max_size = 10;
	pss->_top1 = 0;
	pss->_top2 = pss->max_size;

}
void ShareStackPush1(ShareStack* pss, SSDataType x)
{
	assert(pss != NULL);
	if (pss->_top1 == pss->_top2)
	{
		return;
	}
	pss->_a[pss->_top1] = x;
	pss->_top1++;
	return;
}

void ShareStackPush2(ShareStack* pss, SSDataType x)
{
	assert(pss != NULL);
	if (pss->_top1 == pss->_top2)
	{
		return;
	}
	pss->_top2--;
	pss->_a[pss->_top2] = x;
}


// which 1、2
void ShareStackPush(ShareStack* pss, SSDataType x, int which)
{
	assert(pss&&which==1||which==2);
	
	if (which == 1)
	{
		if (pss->_top1 >= N)
		{
			printf("Stack1 Full\n"); 
			return;
		}
			
		pss->_a[pss->_top1] = x;
		pss->_top1 += 2;
	}
	else if (which == 2)
	{
		if (pss->_top2 >= N)
		{
			printf("Stack2 Full\n");
			return;
		}
		pss->_a[pss->_top2] = x;
		pss->_top2 += 2;
	}

}
void ShareStackPop(ShareStack* pss, int which)
{
	assert(pss&&which == 1 || which == 2);
	if (which == 1)
	{
		if (0 == pss->_top1)
		{
			printf("栈1为空\n");
		}
		pss->_top1 -= 2;
	}
	else
	{
		if (0 == pss->_top2)
		{
			printf("栈2为空\n");
		}
		pss->_top2 -= 2;
	}
}
SSDataType ShareStackTop(ShareStack* pss, int which)
{
	assert(pss&&which == 1 || which == 2);
	if (which == 1)
	{
		return pss->_a[pss->_top1 - 1];
	}
	else
	{
		return pss->_a[pss->_top2 - 1];
	}
}

test.c

#include"Queue.h"
#include"Stack.h"
#include"Topic.h"
void TestQueueByTwoStack()
{
	QueueByTwoStack qts;
	QueueByTwoStackInit(&qts);
	QueueByTwoStackPush(&qts, 1);
	QueueByTwoStackPush(&qts, 2);
	QueueByTwoStackPush(&qts, 3);
	
	QueueByTwoStackPop(&qts);
	QueueByTwoStackPop(&qts);
	QueueByTwoStackPush(&qts, 4);
	QueueByTwoStackPush(&qts, 5);
	while (QueueByTwoStackEmpty(&qts))
	{
		printf("%d", QueueByTwoStackFront(&qts));
		QueueByTwoStackPop(&qts);
	}
	printf("\n");
}
void TestStackByTwoQueue()
{
	StackByTwoQueue stq;
	StackByTwoQueueInit(&stq);
	StackByTwoQueuePush(&stq, 1);
	StackByTwoQueuePush(&stq, 2);
	StackByTwoQueuePush(&stq, 3);
	StackByTwoQueuePush(&stq, 4);
	while (StackByTwoQueueEmpty(&stq))
	{
		printf("%d", StackByTwoQueueTop(&stq));
		StackByTwoQueuePop(&stq);
	}
	printf("\n");
}
void TestMinStack()
{
	MinStack mst;
	MinStackInit(&mst);
	MinStackPush(&mst,1);
	MinStackPush(&mst, 4);
	MinStackPush(&mst, 0);
	MinStackPush(&mst, 0);
	MinStackPush(&mst, 8);
	MinStackPush(&mst, 0);
	printf("min:%d\n", MinStackMin(&mst));
	MinStackPop(&mst);
	printf("min:%d\n", MinStackMin(&mst));
	MinStackPop(&mst);
	printf("min:%d\n", MinStackMin(&mst));
	MinStackPop(&mst);
	printf("min:%d\n", MinStackMin(&mst));
	MinStackPop(&mst);
	
	MinStackDestory(&mst);

}
void TestIsLegalStackOrder()
{
	int in[5] = { 1, 2, 3, 4, 5 };
	int out[5] = { 4, 5, 3, 2, 1 };
	printf("%d\n",IsLegalStackOrder(in,5,out,5));
}
void TestShareStack()
{

		ShareStack pss;
		ShareStackInit(&pss);
		ShareStackPush(&pss, 0, 1);
		ShareStackPush(&pss, 2, 1);
		ShareStackPush(&pss, 4, 1);
		ShareStackPush(&pss, 6, 1);
		ShareStackPush(&pss, 8, 1);
		ShareStackPush(&pss, 1, 2);
		ShareStackPush(&pss, 3, 2);
		ShareStackPush(&pss, 5, 2);
		ShareStackPush(&pss, 7, 2);
		printf("%d", ShareStackTop(&pss, 1));
		printf("%d", ShareStackTop(&pss, 2));

}
int main()
{
	//TestQueueByTwoStack();
	//TestStackByTwoQueue();
	//TestMinStack();
	//TestIsLegalStackOrder();
	void TestShareStack();
	system("pause");
	return;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值