程序填空题:使用栈将输入序列逆序输出
要求通过栈实现输入序列的逆序输出
```c
#include
#include
typedef int DataType;
struct Node {
DataType data;
struct Node* next;
};
typedef struct Node *PNode;
typedef struct Node *LinkStack;
LinkStack SetNullStack_Link()
{
LinkStack top = (LinkStack)malloc(sizeof(struct Node));
if (top != NULL) top->next = NULL;
else printf("Alloc failure");
return top;
}
int IsNullStack_link(LinkStack top)
{
if (top->next == NULL)
return 1;
else
return 0;
}
void Push_link(LinkStack top, DataType x)
{
PNode p;
p = (PNode)malloc(sizeof(struct Node));
if (p == NULL)
printf("Alloc failure");
else
{
p->data = x;
p->next = top->next;
top->next = p;
}
}
void Pop_link(LinkStack top)
{
PNode p;
if (top->next == NULL)
printf("it is empty stack!");
else
{
p = top->next;
top->next = p->next;
free(p);
}
}
DataType Top_link(LinkStack top)
{
if (top->next == NULL)
{
printf("It is empty stack!");
return 0;
}
else
return top->next->data;
}
int main()
{
LinkStack stackA = SetNullStack_Link();
DataType data;
scanf("%d,", &data);
while (data != -1)
{
@@[Push_link(stackA, data)](2)
scanf("%d,", &data);
}
while (@@[!IsNullStack_link(stackA)](3))
{
@@[printf("%d,", Top_link(stackA))](3)
@@[Pop_link(stackA)](2)
}
return 0;
}
```
答案:
第1空:Push_link(stackA, data)
第2空:!IsNullStack_link(stackA)
第3空:printf("%d,", Top_link(stackA))
第4空:Pop_link(stackA)
```c
#include
#include
typedef int DataType;
struct Node {
DataType data;
struct Node* next;
};
typedef struct Node *PNode;
typedef struct Node *LinkStack;
LinkStack SetNullStack_Link()
{
LinkStack top = (LinkStack)malloc(sizeof(struct Node));
if (top != NULL) top->next = NULL;
else printf("Alloc failure");
return top;
}
int IsNullStack_link(LinkStack top)
{
if (top->next == NULL)
return 1;
else
return 0;
}
void Push_link(LinkStack top, DataType x)
{
PNode p;
p = (PNode)malloc(sizeof(struct Node));
if (p == NULL)
printf("Alloc failure");
else
{
p->data = x;
p->next = top->next;
top->next = p;
}
}
void Pop_link(LinkStack top)
{
PNode p;
if (top->next == NULL)
printf("it is empty stack!");
else
{
p = top->next;
top->next = p->next;
free(p);
}
}
DataType Top_link(LinkStack top)
{
if (top->next == NULL)
{
printf("It is empty stack!");
return 0;
}
else
return top->next->data;
}
int main()
{
LinkStack stackA = SetNullStack_Link();
DataType data;
scanf("%d,", &data);
while (data != -1)
{
@@[Push_link(stackA, data)](2)
scanf("%d,", &data);
}
while (@@[!IsNullStack_link(stackA)](3))
{
@@[printf("%d,", Top_link(stackA))](3)
@@[Pop_link(stackA)](2)
}
return 0;
}
```
答案:
第1空:Push_link(stackA, data)
第2空:!IsNullStack_link(stackA)
第3空:printf("%d,", Top_link(stackA))
第4空:Pop_link(stackA)