OJ、用栈实现队列&用队列实现栈

用栈实现队列

typedef int DataT;

typedef struct Stack
{
    
    
	int* a;
	int top;//栈顶下标
	int capacity;//目标容量
}Stack;

//初始化
void StackInit(Stack* pst)
{
    
    
	assert(pst);
	pst->a = (int *)malloc(sizeof(DataT)*4);
	pst->top = 0;
	pst->capacity = 4;
}
//销毁
void StackDestroy(Stack* pst)
{
    
    
	assert(pst);
	free(pst->a);
	pst->a = NULL;
	pst->top = pst->capacity = 0;
}

//入栈
void StackPush(Stack* pst,DataT x)
{
    
    
	assert(pst);
	//空间不够则增容
	if (pst->top == pst->capacity)
	{
    
    
		pst->capacity *= 2;
		DataT* tmp = (DataT*)realloc(pst->a, sizeof(DataT) * pst->capacity);
		if (tmp == NULL)
		{
    
    
			printf("内存不足\n");
			exit(-1);
		}
		else
		{
    
    
			pst->a = tmp;
		}
	}
	pst->a[pst->top] = x;
	pst->top++;
}
//出栈
void StackPop(Stack* pst)
{
    
    
	assert(pst);
	assert(pst->top > 0);
	--pst->top;
}
int  StackSize(Stack* pst)
{
    
    
	assert(pst);
	return pst->top;
}
//返回1为空,返回0为非空
int StackEmpty(Stack* pst)
{
    
    
	assert(pst);
	return pst->top == 0 ? 1 : 0;
}
//获取栈顶的数据
DataT StackTop(Stack* pst) 
{
    
    
	assert(pst);
	assert(pst->top > 0);

	return pst->a[pst->top - 1];
}
typedef struct 
{
    
    
    //建立两个栈,用来实现队列,push用来入;pop用来出
    Stack pushSt;
    Stack popSt;
} MyQueue;

/** Initialize your data structure here. */

MyQueue* myQueueCreate() 
{
    
    
    //初始化两个栈
    MyQueue*q=(MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&q->pushSt);
    StackInit(&q->popSt);
    return q;
}

/** Push element x to the back of queue. */
//将元素 x 推到队列的末尾
void myQueuePush(MyQueue* obj, int x) 
{
    
    
    //将数据放进pushSt中即可;
    StackPush(&obj->pushSt,x);
}

/** Removes the element from in front of queue and returns that element. */
//从队列的开头移除并返回元素
int myQueuePop(MyQueue* obj) 
{
    
    
    //复用myQuenePop函数,先找到队头,然后删除;
    int frist=myQueuePeek(obj);
    StackPop(&obj->popSt);
    return frist;
}

/** Get the front element. */
//返回队列开头的元素
int myQueuePeek(MyQueue* obj) 
{
    
    
    //如果popSt不为空,则返回popSt的队头即可
    if(!StackEmpty(&obj->popSt))
    {
    
    
        return StackTop(&obj->popSt);
    }
    //如果数据都在pushSt中,则将pushSt中的数据以此放进popSt中,然后返回popSt的队头
    else
    {
    
    
        while(!StackEmpty(&obj->pushSt))
        {
    
    
            StackPush(&obj->popSt,StackTop(&obj->pushSt));
            StackPop(&obj->pushSt);
        }
        return StackTop(&obj->popSt);
    }
}

/** Returns whether the queue is empty. */
//如果队列为空,返回 true ;否则,返回 false
bool myQueueEmpty(MyQueue* obj) 
{
    
    
    //pushSt与popSt都为空时,返回1;其余情况返回0;
    return StackEmpty(&obj->popSt)&&StackEmpty(&obj->pushSt);
}

void myQueueFree(MyQueue* obj) 
{
    
    
    StackDestroy(&obj->popSt);
    StackDestroy(&obj->pushSt);
    free(obj);
}

用队列实现栈

typedef int DataT;
//用链表实现,建立队列节点
typedef struct QueneNode
{
    
    
	struct QueneNode* next;
	DataT val;

}QueneNode;
//建立队头以及队尾
typedef struct Quene
{
    
    
	QueneNode* head;
	QueneNode* tail;
}Quene;
//获取队列长度
int QueneSize(Quene* pq)
{
    
    
	assert(pq);
	int size = 0;
	QueneNode* cur = pq->head;
	while (cur)
	{
    
    
		++size; 
		cur = cur->next;
	}
    return size;
}
//初始化
void QueneInit(Quene* pq)
{
    
    
	assert(pq);
	pq->head = pq->tail = NULL;
}
//摧毁
void QueneDestory(Quene* pq)
{
    
    
	QueneNode* cur = pq->head;
	while (cur)
	{
    
    
		QueneNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}
//在队尾插入数据
void QuenePush(Quene* pq, DataT x)
{
    
    
	assert(pq);
	QueneNode* NewNode = (QueneNode*)malloc(sizeof(QueneNode));
	if (NewNode == NULL)
	{
    
    
		printf("内存不足\n");
		exit(-1);
	}
	NewNode->val = x;
	NewNode->next = NULL;
	if (pq->head == NULL)
	{
    
    
		pq->head = pq->tail = NewNode;
	}
	else
	{
    
    
		pq->tail->next = NewNode;
		pq->tail = NewNode;
	}
}
//删除队头元素
void QuenePop(Quene* pq)
{
    
    
	assert(pq);
	assert(pq->head);
	QueneNode* next = pq->head->next;
	free(pq->head);
	pq->head = next;
	if (pq->head == NULL)
	{
    
    
		pq->tail = NULL;
	}
}
//返回队头数据
DataT QueneFront(Quene* pq)
{
    
    
	assert(pq);
	assert(pq->head);
	return pq->head->val;
}
//返回队尾数据
DataT QueneBack(Quene* pq)
{
    
    
	assert(pq);
	assert(pq->head);
	return pq->tail->val;
}
//返回0是非空,返回1是空
int QueneEmpty(Quene* pq)
{
    
    
	assert(pq);
	return pq->head == NULL ? 1 : 0;
}

//用两个队列来实现栈
typedef struct
{
    
    
    Quene q1;
    Quene q2;
} MyStack;

/** Initialize your data structure here. */
//建立栈,并初始化队列q1,q2;
MyStack* myStackCreate() 
{
    
    
    MyStack*st=(MyStack*)malloc(sizeof(MyStack));
    QueneInit(&st->q1);
    QueneInit(&st->q2);
    return st;
}
//元素 x 入栈
/** Push element x onto stack. */
void myStackPush(MyStack* obj, int x) 
{
    
    
    //如果q1为非空,插入q1中
    if(!QueneEmpty(&obj->q1))
    {
    
    
        QuenePush(&obj->q1,x);
    }
    //q1为空,则不管怎样,插入q2中即可
    else
    {
    
    
        QuenePush(&obj->q2,x);
    }
}
//移除栈顶元素
/** Removes the element on top of the stack and returns that element. */
int myStackPop(MyStack* obj) 
{
    
    
    //定义一个空队列Empty,一个非空队列NoEmpty
    Quene*Empty=&obj->q1;
    Quene*NoEmpty=&obj->q2;
    if(QueneEmpty(&obj->q2))
    {
    
    
        Empty=&obj->q2;
        NoEmpty=&obj->q1;
    }
    //如果非空队列NoEmpty长度超过1,则将NoEmpty中的数据依次插入Empty中,最终NoEmpty中仅剩的元素为栈顶元素,将其删除即可。
    while(QueneSize(NoEmpty)>1)
    {
    
    
        QuenePush(Empty,QueneFront(NoEmpty));
        QuenePop(NoEmpty);
    }
    int top=QueneFront(NoEmpty);
    QuenePop(NoEmpty);
    return top;
}
//获取栈顶元素
/** Get the top element. */
int myStackTop(MyStack* obj) 
{
    
    
    if(!QueneEmpty(&obj->q1))
    {
    
    
        return QueneBack(&obj->q1);
    }    
    else
    {
    
    
        return QueneBack(&obj->q2);    
    }
}
//返回栈是否为空
/** Returns whether the stack is empty. */
bool myStackEmpty(MyStack* obj) 
{
    
    
    return QueneEmpty(&obj->q1)&&QueneEmpty(&obj->q2);
}

void myStackFree(MyStack* obj) 
{
    
    
    QueneDestory(&obj->q1);
    QueneDestory(&obj->q2);
    free(obj);
}

猜你喜欢

转载自blog.csdn.net/qq_43745617/article/details/112723982