栈模拟队列(C)


前言

大二,刚刚开始学数据结构与算法,写得不好。。。。


栈模拟队列

使用栈实现队列的下列操作:
1.init():初始化队列
2.push(x):将1个元素放入队列的尾部。
3.pop(): 从队列首部移除元素。
4.display():展示队列元素。

#include <stdlib.h>
#include <stdio.h>

typedef struct stack {
    int data;
    struct stack *next;
} Stack;

Stack *Init(int n) {
    Stack *top = NULL;
    int i;
    Stack *node;
    for (i = 1; i < n+1; i++) {
        node = (Stack *) malloc(sizeof(Stack));
        node->data = i;
        node->next = top;
        top = node;
        printf("入队: %d\n",node->data);
    }
    return top;
}

void Display(Stack *top) {
    Stack *p = top;
    while (p->next != NULL) {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("%d ", p->data);
    printf("\n");
}

Stack *Push(Stack *top, int data) {
    Stack *node = (Stack *) malloc(sizeof(Stack));
    node->data = data;
    node->next = top;
    top = node;
    printf("入队: %d\n",top->data);
    return top;
}

Stack *Pop(Stack *top) {
    Stack *p = top;
    printf("出队:%d\n",p->data);
    top = top->next;
    free(p);
    return top;
}
Stack * Queue(Stack *top,int data){
    Stack *node = (Stack *) malloc(sizeof(Stack));
    node->data = data;
    node->next = top;
    top = node;
    return top;
}
int main() {
    Stack *z1, *z2,*p;
    z1 = Init(10);
    z2 = Init(0);
    p=z1;
    while (p->next!=NULL){
        z2=Queue(z2,p->data);
        p=p->next;
    }
    z2=Queue(z2,p->data);
    z2=Pop(z2);
    z2=Pop(z2);
    z2=Pop(z2);
    Display(z2);
}

总结

就离谱 算法也是很难。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用两个模拟队列,具体实现如下: 1. 定义两个 Stack1 和 Stack2,用 Stack1 来存放入队的元素,用 Stack2 来处理出队操作。 2. 入队操作:将元素压入 Stack1。 3. 出队操作:首先判断 Stack2 是否为空,如果不为空,则弹出顶元素;如果为空,将 Stack1 中的元素依次出并压入 Stack2,再弹出顶元素。 下面是用 C 语言实现的代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 // 定义结构体 typedef struct { int data[MAX_SIZE]; int top; } Stack; // 初始化 void initStack(Stack *s) { s->top = -1; } // 判断是否为空 int isStackEmpty(Stack *s) { return s->top == -1; } // 判断是否已满 int isStackFull(Stack *s) { return s->top == MAX_SIZE - 1; } // 入 void push(Stack *s, int value) { if (isStackFull(s)) { printf("Stack overflow!\n"); exit(1); } s->data[++s->top] = value; } // 出 int pop(Stack *s) { if (isStackEmpty(s)) { printf("Stack underflow!\n"); exit(1); } return s->data[s->top--]; } // 定义队列结构体 typedef struct { Stack s1; Stack s2; } Queue; // 初始化队列 void initQueue(Queue *q) { initStack(&q->s1); initStack(&q->s2); } // 入队 void enqueue(Queue *q, int value) { push(&q->s1, value); } // 出队 int dequeue(Queue *q) { int value; if (isStackEmpty(&q->s2)) { while (!isStackEmpty(&q->s1)) { value = pop(&q->s1); push(&q->s2, value); } if (isStackEmpty(&q->s2)) { printf("Queue underflow!\n"); exit(1); } } return pop(&q->s2); } int main() { Queue q; initQueue(&q); enqueue(&q, 10); enqueue(&q, 20); enqueue(&q, 30); printf("%d\n", dequeue(&q)); printf("%d\n", dequeue(&q)); printf("%d\n", dequeue(&q)); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值