数据结构——栈和队列详解

一、栈的基本概念

1、
栈是一种特殊的线性表,遵循着后进先出的原则(Last In First Out ),只允许在数据的一端进行数据的插入与删除,这一端称为栈顶。
后进先出就像吃甜筒一样,先放进去的在底下,后放进去的在上面,先吃上面的,再吃下面的;当然,我们也可以放一点吃一点,这样也可以达到先进先出的效果,在数据结构中也是如此。
2、
栈顶(top) :线性表允许进行插入与删除的一端,即栈中最后一个被插入元素的位置。
栈底(bottom):线性表不允许进行插入与删除的一端。
元素:栈中存储的数据项。
在这里插入图片描述

二、栈的基本操作

1、初始化一个空栈INIT
2、判断一个栈是否为空STEmpty
3、压栈,将X加入使其成为新栈顶STPush
4、出栈,删除栈顶元素,并将其返回STPop
5、获取栈的大小STSize
6、若栈不为空,读取栈顶元素STTop

三、栈完整代码示例

typedef int SLDataType;
typedef struct Stack
{
	int capacity;
	int top;
	SLDataType* a;
}ST;


void INIT(ST* ps)//栈的初始化
{
	assert(ps);
	ps->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	ps->capacity = 4;
	ps->top = 0;
}

void STDestroy(ST* ps)//栈的销毁
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

void STPush(ST* ps, SLDataType x)//压栈
{
	assert(ps);
	if (ps->capacity == ps->top)
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->a, 
			sizeof(SLDataType) * ps->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->capacity *= 2;
		ps->a = tmp;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

bool STEmpty(ST* ps)//判空
{
	assert(ps);
	return ps->top==0;
}

void STPop(ST* ps)//出栈
{
	assert(ps);
	assert(!STEmpty(ps));
	ps->top--;
}

int STSize(ST* ps)//元素个数
{
	assert(ps);
	return ps->top;
}

SLDataType STTop(ST* ps)//读取栈顶元素
{
	assert(ps);
	assert(!STEmpty(ps));
	return ps->a[ps->top -1 ];
}

此代码示例中,站在初始化时,top即可为0,也可以初始化为-1。只不过在压栈时,操作有所不同你。

ps->top=0,是栈顶元素下一位置

ps->top=-1,是栈顶元素位置

在这里插入图片描述

队列

一、队列的基本概念

1、
队列也是一种特殊的线性表,遵循着先进先出(First In First Out)的原则。只允许在一端进行插入,在另一端进行删除操作的线性表。
2、
队头(Front):队列第一个元素所在位置,允许删除的一端。
队尾(Rear):队列最后一个元素所在位置,允许插入的一段。
元素:队列中存储的数据项。
在这里插入图片描述

二、队列的基本操作

1、初始化一个空队列QueueInit
2、在队列尾部插入数据QueuePush
3、在队列头部删除数据QueuePop
4、获取队列元素数量QueueSize
5、判断队列是否为空QueueEmpty
6、获取队列头部元素的数据QueueFront
7、获取队列尾部元素的数据QueueBack
8、队列的销毁QueueDestroy

三、队列完整代码示例

typedef int QDatatype;
typedef struct QueueNode
{
	struct QueueNode* next;
	QDatatype data;
}QNode;

typedef struct Queue
{
	QNode* head;
	QNode* tail;
	int size;
}Queue;

void QueueInit(Queue* pq)//初始化
{
	assert(pq);
	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueueDestroy(Queue* pq)//销毁
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueuePush(Queue* pq, QDatatype x)//尾插
{
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return NULL;
	}
	newnode->data = x;
	newnode->next = NULL;
	if (pq->head == NULL)
	{
		assert(pq->tail == NULL);
		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
	pq->size++;
}

void QueuePop(Queue* pq)//头删
{
	assert(pq);
	assert(pq->head != NULL);
	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}

	pq->size--;
}

int QueueSize(Queue* pq)//获取队列元素数量
{
	assert(pq);
	return pq->size;
}

bool QueueEmpty(Queue* pq)//判空
{
	assert(pq);
	return pq->size == 0;
}

QDatatype QueueFront(Queue* pq)//获取队头数据
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->head->data;
}

QDatatype QueueBack(Queue* pq)//获取队尾数据
{
	assert(pq);
	assert(!QueueEmpty(pq));
	return pq->tail->data;
}

栈与队列的相关题解

1用栈实现队列用栈实现队列
思路

  • 栈的功能是后进先出,队列的功能是先进先出
  • 我们可以使用两个栈来实现队列的功能
    在这里插入图片描述
  • 如图有两个栈,若想要实现先进先出的功能,可开辟两个栈,一个入栈(push),一个出栈(pop)
  • 先在push栈中入栈1,2,3,4,再将push栈中的数字导入pop栈中
  • 若想继续入栈,则入栈push中,当pop栈中的数字出完,再将push栈中的数字导入pop中,继续pop,这样就可以达到先入先出的功能
typedef int SLDataType;
typedef struct Stack
{
	int capacity;
	int top;
	SLDataType* a;
}ST;
//
void INIT(ST* ps)//栈的初始化
{
	assert(ps);
	ps->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);
	if (ps->a == NULL)
	{
		perror("malloc fail");
		return;
	}
	ps->capacity = 4;
	ps->top = 0;//
}

void STDestroy(ST* ps)//栈的销毁
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

void STPush(ST* ps, SLDataType x)//压栈
{
	assert(ps);
	if (ps->capacity == ps->top)
	{
		SLDataType* tmp = (SLDataType*)realloc(ps->a, 
			sizeof(SLDataType) * ps->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->capacity *= 2;
		ps->a = tmp;
	}
	ps->a[ps->top] = x;
	ps->top++;
}

bool STEmpty(ST* ps)//判空
{
	assert(ps);
	return ps->top==0;
}

void STPop(ST* ps)//出栈
{
	assert(ps);
	assert(!STEmpty(ps));
	ps->top--;
}

int STSize(ST* ps)//元素个数
{
	assert(ps);
	return ps->top;
}

SLDataType STTop(ST* ps)//读取栈顶元素
{
	assert(ps);
	assert(!STEmpty(ps));
	return ps->a[ps->top -1 ];
}

typedef struct {
    ST pushst;
    ST popst;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue* obj=(MyQueue*)malloc(sizeof(MyQueue));
    if(obj==NULL)
    {
        perror("malloc fail");
        return NULL;
    }
    INIT(&obj->pushst);
    INIT(&obj->popst);
    return obj;
}

void myQueuePush(MyQueue* obj, int x) {
    STPush(&obj->pushst,x);
}

int myQueuePop(MyQueue* obj) {
    if(STEmpty(&obj->popst))
    {
        while(!STEmpty(&obj->pushst))
        {
            STPush(&obj->popst,STTop(&obj->pushst));
            STPop(&obj->pushst);
        }
    }
    int front=STTop(&obj->popst);
    STPop(&obj->popst);
    return front;
}

int myQueuePeek(MyQueue* obj) {
    if(STEmpty(&obj->popst))
    {
        while(!STEmpty(&obj->pushst))
        {
            STPush(&obj->popst,STTop(&obj->pushst));
            STPop(&obj->pushst);
        }
    }
    return STTop(&obj->popst);
}

bool myQueueEmpty(MyQueue* obj) {
    return STEmpty(&obj->pushst)&&STEmpty(&obj->popst);
}

void myQueueFree(MyQueue* obj) {
    STDestroy(&obj->popst);
    STDestroy(&obj->pushst);\
    free(obj);
}

2用队列实现栈用队列实现栈
思路

  • 栈的功能是后进先出,队列的功能是先进先出
  • 我们可以使用两个队列来实现栈的功能
  • 一个队列为空,一个队列存数据
  • 若想达到出栈的目的,则需把前面的数据倒入空队列
    在这里插入图片描述
typedef char QDatatype;
typedef struct QueueNode
{
	struct QueueNode* next;
	QDatatype data;
}QNode;

typedef struct Queue
{
	QNode* head;
	QNode* tail;
	int size;
}Queue;

void QueueInit(Queue* pq)
{
	assert(pq);

	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueueDestroy(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}

	pq->head = pq->tail = NULL;
	pq->size = 0;
}

void QueuePush(Queue* pq, QDatatype x)
{
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;

	if (pq->head == NULL)
	{
		assert(pq->tail == NULL);

		pq->head = pq->tail = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}

	pq->size++;
}

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(pq->head != NULL);

	

	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QNode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}

	pq->size--;
}

int QueueSize(Queue* pq)
{
	assert(pq);

	return pq->size;
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);

	return pq->size == 0;
}

QDatatype QueueFront(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->head->data;
}

QDatatype QueueBack(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));

	return pq->tail->data;
}
typedef struct {
    Queue q1;
    Queue q2;
} MyStack;


MyStack* myStackCreate() {
    MyStack* pst=(MyStack*)malloc(sizeof(MyStack));
    if(pst==NULL)
    {
        perror("malloc fail");
        return NULL;
    }
    QueueInit(&pst->q1);
    QueueInit(&pst->q2);
    return pst;
}

void myStackPush(MyStack* obj, int x) {
    if(!QueueEmpty(&obj->q1))
    {
        QueuePush(&obj->q1,x);
    }
    else
    {
        QueuePush(&obj->q2,x);
    }
}

int myStackPop(MyStack* obj) {
//假设q1是空队列
    Queue* emptyQ=&obj->q1;
    Queue* noemptyQ=&obj->q2;
    if(!QueueEmpty(&obj->q1))
    {
        emptyQ=&obj->q2;
        noemptyQ=&obj->q1;
    }

    //倒数据  将非空队列的数据倒入空队列中,
    while(QueueSize(noemptyQ)>1)
    {
        QueuePush(emptyQ,QueueFront(noemptyQ));
        QueuePop(noemptyQ);
    }
    int top=QueueFront(noemptyQ);
    QueuePop(noemptyQ);
    return top;

}

int myStackTop(MyStack* obj) {
    if(!QueueEmpty(&obj->q1))
    {
        return QueueBack(&obj->q1);
    }
    else
    {
        return QueueBack(&obj->q2);
    }
}

bool myStackEmpty(MyStack* obj) {
    return (QueueEmpty(&obj->q1)&&QueueEmpty(&obj->q2)) ;
}

void myStackFree(MyStack* obj) {
    QueueDestroy(&obj->q1);
    QueueDestroy(&obj->q2);
    free(obj);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值