以下为相关代码:
#include <stdio.h> #include <stdlib.h> #define MAX 100 typedef struct Node { int data; int next; }Node; void push(Node A[]) { for (int i = 0; i < 3; i++) { A[i].next = 0; } for (int i = 3; i < MAX; i++) { A[i].next = i + 1; } A[MAX - 1].next = 0; for (int i = 0; i < MAX; i++) { A[i].data = 0; } int top = 3; for (int i = 0; i < MAX; i++) { int j = top; int t; char result; printf("请输入入栈数字:"); scanf("%d",&t); getchar(); top++; A[j].data = t; if ( t < 80 ) { A[j].next=A[0].next; A[0].next=j; } else if ( t > 100 ) { A[j].next=A[2].next; A[2].next=j; } else if ( t <= 100 && t >= 80 ) { A[j].next=A[1].next; A[1].next=j; } printf("还想继续输入吗?(t--继续,f--停止)"); scanf("%c",&result); if (result == 't') { continue; } else { break; } } } void pop(Node A[]) { int i; printf("\n你要出第几个栈?"); scanf("%d",&i); printf("出栈元素:"); int j = A[i - 1].next; printf("%d\t",A[j].data); while (A[j].next != 0) { j = A[j].next; printf("%d\t",A[j].data); } printf("\n"); } int main() { Node A[MAX]; push(A); pop(A); system("pause"); return 0; }