1. 利用顺序表实现栈
函数接口:
typedef int SDataType;
typedef struct Stack {
SDataType array[100];
int top;
} Stack;
// 初始化
void StackInit(Stack *pStack);
// 压栈
void StackPush(Stack *pStack, SDataType data);
// 出栈
void StackPop(Stack *pStack);
// 返回栈顶元素
SDataType StackTop(Stack *pStack);
// 判断是否为空
// 1 空
// 0 不空
int StackIsEmpty(Stack *pStack);
// 返回数据个数
int StackSize(Stack *pStack);
2. 利用单链表实现队列
函数接口:
typedef int QDataType;
typedef struct QNode {
QDataType data;
struct QNode *pNext;
} QNode;
typedef struct Queue {
QNode *pFront;
QNode *pRear;
int size;
} Queue;
// 初始化
void QueueInit(Queue *pQueue);
// 入队列
void QueuePush(Queue *pQueue, QDataType data);
// 出队列
void QueuePop(Queue *pQueue);
// 返回队首元素
QDataType QueueFront(Queue *pQueue);
// 判断是否为空
// 1 空
// 0 不空
int QueueIsEmpty(Queue *pQueue);
// 返回数据个数
int QueueSize(Queue *pQueue);
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int QDataType;
typedef struct QNode {
QDataType data;
struct QNode *pNext;
} QNode;
typedef struct Queue {
QNode *pFront;
QNode *pRear;
int size;
} Queue;
// 初始化
void QueueInit(Queue *pQueue)
{
assert(pQueue);
pQueue->pFront = pQueue->pRear = NULL;
pQueue->size = 0;
}
// 入队列
void QueuePush(Queue *pQueue, QDataType data)
{
assert(pQueue);
pQueue->size++;
QNode *pNewNode = (QNode*)malloc(sizeof(QNode));
assert(pNewNode);
pNewNode->data = data;
pNewNode->pNext = NULL;
if (pQueue->pRear == NULL)
{
pQueue->pFront = pQueue->pRear = pNewNode;
return;
}
pQueue->pRear->pNext = pNewNode;
pQueue->pRear = pNewNode;
}
// 出队列
void QueuePop(Queue *pQueue)
{
assert(pQueue);
pQueue->size--;
QNode *pOldNode = pQueue->pFront;
pQueue->pFront = pQueue->pFront->pNext;
free(pOldNode);
if (pQueue->pFront == NULL)
{
pQueue->pRear = NULL;
}
}
// 返回队首元素
QDataType QueueFront(Queue *pQueue)
{
assert(pQueue);
assert(pQueue->size > 0);
return pQueue->pFront->data;
}
// 判断是否为空
// 1 空
// 0 不空
int QueueIsEmpty(Queue *pQueue)
{
return pQueue->size == 0 ? 1 : 0;
}
// 返回数据个数
int QueueSize(Queue *pQueue)
{
return pQueue->size;
}
void print2(Queue *pQueue)
{
assert(pQueue);
QNode *pNode = pQueue->pFront;
int i = 0;
for (pNode; i<pQueue->size; pNode = pNode->pNext)
{
printf("%d ", pNode->data);
i++;
}
printf("\n");
}
void testQueue()
{
Queue queue;
QueueInit(&queue);
QueuePush(&queue, 1);
QueuePush(&queue, 2);
QueuePush(&queue, 3);
QueuePush(&queue, 4);
print2(&queue);
int x = QueueSize(&queue);
printf("%d\n", x);
QueuePop(&queue);
print2(&queue);
}
3. 栈与队列面试题
1) 实现一个栈,要求实现Push(出栈)、Pop(入栈)、Min(返回最小值)的时间复杂度为O(1)
2) 使用两个栈实现一个队列
3) 使用两个队列实现一个栈
4) 元素出栈、入栈顺序的合法性。如入栈的序列(1,2,3,4,5),出栈序列为 (4,5,3,2,1)
5) 一个数组实现两个栈(共享栈)
#pragma once
typedef void* SDataType;
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef struct Stack {
SDataType array[100];
int top;//表示个数
} Stack;
// 初始化
void StackInit(Stack *pStack)
{
assert(pStack != NULL);
pStack->top = 0;
}
void Stackdestroy(Stack *pStack)
{
pStack->top = 0;
}
// 压栈
void StackPush(Stack *pStack, SDataType data)
{
assert(pStack);
if (pStack->top < 100)
{
pStack->array[pStack->top] = data;
pStack->top++;
}
}
// 出栈
void StackPop(Stack *pStack)
{
assert(pStack);
if (pStack->top > 0)
{
pStack->top--;
}
}
// 返回栈顶元素
SDataType StackTop(Stack *pStack)
{
assert(pStack);
if (pStack->top > 0)
{
return pStack->array[pStack->top-1];
}
}
// 判断是否为空
// 1 空
// 0 不空
int StackIsEmpty(Stack *pStack)
{
return pStack->top == 0 ? 1 : 0;
}
// 返回数据个数
int StackSize(Stack *pStack)
{
return pStack -> top;
}
void print1(Stack *pStack)//打印栈
{
assert(pStack);
while (pStack->top)
{
printf("%d ", pStack->array[pStack->top - 1]);
pStack->top--;
}
printf("\n");
}
void TestStack()
{
Stack stack;
StackInit(&stack);
StackPush(&stack, 1);
StackPush(&stack, 1);
StackPush(&stack, 1);
StackPush(&stack, 1);
int x=StackTop(&stack);
printf("%d\n", x);
int y = StackSize(&stack);//元素个数
printf("%d\n", y);
int z = StackIsEmpty(&stack);//判空
printf("%d\n", z);
StackPop(&stack);//出栈
print1(&stack);
Stackdestroy(&stack);
}
//实现一个栈,要求实现Push(出栈)Pop(入栈)、Min(返回最小值)的时间复杂度为O(1)
/*typedef struct MinStack {
int array1[100]; // 普通数据
int top1;
int array2[100]; // 最小数据
int top2;
} MinStack;*/
typedef struct MinStack{
int array[100];
int top;
}MinStack;
void Init(MinStack *pStack)
{
pStack->top = 0;
//pStack->top1 = pStack->top2 = 0;
}
void Push(MinStack *pStack, int data)
{
int min = data;
if (pStack->top != 0 && pStack->array[pStack->top - 1] < data)
{
min = pStack->array[pStack->top - 1];
}
pStack->array[pStack->top++] = data;
pStack->array[pStack->top++] = min;
/*if (pStack->top2 != 0 && pStack->array2[pStack->top2-1] < data)
{
min = pStack->array2[pStack->top2-1];
}
pStack->array1[pStack->top1++] = data;
pStack->array2[pStack->top2++] = min;*/
}
void Pop(MinStack *pStack)
{
pStack->top -= 2;
//pStack->top1--;
//pStack->top2--;
}
int Top(MinStack *pStack)
{
return pStack->array[pStack->top - 2];
//return pStack->array1[pStack->top1-1];
}
int Min(MinStack *pStack)
{
return pStack->array[pStack->top - 1];
//return pStack->array2[pStack->top2-1];
}
void TestMinStack()
{
MinStack minStack;
int array[] = { 3, 2, 3, 4, 5, 6, 7, 8, 9 };
Init(&minStack);
int i = 0;
for (; i < sizeof(array) / sizeof(array[0]); i++)
{
Push(&minStack, array[i]);
printf("栈顶元素: %d,Min: %d\n", Top(&minStack), Min(&minStack));
}
for (; i < sizeof(array) / sizeof(array[0]); i++) {
Pop(&minStack);
}
}
//使用两个栈实现一个队列
typedef struct SQueue{
Stack stack1;//入数据
Stack stack2;//出数据
}SQueue;
void SInit(SQueue *PS)
{
Stack *p1,*p2;
p1 = &(PS->stack1);
p2 = &(PS->stack2);
StackInit(p1);
StackInit(p2);
}
void SPush(SQueue *PS, SDataType data)
{
Stack *p1, *p2;
p1 = &(PS->stack1);
p2 = &(PS->stack2);
StackPush(p1,data);
}
void SPop(SQueue *PS)
{
Stack *p1, *p2;
p1 = &(PS->stack1);
p2 = &(PS->stack2);
SDataType data;
if (StackIsEmpty(p2))
{
while (!StackIsEmpty(p1))
{
data = StackTop(p1);
StackPop(p1);
StackPush(p2, data);
}
}
StackPop(p2);
}
SDataType SFront(SQueue *PS)
{
Stack *p1, *p2;
p1 = &(PS->stack1);
p2 = &(PS->stack2);
SDataType data;
if (StackIsEmpty(p2))
{
while (!StackIsEmpty(p1))
{
data = StackTop(p1);
StackPop(p1);
StackPush(p2, data);
}
}
return StackTop(p2);
}
//使用两个队列实现一个栈
#include "Queue.h"
typedef struct QStack
{
Queue queue;
}Qstack;
void QInit(Qstack *psd)
{
QueueInit(&(psd->queue));
}
void QPush(Qstack *psd, SDataType data)
{
QueuePush(&(psd->queue), data);
}
void QPop(Qstack *psd)
{
int i;
SDataType data;
Queue *PQ = &(psd->queue);
for (i = 1; i < QueueSize(PQ); i++)
{
data = QueueFront(PQ);
QueuePop(PQ);
QueuePush(PQ, data);
}
QueuePop(PQ);
}
QDataType STop(Qstack *psd)
{
int i;
SDataType data;
Queue *PQ = &(psd->queue);
for (i = 1; i < QueueSize(PQ); i++)
{
data = QueueFront(PQ);
QueuePop(PQ);
QueuePush(PQ, data);
}
data = QueueFront(PQ);
QueuePop(PQ);
QueuePush(PQ, data);
return data;
}
#pragma once
#include<stdio.h>
//1. 两个数组实现
//2. 不再每次都入最小栈
typedef struct Minstack
{
int array1[100];
int top1;//普通数据
int array2[100];
int top2;//最小数据
}Minstack;
void Init(Minstack *pS)
{
pS->top1 = pS->top2 = 0;
}
void Push(Minstack *pS,int data)
{
pS->array1[pS->top1++] = data;
if (pS->top2 == 0 || data <= pS->array2[pS->top2 - 1])
{
pS->array2[pS->top2++] = data;
}
}
void Pop(Minstack *pS)
{
if (pS->array1[pS->top1 - 1] == pS->array2[pS->top2 - 1])
{
pS->top2--;
}
pS->top1--;
}
int Top(Minstack *pS)
{
return pS->array1[pS->top1 - 1];
}
int Min(Minstack *pS)
{
return pS->array2[pS->top2 - 1];
}
void test()
{
Minstack minStack;
int array[] = { 3, 2, 3, 4, 5, 6, 7, 8, 9 };
Init(&minStack);
int i = 0;
for (; i < sizeof(array) / sizeof(array[0]); i++)
{
Push(&minStack, array[i]);
printf("栈顶元素: %d,Min: %d\n", Top(&minStack), Min(&minStack));
}
for (; i < sizeof(array) / sizeof(array[0]); i++) {
Pop(&minStack);
}
}