openjudge data structure exercise set reverse output integer sequence

Reverse output sequence of integers

Total time limit: 
500000ms
Memory limit: 
65535000kB
describe

Enter a sequence of integers (non-negative integers, only positive integers and 0). The sequence ends with -1  . Ask to output this sequence of positive integers in reverse .

enter
A sequence of integers, separated by spaces, non-negative integers, containing only positive integers and 0. -1 means end of input.
output
Reverse output the sequence of integers in the input file.
sample input
3 127 64 1991 -1
Sample output

1991 64 127 

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

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20 /* Initial allocation of storage space*/

typedef int Status;
typedef int SElemType;

typedef struct StackNode
{
    SElemType data;
    struct StackNode *next;
}StackNode,*LinkStackPtr;

typedef struct
{
    LinkStackPtr top;
    int count;
}LinkStack;

Status InitStack(LinkStack *S)
{
    S->top = (LinkStackPtr)malloc(sizeof(StackNode));
    if(!S->top)
        return ERROR;
    S->top=NULL;
    S->count=0;
    return OK;
}

Status StackEmpty(LinkStack S)
{
    if (S.count==0)
        return TRUE;
    else
        return FALSE;
}


Status Push(LinkStack *S,SElemType e)
{
    LinkStackPtr s=(LinkStackPtr)malloc(sizeof(StackNode));
    s->data=e;
    s->next=S->top; /* Assign the current top element of the stack to the immediate successor of the new node*/
    S->top=s; /* assign the new node s to the stack top pointer */
    S->count++;
    return OK;
}

Status Pop(LinkStack *S,SElemType *e)
{
    LinkStackPtr p;
    if(StackEmpty(*S))
        return ERROR;
    *e=S->top->data;
    p=S->top; /* Assign the top node of the stack to p, as shown in the figure ③ */
    S->top=S->top->next; /* Make the stack top pointer move down one bit to point to the next node*/
    free(p); /* free node p */
    S->count--;
    return OK;
}

Status visit(SElemType c)
{
    printf("%d ",c);
    return OK;
}

Status StackTraverse(LinkStack S)
{
    LinkStackPtr p;
    p=S.top;
    while(p)
    {
        visit(p->data);
        p=p->next;
    }
    printf("\n");
    return OK;
}

intmain()
{
    int m,r;
    you are;
    scanf("%d",&m);
    LinkStack s;
    InitStack(&s);
    while(m>=0)
    {
        Push(&s,m);
        scanf("%d",&m);
    }
    while(!StackEmpty(s))
    {
        Pop(&s,&e);
        printf("%d ",e);
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324839329&siteId=291194637