【无标题】

用队列实现栈和用栈实现队列

用队列实现栈

typedef int QDataType;
typedef struct QListNode
{
	struct QListNode* next;
	QDataType data;
}QNode;

// 队列的结构 
typedef struct Queue
{
	QNode* front;
	QNode* rear;
}Queue;
void QueueInit(Queue* q)
{
	assert(q);
	q->front = NULL;
	q->rear = NULL;
}
// 队尾入队列 
int QueueEmpty(Queue* q)
{
	return q->front == NULL;
}
void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	newnode->data = data;
	newnode->next = NULL;
	if (q->front == NULL)
	{
		q->front = q->rear = newnode;
	}
	q->rear->next = newnode;
	q->rear = newnode;
}
// 队头出队列 
void QueuePop(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	QNode* next = q->front->next;
	free(q->front);
	q->front = next;
	if (q->front == NULL)
	{
		q->rear = NULL;
	}
}
// 获取队列头部元素 
QDataType QueueFront(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	return q->front->data;
}
// 获取队列队尾元素 
QDataType QueueBack(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	return q->rear->data;
}
// 获取队列中有效元素个数 
int QueueSize(Queue* q)
{
	assert(q);
	QNode* cur = q->front;
	int size = 0;
	while (cur)
	{
		cur = cur->next;
		size++;
	}
	return size;
}
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0 

// 销毁队列 
void QueueDestroy(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	QNode* cur = q->front;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	q->front = q->rear = NULL;
}
void QueuePrint(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	QNode* cur = q->front;
	while (cur)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");

}

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


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

}

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

}

int myStackPop(MyStack* obj) {
    Queue *nonemptyQ=&obj->q2;
    Queue*emptyQ=&obj->q1;
    if(!QueueEmpty(&obj->q1))
    {
        nonemptyQ=&obj->q2;
        emptyQ=&obj->q2;
    }
    while(QueueSize(nonemptyQ)>1)
    {
        QueuePush(emptyQ,QueueFront(nonemptyQ));
        QueuePop(nonemptyQ);
    }
    int top=QueueFront(nonemptyQ);
    QueuePop(nonemptyQ);
    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);

}

用栈实现队列

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



void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}
void StackPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		//扩容
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = realloc(ps->a, sizeof(STDataType) * newcapacity);
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		else
		{
			ps->a = tmp;
			ps->capacity = newcapacity;

		}
	}
	ps->a[ps->top] = x;
	ps->top++;
}
void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}
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)
{
	assert(ps);
	while (!StackEmpty(ps))
	{
		printf("%d ", StackTop(ps));
		StackPop(ps);
	}
}




typedef struct {
    ST PushST;
    ST PopST;

} 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->PopST)&&StackEmpty(&obj->PushST);

}

void myQueueFree(MyQueue* obj) {
    StackDestroy(&obj->PushST);
    StackDestroy(&obj->PopST);
    free(obj);

}

循环队列

typedef struct {
    int *a;
    int front;
    int rear;
    int k;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
     MyCircularQueue *q=( MyCircularQueue*)malloc(sizeof( MyCircularQueue));
     q->a=(int*)malloc(sizeof(int)*(k+1));
     q->front=q->rear=0;
     q->k=k;
     return q;
}

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

}

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

}

int myCircularQueueFront(MyCircularQueue* obj) {
    if((obj->front)==(obj->rear))
    {
        return -1;
    }
    return obj->a[obj->front];


}

int myCircularQueueRear(MyCircularQueue* obj) {
    if((obj->front)==(obj->rear))
    {
        return -1;
    }
    if(obj->rear==0)
    {
        return obj->a[obj->k];
    }
    else
    {
        return obj->a[obj->rear-1];
    }

}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return (obj->front)==(obj->rear);

}

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

}

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


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值