数据结构栈和队列OJ题

225. 用队列实现栈 - 力扣(LeetCode)

#define DataType int
#include
typedef struct QueueNode
{
    struct QueueNode*next;
    DataType data;
}QueueNode;
typedef struct Queue
{
    QueueNode *head;
    QueueNode *tail;
}Queue;
void QueueInit(Queue* pq);
//销毁,挨个遍历销毁。注意两个指针最后别为野了
void QueueDestroy(Queue* pq);
//尾插,别忘了插入的时候不仅tail的next要指向新节点,tail还要指向新节点
void QueuePush(Queue* pq, DataType x);
//注意断言为空,注意当头指针为空的时候那时候就是空了,还要置尾指针为空,否则尾指针会是野指针
void QueuePop(Queue* pq );
//判断是否为空,直接返回pq->head==0;即可
bool QueueEmpty(Queue* pq);
//这两个取元素注意断言为空
DataType QueueFront(Queue* pq);
DataType QueueBack(Queue* pq);
int QueueSize(Queue* pq);
void QueueInit(Queue *pq)
{   assert(pq);
    pq->head=pq->tail=NULL;
}
bool QueueEmpty(Queue*pq)
{   assert(pq);
    return pq->head==NULL;
}
void QueuePush(Queue *pq,DataType x)
{   
    QueueNode *newnode=(QueueNode*)malloc(sizeof(QueueNode));
    newnode->data=x;
    newnode->next=NULL;
    if(pq->head==NULL)
    {
        pq->head=pq->tail=newnode;
    }
    else
    {
        pq->tail->next=newnode;
        pq->tail=newnode;
    }
}
DataType QueueBack(Queue*pq)
{   assert(pq);
    assert(pq->tail);
    return pq->tail->data;
}
int QueueSize(Queue*pq)
{
    int size=0;
    QueueNode *cur=pq->head;
    while(cur)
    {
        cur=cur->next;
        size++;
    }
    return size;
}
//
void QueuePop(Queue *pq)
 {
    assert(!QueueEmpty(pq));
    QueueNode*temp=pq->head;
    pq->head=pq->head->next;
    free(temp);
    if(pq->head==NULL)
    pq->tail=NULL;

}
DataType QueueFront(Queue* pq)
{   assert(pq);
    assert(!QueueEmpty(pq));
    return (pq->head)->data;
}
void QueueDestroy(Queue *pq)
{   
    QueueNode*cur=pq->head;
    while(cur)
    {
        QueueNode*temp=cur;
        cur=cur->next;
        free(temp);
        temp=NULL;
    }
    pq->tail=pq->head=NULL;
}















typedef struct {
    Queue q1;
    Queue q2;
} MyStack;


MyStack* myStackCreate() {
    MyStack *pst=(MyStack*)malloc(sizeof(MyStack));
    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) {
    assert(obj);
    Queue* emptyQ = &obj->q1;//假设q1为空,q2不为空
    Queue* nonEmptyQ = &obj->q2;
    if (!QueueEmpty(&obj->q1))
    {
        emptyQ = &obj->q2;
        nonEmptyQ = &obj->q1;
    }
    while (QueueSize(nonEmptyQ) > 1)
    {
        QueuePush(emptyQ, QueueFront(nonEmptyQ));
        QueuePop(nonEmptyQ);
    }
    int top = QueueFront(nonEmptyQ);
    QueuePop(nonEmptyQ);//记得pop掉
    return top;
    // Queue*empty=&obj->q1;
    // Queue*noempty=&obj->q2;
    // if(!QueueEmpty(&obj->q1))
    // {
    //     empty=&obj->q2;
    //      noempty=&obj->q1;
    // }
    // while(QueueSize(noempty)>1)
    // {
    //     QueuePush(empty,QueueFront(noempty));
    //     QueuePop(noempty);
    // }
    // int top=QueueFront(noempty);
    // QueuePop(noempty);
    // return top;
    
}

int myStackTop(MyStack* obj) {
    if(!QueueEmpty(&obj->q1))
    {//q1不空
        return QueueBack(&obj->q1);

    }
    else
    {//q2不空
        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);
    obj=NULL;

}

这道题写出来个Bug找了一个多小时,就是判断队列为空的时候

return 了 pq->head==NULL;就因为少写一个==

还有这个

数据结构栈和队列OJ题_第1张图片

 这种假设思想学一下。先假设一个为空另一个不为空

如果(if)不满足的话调一下。


622. 设计循环队列 - 力扣(LeetCode)

typedef struct {
    int *arr;
    int tail;
    int front;
    int k;
} MyCircularQueue;

bool myCircularQueueIsEmpty(MyCircularQueue* obj);
bool myCircularQueueIsFull(MyCircularQueue* obj);
MyCircularQueue* myCircularQueueCreate(int k) {
    MyCircularQueue*cq=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    cq->arr=(int *)malloc(sizeof(int)*(k+1));
    cq->tail=0;
    cq->front=0;
    cq->k=k;
    return cq;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if(myCircularQueueIsFull(obj))
    return false;
    obj->arr[obj->tail]=value;
    obj->tail=(obj->tail+1)%(obj->k+1);
    return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj))
    return false;
    obj->front=(obj->front+1)%(obj->k+1);
    return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj))
    return -1;
    return obj->arr[obj->front];
}

int myCircularQueueRear(MyCircularQueue* obj) {
     if(myCircularQueueIsEmpty(obj))
    return -1;
    return obj->arr[(obj->tail-1+obj->k+1)%(obj->k+1)];
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    if(obj->front==obj->tail)
    return true;
    else
    return false;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
    return (obj->tail+1)%(obj->k+1)==obj->front;
}

void myCircularQueueFree(MyCircularQueue* obj) {
    free(obj->arr);
    free(obj);
}

232. 用栈实现队列 - 力扣(LeetCode)

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include
#include
#define DataType int
typedef struct Stack
{
	DataType* arr;
	int top;
	int capacity;
}ST;
void StackInit(ST* ps);
void StackDestroy(ST* ps);
void StackPush(ST* ps, DataType x);
void StackPop(ST* ps);
DataType StackTop(ST* ps);
int StackSize(ST* ps);
bool StackEmpty(ST* ps);
void StackInit(ST* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}
void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->arr);
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}
void StackPush(ST* ps, DataType x)
{
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		DataType* temp = (DataType*)realloc(ps->arr,sizeof(DataType) * newcapacity);
		//realloc返回的是新开辟的头地址
		if (temp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->capacity = newcapacity;
		ps->arr = temp;
	}
	ps->arr[ps->top++] = x; 
}
void StackPop(ST* ps)
{
	assert(ps);
	assert(ps->top>0);
	ps->top--;
}
DataType StackTop(ST* ps)
{
	assert(ps);
	assert(ps->top >0);
	//等于0表示已经为空了
	return ps->arr[ps->top - 1];
}
int StackSize(ST* ps)
{	
	assert(ps);
	return ps->top;
}
bool StackEmpty(ST* ps)
{
	assert(ps);
	if (ps->top == 0)
		return true;
	else
		return false;


	//或者
	/*return ps->top == 0;*/
	//     ==返回的是true 和false
}





typedef struct {
    ST popST;
    ST pushST;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue*q=(MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&q->popST);
    StackInit(&q->pushST);
    return q;
}

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

int myQueuePop(MyQueue* obj) {

    if(StackEmpty(&obj->popST))
    {
        while(!StackEmpty(&obj->pushST))
        {
            StackPush(&obj->popST,StackTop(&obj->pushST));
            StackPop(&obj->pushST);
        }
    }
    int top=StackTop(&obj->popST);
    StackPop(&obj->popST);
    return  top;
}

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

bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(&obj->pushST)&&StackEmpty(&obj->popST);
}

void myQueueFree(MyQueue* obj) {
    StackDestroy(&obj->popST);
       StackDestroy(&obj->pushST);
       free(obj);
}

你可能感兴趣的:(数据结构,算法,数据结构)