栈和队列的面试题

#pragma once

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

typedef int SDataType;

#define MAX_SIZE (100)

typedef struct Stack
{
	SDataType array[MAX_SIZE];
	int top;		// 起了个别名,含义还是 size
}	Stack;

//初始化
void StackInit(Stack *pS)
{
	pS->top = 0;
}

//销毁
void StackDestroy(Stack *pS)
{
	pS->top = 0;
}

//压栈
void StackPush(Stack *pS, SDataType data)
{
	assert(pS->top < MAX_SIZE);
	pS->array[pS->top++] = data;
}


//出栈
void StackPop(Stack *pS)
{
	assert(pS->top > 0);
	pS->top--;
}

//返回栈顶元素
SDataType StackTop(const Stack *pS)
{
	assert(pS->top > 0);
	return pS->array[pS->top - 1];
}

//求栈的元素个数
int StackSize(const Stack *pS)
{
	return pS->top;
}

//判断栈是否为空
int StackEmpty(const Stack *pS)
{
	return pS->top == 0 ? 1 : 0;
}

队列

#pragma  once

#include "assert.h"
#include "stdio.h"
#include "stdlib.h"

typedef int QDataType;

typedef struct QNode 
{
	QDataType data;
	struct QNode* next;
}QNode;

typedef struct Queue
{
	struct QNode *front;
	struct QNode *rear;
	int size;
}	Queue;

//初始化
void QueueInit(Queue* pQ)
{
	assert(pQ);
	pQ->front = pQ->rear = NULL;
}

//销毁
void QueueDestroy(Queue* pQ)
{
	assert(pQ);
	QNode *cur, *next;
	for (cur = pQ->front; cur != NULL;cur=next)
	{
		next = cur->next;
		free(cur);
	}
	pQ->front = pQ->rear = NULL;
}

// 插入
void QueuePush(Queue *pQ, QDataType data)
{
	pQ->size++;
	QNode* node = (QNode*)malloc(sizeof(QNode));
	node->data = data;
	node->next = NULL;

	if (pQ->rear==NULL)
	{
		pQ->front = pQ->rear=node;
		return;
	}
	pQ->rear->next = node;
	pQ->rear = node;
}

//删除
void QueuePop(Queue* pQ)
{
	pQ->size--;
	assert(pQ);
	assert(pQ->front);
#if 0
	if (pQ->front==pQ->rear)
	{
		free(pQ->front);
		pQ->front = pQ->rear = NULL;
	}
	else
	{
		QNode* Del = pQ->front;
		pQ->front = pQ->front->next;
		free(Del);
	}
#endif
	QNode *del = pQ->front;
	pQ->front = pQ->front->next;
	free(del);
	if (pQ->front==NULL)
	{
		pQ->rear = NULL;
	}
}

QDataType QueueFront(const Queue *pQ)
{
	assert(pQ != NULL);
	assert(pQ->front != NULL);	// 表示不能是空队列

	return pQ->front->data;
}

int QueueEmpty(const Queue *pQ)
{
	return pQ->front == NULL ? 1 : 0;
}

int QueueSize(const Queue *pQ)
{
	return pQ->size;
}

面试题

  • 实现一个栈,要求实现Push(入栈)、Pop(出栈)、Min(返回最小值)的时间复杂度为O(1)

#pragma once

#include "stack.h"

typedef struct MinStruct 
{
	struct Stack s;//普通的栈
	struct Stack m;//最小栈
}MinStack;


void MinStackInit(MinStack* pMS)
{
	StackInit(&pMS->s);
	StackInit(&pMS->m);
}


void MinStackDestroy(MinStack* pMS)
{
	StackDestroy(&pMS->s);
	StackDestroy(&pMS->m);
}


void MinStackPush(MinStack *pMS, SDataType data)
{
#if 0
	StackPush(&pMS->s, data);
	if (StackEmpty(&pMS->m) || data < StackTop(&pMS->m))
	{
		StackPush(&pMS->m, data);
	}
	else
	{
		StackPush(&pMS->m, StackTop(&pMS->m));
	}
#endif

	StackPush(&pMS->s, data);
	if (StackEmpty(&pMS->m) || data < StackTop(&pMS->m)) 
	{
		StackPush(&pMS->m, data);
	}
	
}


void MinStackPop(MinStack *pMS)
{
#if 0
	StackPop(&pMS->s);
	StackPop(&pMS->m);
#endif
	
	SDataType data = StackTop(&pMS->s);
	StackPop(&pMS->s);
	if (data==StackTop(&pMS->m))
	{
		StackPop(&pMS->m);
	}
}


SDataType MinStackTop(MinStack *pMS)
{
	return StackTop(&pMS->s);
}


SDataType MinStackMin(MinStack *pMS)
{
	return StackTop(&pMS->m);
}

  • 使用两个栈实现一个队列

//使用两个栈实现一个队列

#pragma once

#include "stack.h"

typedef struct TSIOQueue
{
	Stack push;
	Stack pop;
}TSQueue;


void TSQueueInit(TSQueue* pTSQ)
{
	StackInit(&pTSQ->push);
	StackInit(&pTSQ->pop);
}


void TSQueueDestroy(TSQueue* pTSQ)
{
	StackDestroy(&pTSQ->push);
	StackDestroy(&pTSQ->pop);
}


void TSQueuePush(TSQueue* pTSQ, SDataType data)
{
	StackPush(&pTSQ->push, data);
}


void TSQueuePop(TSQueue* pTSQ)
{
	if (StackEmpty(&pTSQ->pop))
	{
		while (!StackEmpty(&pTSQ->push))
		{
			SDataType data = StackTop(&pTSQ->push);
			StackPop(&pTSQ->push);

			StackPush(&pTSQ->pop, data);
		}
	}

	StackPop(&pTSQ->pop);
}


SDataType TSQueueFront(TSQueue* pTSQ)
{
	if (StackEmpty(&pTSQ->pop))
	{
		while (!StackEmpty(&pTSQ->push))
		{
			SDataType data = StackTop(&pTSQ->push);
			StackPop(&pTSQ->push);

			StackPush(&pTSQ->pop, data);
		}
	}

	return StackTop(&pTSQ->pop);
}
  • 使用两个队列实现一个栈

//使用两个队列实现一个栈

#pragma once

#include "Queue.h"

typedef struct TQIOStack
{
	struct Queue queue1;
	struct Queue queue2;
}TQStack;


void TQStackInit(TQStack* pTQS)
{
	QueueInit(&pTQS->queue1);
	QueueInit(&pTQS->queue2);
}


void TQStackDestroy(TQStack* pTQS)
{
	QueueDestroy(&pTQS->queue1);
	QueueDestroy(&pTQS->queue2);
}


void TQStackPush(TQStack* pTQS,QDataType data)
{
	//默认队列一
	Queue* pQueue = &pTQS->queue1;
	if (!QueueEmpty(&pTQS->queue2))
	{
		pQueue = &pTQS->queue2;
	}

	QueuePush(pQueue, data);
}


void TQStackPop(TQStack* pTQS)
{
	Queue* pNotEmpty = &pTQS->queue1;
	Queue* pEmpty = &pTQS->queue2;
	if (!QueueEmpty(&pTQS->queue2))
	{
		pNotEmpty = &pTQS->queue2;
		pEmpty = &pTQS->queue1;
	}

	while (QueueSize(pNotEmpty)>1)
	{
		QDataType data = QueueFront(pNotEmpty);
		QueuePop(pNotEmpty);
		QueuePush(pEmpty, data);
	}

	QueuePop(pNotEmpty);
}


QDataType TQStackTop(TQStack* pTQS)
{
	Queue* pNotEmpty = &pTQS->queue1;
	Queue* pEmpty = &pTQS->queue2;
	if (!QueueEmpty(&pTQS->queue2))
	{
		pNotEmpty = &pTQS->queue2;
		pEmpty = &pTQS->queue1;
	}

	while (QueueSize(pNotEmpty) > 1)
	{
		QDataType data = QueueFront(pNotEmpty);
		QueuePop(pNotEmpty);
		QueuePush(pEmpty, data);
	}

	QDataType data = QueueFront(pNotEmpty);
	QueuePop(pNotEmpty);
	QueuePush(pEmpty, data);
	return data;
}
  • 判断元素出栈、入栈的合法性

//判断元素出栈、入栈的合法性

#pragma once

#include <stdio.h>
#include "stack.h"


void JOLPrint(int in[],int out[], int size)
{
	int ii = 0; int io = 0;
	Stack stack;
	StackInit(&stack);

	while (ii<size)
	{
		if (in[ii]==out[io])
		{
			ii++; io++;
		}
		else
		{
			if (!StackEmpty(&stack) && out[io] == StackTop(&stack))
			{
				StackPop(&stack);
				io++;
			}
			else
			{
				StackPush(&stack, in[ii]);
				ii++;
			}
		}
	}

	while (!StackEmpty(&stack))
	{
		if (out[io]!=StackTop(&stack))
		{
			printf("不合法\n");
			return;
		}

		StackPop(&stack);
		io++;
	}
	printf("合法\n");
}


void test()
{
	int in[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
	int out[] = { 'f', 'e', 'g', 'd', 'a', 'c', 'b' };
	int size = sizeof(in) / sizeof(int);

	JOLPrint(in, out, size);
}
  • 一个数组实现两个栈(共享栈)

                   1.首尾共享栈

#pragma once

#include "stdlib.h"
#include "assert.h"

typedef int SDataType;

typedef struct HTStack
{
	SDataType *array;
	int capacity;
	int top1;
	int top2;
} HTStack;


void HTStackInit(HTStack* pHT)
{
	pHT->capacity = 6;
	pHT->array = (SDataType *)malloc(sizeof(SDataType) * 6);
	pHT->top1 = 0;
	pHT->top2 = 6 - 1;
}

void HTStackDestroy(HTStack* pHT)
{
	free(pHT->array);
}


void ExpandCapacity(HTStack* pHT)
{
	if (pHT->top2 > pHT->top1-1)
	{
		return;
	}

	int newCapacity = pHT->capacity * 2;
	SDataType* newArray = (SDataType*)malloc(sizeof(SDataType)*newCapacity);

	for (int i = 0; i < pHT->top1;i++)
	{
		newArray[i] = pHT->array[i];
	}

	int expandLength = newCapacity - pHT->capacity;
	for (int j = pHT->capacity - 1; j > pHT->top2;j--)
	{
		newArray[j + expandLength] = pHT->array[j];
	}

	pHT->top2 += expandLength;

	free(pHT->array);
	pHT->array = newArray;
}


void HTStackPush_1(HTStack* pHT,SDataType data)
{
	ExpandCapacity(pHT);

	pHT->array[pHT->top1] = data;
	pHT->top1++;
}


void HTStackPush_2(HTStack* pHT, SDataType data)
{
	ExpandCapacity(pHT);

	pHT->array[pHT->top2] = data;
	pHT->top2--;
}


void HTStackPop_1(HTStack* pHT)
{
	if (pHT->top1<=0)
	{
		return;
	}

	pHT->top1--;
}



void HTStackPop_2(HTStack* pHT)
{
	if (pHT->top1>=pHT->capacity)
	{
		return;
	}

	pHT->top2++;
}


SDataType HTStackTop_1(HTStack* pHT)
{
	assert(pHT->top1 > 0);
	return pHT->array[pHT->top1 - 1];
}


SDataType HTStackTop_2(HTStack* pHT)
{
	assert(pHT->top1 < pHT->capacity-1);
	return pHT->array[pHT->top2 + 1];
}


int HTStackSize_1(HTStack* pHT)
{
	return pHT->top1;
}


int HTStackSize_2(HTStack* pHT)
{
	return pHT->capacity-1-pHT->top2;
}


int HTStackEmpty_1(HTStack* pHT)
{
	return pHT->top1 = 0 ? 1 : 0;
}


int HTStackEmpty_2(HTStack* pHT)
{
	return pHT->top2 = pHT->capacity-1 ? 1 : 0;
}

              2.奇偶共享栈

#pragma omce

#include "stdlib.h"
#include "assert.h"

typedef int SDataType;

typedef struct PSStack
{
	SDataType *array;
	int capacity;
	int top1;
	int top2;
} PSStack;


void PSStackInit(PSStack* pPS)
{
	pPS->capacity = 6;
	pPS->array = (SDataType *)malloc(sizeof(SDataType) * 6);
	pPS->top1 = 0;
	pPS->top2 = 1;
}

void PSStackDestroy(PSStack* pPS)
{
	free(pPS->array);
}


void ExpandCapacity(PSStack* pPS)
{
	if (pPS->top1 == pPS->capacity-2|| pPS->top2 == pPS->capacity - 1)
	{
		return;
	}

	int newCapacity = pPS->capacity * 2;
	SDataType* newArray = (SDataType*)malloc(sizeof(SDataType)*newCapacity);

	for (int i = 0; i < pPS->top1; i+=2)
	{
		newArray[i] = pPS->array[i];
	}

	for (int j = 1; j <pPS->top2; j+=2)
	{
		newArray[j] = pPS->array[j];
	}

	free(pPS->array);
	pPS->array = newArray;

}


void PSStackPush_1(PSStack* pPS, SDataType data)
{
	ExpandCapacity(pPS);

	pPS->array[pPS->top1] = data;
	pPS->top1+=2;
}


void PSStackPush_2(PSStack* pPS, SDataType data)
{
	ExpandCapacity(pPS);

	pPS->array[pPS->top2] = data;
	pPS->top1 += 2;
}


void PSStackPop_1(PSStack* pPS)
{
	if (pPS->top1 <= 0)
	{
		return;
	}

	pPS->top1-=2;
}


void PSStackPop_2(PSStack* pPS)
{
	if (pPS->top1 <= 1)
	{
		return;
	}

	pPS->top2 -= 2;
}


SDataType PSStackTop_1(PSStack* pPS)
{
	assert(pPS->top1 > 0);
	return pPS->array[pPS->top1 - 2];
}

SDataType PSStackTop_2(PSStack* pPS)
{
	assert(pPS->top2 > 1);
	return pPS->array[pPS->top1 - 2];
}


int PSStackSize_1(PSStack* pPS)
{
	return (pPS->top1)/ 2;
}

int PSStackSize_2(PSStack* pPS)
{
	return (pPS->top2) / 2;
}


int PSStackEmpty_1(PSStack* pPS)
{
	return pPS->top1 = 0 ? 1 : 0;
}


int PSStackEmpty_2(PSStack* pPS)
{
	return pPS->top2 = 1 ? 1 : 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值