一、实现一个栈,要求实现Push(出栈)、Pop(入栈)、Min(返回最小值)的时间复杂度为O(1)
/////////////////////////////////////////////////////////////////////
//.h
#include "Stack.h"
typedef struct MinStack
{
Stack st;
Stack minst;
}MinStack;
void MinStackInit(MinStack* mst);
void MinStackDestory(MinStack* mst);
void MinStackPush(MinStack* mst, DataType x);
void MinStackPop(MinStack* mst);
int MinStackEmpty(MinStack* mst);
int MinStackSize(MinStack* mst);
int MinStackReturn(MinStack* mst);
void TestMinStack();
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//.c
#include "MinStack.h"
void MinStackInit(MinStack* mst)
{
assert(mst);
StackInit(&mst->st);
StackInit(&mst->minst);
}
void MinStackDestory(MinStack* mst)
{
assert(mst);
StackDestory(&mst->st);
StackDestory(&mst->minst);
}
void MinStackPush(MinStack* mst, DataType x)
{
assert(mst);
if(StackEmpty(&mst->st) == 0)
{
StackPush(&mst->st,x);
StackPush(&mst->minst,x);
}
else if(StackTop(&mst->minst) >= x)
{
StackPush(&mst->st,x);
StackPush(&mst->minst,x);
}
else
StackPush(&mst->st,x);
}
void MinStackPop(MinStack* mst)
{
assert(mst);
if(StackTop(&mst->st) > StackTop(&mst->minst))
{
StackPop(&mst->st);
}
else
{
StackPop(&mst->st);
StackPop(&mst->minst);
}
}
int MinStackReturn(MinStack* mst)
{
return StackTop(&mst->minst);
}
///////////////////////////////////////////////////////////////////////
二、使用两个栈实现一个队列
////////////////////////////////////////////////////////////////
//.h
#include "Stack.h"
//俩个栈实现一个队列
typedef struct QueueBy2S
{
Stack s1;
Stack s2;
}QueueBy2S;
void QueueBy2SInit(QueueBy2S * qbs);
void QueueBy2SDestory(QueueBy2S* qbs);
void QueueBy2SPush(QueueBy2S* qbs,DataType x);
void QueueBy2SPop(QueueBy2S* qbs);
int QueueBy2SEmpty(QueueBy2S* qbs);
int QueueBy2SSize(QueueBy2S* qbs);
DataType QueueBy2SFront(QueueBy2S* qbs);
void TestQueueBy2S();
/////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//.c
#include "QueueBy2S.h"
#include "Stack.h"
void QueueBy2SInit(QueueBy2S* qbs)
{
assert(qbs);
StackInit(&qbs->s1);
StackInit(&qbs->s2);
}
void QueueBy2SDestory(QueueBy2S* qbs)
{
assert(qbs);
StackDestory(&qbs->s1);
StackDestory(&qbs->s2);
}
int QueueBy2SEmpty(QueueBy2S* qbs)
{
return StackEmpty(&qbs->s1) || StackEmpty(&qbs->s2);
}
int QueueBy2SSize(QueueBy2S* qbs)
{
return StackSize(&qbs->s1) + StackSize(&qbs->s2);
}
void QueueBy2SPush(QueueBy2S* qbs,DataType x)
{
assert(qbs);
StackPush(&qbs->s1,x);
}
void QueueBy2SPop(QueueBy2S* qbs)
{
assert(qbs);
if(!StackEmpty(&qbs->s2))
{
while(StackEmpty(&qbs->s1) != 0)
{
StackPush(&qbs->s2,StackTop(&qbs->s1));
StackPop(&qbs->s1);
}
}
StackPop(&qbs->s2);
}
DataType QueueBy2SFront(QueueBy2S* qbs)
{
assert(qbs);
if(!StackEmpty(&qbs->s2))
{
while(StackEmpty(&qbs->s1) != 0)
{
StackPush(&qbs->s2,StackTop(&qbs->s1));
StackPop(&qbs->s1);
}
}
return StackTop(&qbs->s2);
}
////////////////////////////////////////////////////////////////
三、使用两个队列实现一个栈
///////////////////////////////////////////////////////////////////
//.h
#include "Queue.h"
//俩个队列实现栈
typedef struct StackBy2Q
{
Queue q1;
Queue q2;
}StackBy2Q;
void StackBy2QInit(StackBy2Q * sbq);
void StackBy2QDestory(StackBy2Q* sbq);
void StackBy2QPush(StackBy2Q* sbq,DataType x);
void StackBy2QPop(StackBy2Q* sbq);
int StackBy2QSEmpty(StackBy2Q* sbq);
int StackBy2QSize(StackBy2Q* sbq);
void TestStackBy2Q();
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//.c
#include "Queue.h"
#include "StackBy2Q.h"
void StackBy2QInit(StackBy2Q * sbq)
{
QueueInit(&sbq->q1);
QueueInit(&sbq->q2);
}
void StackBy2QDestory(StackBy2Q* sbq)
{
QueueDestory(&sbq->q1);
QueueDestory(&sbq->q2);
}
void StackBy2QPush(StackBy2Q* sbq,DataType x)
{
assert(sbq);
if(QueueEmpty(&sbq->q1) != 0)
{
QueuePush(&sbq->q1,x);
}
else
QueuePush(&sbq->q2,x);
}
void StackBy2QPop(StackBy2Q* sbq)
{
Queue* empty = &sbq->q1,* nonempty = &sbq->q2;
assert(sbq);
if(QueueEmpty(&sbq->q1) != 0)
{
empty = &sbq->q2;
nonempty = &sbq->q1;
}
while(QueueSize(nonempty) > 1)
{
QueuePush(empty,QueueFront(nonempty));
QueuePop(nonempty);
}
QueuePop(nonempty);
}
int StackBy2QSEmpty(StackBy2Q* sbq)
{
return QueueEmpty(&sbq->q1) || QueueEmpty(&sbq->q2);
}
int StackBy2QSize(StackBy2Q* sbq)
{
return QueueSize(&sbq->q1) + QueueSize(&sbq->q2);
}
///////////////////////////////////////////////////////
四、元素出栈、入栈顺序的合法性。如入栈的序列(1,2,3,4,5),出栈序列为(4,5,3,2,1)
////////////////////////////////////////////////////////////////
//.h
#include "Stack.h"
int FixStack(int* in,int insize,int* out,int outsize);
void TestFixStack();
/////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//.c
#include "FixStack.h"
int FixStack(int* in,int insize,int* out,int outsize)
{
Stack fs;
int inindex = 0,outindex = 0;
assert(in);
assert(out);
assert(insize == outsize);
StackInit(&fs);
while(inindex < insize)//入序列不为空
{
StackPush(&fs,in[inindex]);//入栈
++inindex;
while(StackEmpty(&fs) //栈为空
&& StackTop(&fs) == out[outindex])//且栈顶数据和出序列相同
{
StackPop(&fs);
++outindex;
}
}
if(StackEmpty(&fs) == 0)
{
return 1;
}
return 0;
StackDestory(&fs);
}
///////////////////////////////////////////////////////////////////
五、一个实现两个栈(共享栈)
////////////////////////////////////////////////////////////////////
//.h
#include "Stack.h"
#define N 100
typedef struct ShareStack
{
DataType _a[N];
int _top1;
int _top2;
}ShareStack;
void ShareStackInit(ShareStack* sst);
void ShareStackPush(ShareStack* sst,DataType x,int which);
void ShareStackPrint(ShareStack* sst);
void TestShareStack();
////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//.c
#include "ShareStack.h"
void ShareStackInit(ShareStack* sst)
{
assert(sst);
sst->_top1 = 1;
sst->_top2 = 2;
}
void ShareStackPush(ShareStack* sst,DataType x,int which)
{
assert(sst);
if(which == 1)
{
sst->_a[sst->_top1] = x;
sst->_top1 += 2;
}
else if(which == 2)
{
sst->_a[sst->_top2] = x;
sst->_top2 += 2;
}
else
{
exit(1);
}
}
void ShareStackPrint(ShareStack* sst)
{
int i = 1;
for(;i < sst->_top1;i++)
{
printf("%d ",sst->_a[i]);
}
printf("\n");
}
//////////////////////////////////////////////////////////
最后附上栈的代码:
///////////////////////////////////////////////////////////////////////////
//.h
#pragma once
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <stdlib.h>
//typedef int DataType;
//
//#define N 10
//typedef struct Stack
//{
// DataType _a[N];
// int _top; // 栈顶
//}Stack;
typedef int DataType;
typedef struct Stack
{
DataType* _a;
int _top; // 栈顶
int _capacity; // 容量
}Stack;
void StackInit(Stack* ps);
void StackDestory(Stack* ps);
void StackPush(Stack* ps, DataType x);
void StackPop(Stack* ps);
DataType StackTop(Stack* ps);
int StackEmpty(Stack* ps);
int StackSize(Stack* ps);
void StackPrint(Stack * ps);
void TestStack();
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
//.c
#include "Stack.h"
void StackInit(Stack* ps)
{
assert(ps);
ps->_a = (DataType*)malloc(sizeof(DataType));
assert(ps->_a);
ps->_top = 0;
ps->_capacity = 1;
}
void StackDestory(Stack* ps)
{
assert(ps);
if(ps->_a)
{
free(ps->_a);
ps->_a = NULL;
ps->_top = 0;
ps->_capacity = 0;
}
}
void StackPush(Stack* ps,DataType x)
{
assert(ps);
if(ps->_top == ps->_capacity)
{
ps->_a = realloc(ps->_a,2*ps->_capacity*sizeof(DataType));
ps->_capacity *= 2;
}
ps->_a[ps->_top] = x;
ps->_top++;
}
void StackPop(Stack* ps)
{
assert(ps->_a);
assert(ps->_top > 0);
ps->_top--;
}
DataType StackTop(Stack* ps)
{
assert(ps->_a && ps->_top > -1);
return ps->_a[ps->_top-1];
}
//空 0
//非空 1
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->_top == 0 ? 0 :1;
}
int StackSize(Stack* ps)
{
return ps->_top;
}
void StackPrint(Stack *ps)
{
int i;
if(ps->_top == 0)
{
printf("栈为空,无法打印\n");
return ;
}
printf("栈内容为:");
for(i=ps->_top-1;i>=0;i--)
{
printf("%d ",ps->_a[i]);
}
printf("\n");
}
///////////////////////////////////////////////////////////////////////////////