【输入形式】 输入的第一行为队列元素个数,第二行为队列从首至尾的元素
【输出形式】 输出队列的逆置
【样例输入】 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;
}