队列元素逆置_数据结构(队列)c语言

【输入形式】 输入的第一行为队列元素个数,第二行为队列从首至尾的元素

【输出形式】 输出队列的逆置

【样例输入】 3 1 2 3

【样例输出】 3 2 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100
typedef struct Node{
    int data;
    struct Node *next;
}Qnode;
typedef struct Queue{
    Qnode *front;
    Qnode *rear;
}LinkQueue;
typedef struct
{
    int  *top;
    int  *base;
    int stacksize;
}SqStack;
void NewStack(SqStack *S)
{
    S->base = (SqStack *)malloc(N * sizeof(SqStack));
    if(!S->base)
        exit(-1);
    S->top = S->base;
    S->stacksize = N;
}
void Push(SqStack *S, int e)
{
    if(S->top - S->base >= S->stacksize)
    {
        int size;
        size =(S->stacksize+N)*sizeof(SqStack);
        S->base = (SqStack *)realloc(S->base, size);
        if(!S->base)
           exit(-1);
        S->top = S->base + S->stacksize;
        S->stacksize = S->stacksize + N;
    }
    *S->top++= e;
}
int StackEmpty_Sq(SqStack *s)
{
    if(s->base == s->top)
        return -1;
    else
        return 0;
}
int StackLength_Sq(SqStack *s)
{
    return s->top - s->base;
}
int ClearStack_Sq(SqStack *s)
{
    if(s->base != NULL)
        s->base = s->top;
    return -1;
}
void DestroyStack(SqStack *s)
{
    if(s->base != NULL)
    {
        free(s->base);
        s->stacksize = 0;
        s->base = s->top = NULL;
    }
}
void Pop(SqStack *s, int *x)
{
    --s->top;
    *x = *s->top;
}
int putlength(SqStack *s)
{
    int i = 0;
    int *base;
    base = s->base;
    while(s->base<s->top)
    {
        s->base++;
        i++;
    }
    s->base = base;
    return i;
}
void IntoQnode(LinkQueue *p)
{
    p->front = p->rear = NULL;
}
void empty(LinkQueue *p)
{
    if(p->front == p->rear)
    {
        printf("no");
    }
    else
        printf("ok");
}
void PushNode(LinkQueue *p, int x)
{
    Qnode *s = (Qnode*)malloc(sizeof(Qnode));
    s->data = x;
    s->next = NULL;
    if(p->front == NULL)
        p->front = p->rear = s;
    else
    {
         p->rear->next = s;
         p->rear = s;
    }
}
void PutLength(LinkQueue *p, int *n)
{
    Qnode *s;
    s = p->front;
    while(s!= NULL)
    {
        *n = *n + 1;
        s = s->next;
    }
}
void PopNode(LinkQueue *p, int *x)
{
    Qnode *s;
    if(p->front != p->rear)
    {
        s = p->front;
        p->front = s->next;
        *x = s->data;
        free(s);
        if(p->front== NULL)
        {
            p->front = p->rear;
        }
    }
}
void put(LinkQueue *p, int *x)
{
    Qnode *s;
    s = p->front;
    p->front = p->front->next;
    *x = s->data;
}
int main()
{
    LinkQueue *p = (LinkQueue*)malloc(sizeof(LinkQueue));
    SqStack *q = (SqStack*)malloc(sizeof(SqStack));
    IntoQnode(p);
    NewStack(q);
    int i, n, x, m;
    scanf("%d", &m);
    for(i = 0; i < m; i++)
    {
        scanf("%d", &n);
        char ch = getchar();
        PushNode(p, n);
        if(ch != ' ')
            break;
    }
    n = 0;
    PutLength(p, &n);
    for(i = 0; i < n; i++)
    {
         put(p, &x);
         Push(q, x);
    }
    for(i = 0; i < n; i++)
    {
        Pop(q, &x);
        printf("%d ", x);
    }
/*    for(i = 0; i < n; i++)
    {
        PopNode(p, &x);
        Push(q, x);
    }
    for(i = 0; i < n; i++)
    {
        Pop(q, &x);
        printf("%d ", x);
    }*/
    return 0;
}

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星纪@大梁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值