| 发表于:2007-06-21 22:41:43 楼主 |
#include <stdlib.h> struct list { int data; struct list *next; }; typedef struct list stack; typedef stack* link; link top=null; /*将数据压入堆栈*/ void push(int item) { link new_node; new_node=(link)malloc(sizeof(stack)); new_node-> data=item; top=new_node; new_node-> next=top; } int pop() { int temp; link ding; if(top!=null) { ding=top; top=top-> next; temp=ding-> data; free(ding); return temp; } else return -1; } void main() { int temp; int select; link point=null; printf( "\n1.input:\n "); printf( "2.output\n "); printf( "exit\n "); scanf( "%d ",&select); do { switch(select) { case 1: printf( "input a data: "); scanf( "%d ",&temp); push(temp); point=top; while(point!=null) { printf( "%d\n ",point-> data); point=point-> next; } break; case 2: if((temp=pop())==-1) printf( "the list is empty\n "); else { printf( "the output is %d ",temp); point=top; while(point!=null) { printf( "the afterdata is%d\n ",point-> data); point=point-> next; } } break; } printf( "\n1.input:\n "); printf( "2.output\n "); printf( "exit "); scanf( "%d ",&select); }while(select!=3); printf( "\nthank you\n "); system( "pause "); } |
|
|
|
|