Stacks and queues of the training camp for brushing questions


insert image description here

foreword

This question is based on the stack and queue, to consolidate the two questions, so it is basically done on the basis of realizing the stack and queue, if there is no basis for the stack and queue, please see my previous article, data Detailed explanation of the stack and queue of the structure

1. Implementing a stack with a queue

1. Topic introduction

The topic is in 225. Implementing a stack with a queue
insert image description here

2. Ideas

1️⃣ Import the data of the non-empty queue into an empty queue until there is only one data remaining insert image description here
2️⃣ Queue out 4, for the stack, it is 4 out of the stack insert image description here
3️⃣
insert image description here
4️⃣ When one of the queues is empty, it will not be empty The data of the queue is imported into the empty queue until there is only one data remaining
insert image description here
5️⃣Repeat the above operation
insert image description here

3. Code

typedef char QDatatype;

typedef struct QueueNode
{
    
    
	struct QueueNode* next;
	QDatatype Data;
}QNode;

typedef struct Queue
{
    
    
	QNode* head;
	QNode* tail;
	int size;
}Queue;
typedef struct 
{
    
    
    Queue q1;
    Queue q2;
}MyStack;
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)
{
    
    
    assert(pq);
    QNode* newnode = (QNode*)malloc(sizeof(QNode));
    assert(newnode);
    newnode->Data = x;
    newnode->next=NULL;
    if (pq->size == 0)
    {
    
    
        pq->head =pq->tail= newnode;
    }
    else
    {
    
    
        pq->tail->next = newnode;
        pq->tail = newnode;
    }
    pq->size++;

}

void QueuePop(Queue* pq)
{
    
    
    assert(pq);
    assert(pq->size);
    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;
}

//此位置之上属于队列的实现代码,此下为题目所需完成的

MyStack* myStackCreate() {
    
    
    
    MyStack *pst=( MyStack *)malloc(sizeof( MyStack));
    QueueInit(&pst->q1);
    QueueInit(&pst->q2);
    return pst;
}

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

int myStackPop(MyStack* obj) {
    
    
    if(!QueueEmpty(&obj->q1))
    {
    
    
        while(QueueSize(&obj->q1)>1)
        {
    
    
            QDatatype front=QueueFront(&obj->q1);
            QueuePop(&obj->q1);
            QueuePush(&obj->q2,front);
        }
        QDatatype front=QueueFront(&obj->q1);
        QueuePop(&obj->q1);
        return front;
    }
    else
    {
    
    
        while(QueueSize(&obj->q2)>1)
        {
    
    
            QDatatype front=QueueFront(&obj->q2);
            QueuePop(&obj->q2);
            QueuePush(&obj->q1,front);
        }
        QDatatype front=QueueFront(&obj->q2);
        QueuePop(&obj->q2);
        return front;
    }
   
}

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);
}

Second, implement the queue with the stack

1. Topic introduction

The topic is implementing a queue with a stack
insert image description here

2. Ideas

For this question, we need to think carefully about whether we still need to toss and turn data back and forth like the above. For example, let me give an example
insert image description here

When we released a data 1, we found that we don't need to go back and forth,当我们在想出后面的时,我们一直只需要出另外一个

So if we need to input data after outputting one, where do we enter it, for example, consider the following situation, when we output 1 and 2, we enter 1 and 2 at the back of the queue, then we are in the stack How to enter it, because the stacks of 3 and 4 need to get out other data and cannot be entered, so we can only operate the stack on the left and then
insert image description here
insert image description here
if our stack on the right is finished, we repeat the above operation
insert image description here

Then we can conclude
1️⃣ We only use a stack as a data-making, and a stack as a data-input ( 对于上图来说左边是入的,右边是出的)
2️⃣When the out-stack is empty, we put the data in the in-stack into the out-stack
3️⃣ When the output stack is not empty, we only output data to the stack when we output data

3. Code

typedef int STDataType;
typedef struct Stack
{
    
    
	STDataType* a;
	int top;
	int capacity;
}ST;
void StackInit(ST* ps)
{
    
    
	assert(ps);
	ps->a = NULL;
	ps->top = 0;//初始化时如果top是0,即top指向栈顶上的后一位
	ps->capacity = 0;
}
void StackDestroy(ST* ps)
{
    
    
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;

}
void StackPush(ST* ps, STDataType x)
{
    
    
		assert(ps);
		if (ps->top == ps->capacity)
		{
    
    
			int newcapacity = ps->capacity == 0 ? 4: ps->capacity * 2;
			STDataType* temp = (STDataType * )realloc(ps->a, sizeof(STDataType)*newcapacity);
			if (temp == NULL)
			{
    
    
				printf("realloc fail\n");
				exit(-1);
			}
			ps->a = temp;
			ps->capacity = newcapacity;
		}
		ps->a[ps->top] = x;
		ps->top++;

}
void StackPop(ST* ps) 
{
    
    
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}
STDataType StackTop(ST* ps)
{
    
    
	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];
}
bool StackEmpty(ST* ps)
{
    
    
	return ps->top == 0;
}

void StackPrint(ST* ps)
{
    
    
	while (ps->top)
	{
    
    
		printf("%d", StackTop(ps));
		StackPop(ps);
	}
}
typedef struct {
    
    
    ST st1;//入的栈
    ST st2;//出的栈
} MyQueue;


MyQueue* myQueueCreate() {
    
    
    MyQueue* Queue=(MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&Queue->st1);
    StackInit(&Queue->st2);
    return Queue;
}

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

int myQueuePeek(MyQueue* obj) {
    
    
    if(StackEmpty(&obj->st2))
    {
    
    
        while(!StackEmpty(&obj->st1))
        {
    
    
            StackPush(&obj->st2,StackTop(&obj->st1));
            StackPop(&obj->st1);
        }
    }
  
    return StackTop(&obj->st2);
}

int myQueuePop(MyQueue* obj) {
    
    
    int front=myQueuePeek(obj);
    StackPop(&obj->st2);
    return front;
    
}

bool myQueueEmpty(MyQueue* obj) {
    
    
    return (StackEmpty(&obj->st1)&&StackEmpty(&obj->st2));
}

void myQueueFree(MyQueue* obj) {
    
    
    StackDestroy(&obj->st2);
    StackDestroy(&obj->st1);
    free(obj);
}

/**
 * Your MyQueue struct will be instantiated and called as such:
 * MyQueue* obj = myQueueCreate();
 * myQueuePush(obj, x);
 
 * int param_2 = myQueuePop(obj);
 
 * int param_3 = myQueuePeek(obj);
 
 * bool param_4 = myQueueEmpty(obj);
 
 * myQueueFree(obj);
*/

Guess you like

Origin blog.csdn.net/Ruiren_/article/details/129764090