#define Max 100 struct student {char name[10]; char sex[10]; int age; int score; }b;
typedef struct node {
struct student information[M]; int top,bot; }SeqStack; SeqStack S;
void makempty(SeqStack *S) {
(*S).bot=0; (*S).top=-1; }
int empty(SeqStack *S) {
if(S->top==-1) return 1; else return 0; }
void insertyuansu() {
printf(\"请输入姓名,性别,年龄,分数\\n\"); scanf(\"%s\ scanf(\"%s\ scanf(\"%d\ scanf(\"%d\}
int push(SeqStack *S) {
if((*S).top==(M-1)) return 0; else {
insertyuansu(); (*S).top++;
(*S).information[(*S).top]=b; return 1; } }
int pop(SeqStack *S) {
if(empty(S)) return 0; else {
(*S).top--; return 1; } }
int gettop(SeqStack *S,struct student *m) {
if(empty(S)) return 0; else {
*m=S->information[(*S).top]; return 1; } }
void print(struct student *m) {
printf(\"栈顶元素信息为 \\n\"); printf(\" 学生姓名 \"); puts((*m).name);
printf(\" 学生性别 \"); puts((*m).sex);
printf(\" 学生年龄 \"); printf(\"%d\\n\ printf(\" 学生得分 \");
printf(\"%d\\n\ printf(\"\\n\"); }
void menu() {
printf(\" 选择选项对应相应操作\\n\");
printf(\" 入栈 ************ 1\\n\"); printf(\" 出栈 ************ 2\\n\"); printf(\" 访问栈顶元素 ************ 3\\n\"); printf(\" 结束操作 ************ 4\\n\"); }
void main() {
int xuanze;
struct student m; makempty(&S); menu(); while(1) {
printf(\"\\n输入操作选项\\n\"); printf(\"操作选项为\"); scanf(\"%d\ if(xuanze==1) {
if(push(&S)) printf(\"入栈成功\\n\"); else printf(\"栈满溢出,入栈失败\\n\"); }
if(xuanze==2) {
if(pop(&S)) printf(\"出栈成功\\n\"); else printf(\"栈空,出栈失败\\n\"); }
if(xuanze==3) {
if(gettop(&S,&m)) {
printf(\"访问栈顶元素成功\\n\"); print(&m); }
else printf(\"栈空,访问栈顶元素失败\\n\"); }
if(xuanze==4) break; } } 链栈
#include #include typedef struct Node { int data; struct Node *next; }StackNode;StackNode *Creat()//创建头节点 { StackNode *top; top=(StackNode *)malloc(sizeof(StackNode)); top->next=NULL; return top; }
void Push(StackNode *s)//创建,头插法 { StackNode *p; int x; printf(\"输入数据,以-1结束: \"); scanf(\"%d\ while(x!=-1) { p=(StackNode *)malloc(sizeof(StackNode)); p->data=x; p->next=s->next; s->next=p;
printf(\"输入下一个数据: \"); scanf(\"%d\ } }
void Empty(StackNode *s)//判断是否栈空 { if(s->next==NULL) printf(\"该栈为空!\"); else printf(\"该栈不为空\"); }
int Pop(StackNode *s)//出栈 { int *y; StackNode *p; if(s->next==NULL) { printf(\"该栈为空,不可出栈!\"); return 0; } else { *y=s->next->data;//传回栈顶结点的数据 printf(\"%d\ p=s->next; s->next=p->next;
free(p);
return 1; } }
void main() { StackNode *top; top=Creat(); Push(top); Pop(top); }