《《算法与数据结构》 项目实训3.docx》由会员分享,可在线阅读,更多相关《《算法与数据结构》 项目实训3.docx(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、/*顺序栈的应用*/#includes j. h /*调用自定义库函数*/typedef structElemType elemMAXSIZE;int top;SeqStack; /*定义顺序栈结构*/void InitStack Sq(SeqStack *s) /*初始化栈操作*/(S-top=-l; /*InitStack_sq*/Status Empty_Sq (SeqStack *s) /*判栈是否为空*/(return (s-top=-l); /*Empty_sq*/Status PushSeqStack(SeqStack *s, ElemType x)(if (s-top=MAXS
2、IZE-l)return OVERFLOW ; /* 栈满不能入栈 */else(s-top+;s-elems-top=x ;return OK;Status Pop SeqStack(SeqStack *s, ElemType *y)if (Empty_Sq(s)return OVERFLOW; /* 栈空不能出栈 */ else (*y=s-elems-top;s-top一;return OK; /*栈顶元素存入*y,返回*/ ElemType GetTop (SeqStack *s, ElemType *y) /*取顶元素*/ (if(Empty_Sq(s)return OVERFLOW
3、; /* 栈空不能出栈 */ else (*y=s-elems-top;return OK;/*SQstack, h*/*主函数,用于输出菜单及功能调用*/void main ()int sei;do(printf (,?ttt 顺序栈的应用n);printf (n);1+ I +vlx y-v |nj 1 f 1 L11 l不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不),printf (tttl:栈的初始化 n);printf (ttt2:置空栈n);printf (ttt3:入栈n);printf (ttt4:出栈 n);printf
4、 (z,ttt5 取栈顶元素 n);printf (tttO:退出:n);v k n v ( 4 !zx!zxlz !z!z!zx!zxlz !z!z !zlz y-、x 1 f 1 L,11 l不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不),scanf(d,&sel);switch (sei)(case 1: InitStack_Sq ();break;Empty_Sq ();break;Push_SeqStack ();break;case 4: Pop_SeqStack ();break;GetTopO ;Break;exit ()
5、;) while (sei!=0);/*链栈的应用*/Alstack. h*/#includesj. h /*调用自定义库函数*/ typedef struct nodeElemType data;struct node *next;StackNode;typedef structstruct node *top;LinkStack;void Init_LS (LinkStack *ls) /*置空链栈 */(ls-top=NULL;)Status Empty_LS(LinkStack *ls) /*判栈空 */(return ls-top=NULL;void Push LS(LinkStac
6、k *ls, ElemType x) /*入栈 */StackNode *p=(StackNode *)malloc(sizeof (StackNode);p-data=x;p-next=ls-top; /*将元素x插入链栈顶*/ ls-top=p ;Status Pop_LS(LinkStack *ls, ElemType *y) /* 出栈*/(StackNode *p;if (Empty_LS(Is)printf (下溢!);return OVERFLOW;else *y=ls-top-data ;p=ls-top ;ls-top=p-next;free (p);return OK ;
7、Status GetTop(LinkStack *ls, ElemType *y)/*取栈顶元素 */(if ( Empty_LS(Is)printf (下溢!”);return OVERFLOW; else (*y=ls-top-data;return OK ;/*主函数,用于输出菜单及功能调用*/void main ()(int sei;doprintf (z/ttt链栈的应用n);w i 4- -I- -4- v i P】JL 1 L1 不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不不),printf (tttl:栈的初始化 n);pr
8、intf (ttt2:置空栈n);printf (ttt3:入栈n);printf (ttt4:出栈 n);printf (z,ttt5 取栈顶元素 n);printf (tttO:退出:n);s; + r ( 4丈丈丈丈丈业土/丈土土”丈丈丈丈丈丈丈丈土土“业必&土土丈丈丈丈丈丈丈丈丈丈丈土土/&土 h i )111 L, !不不不不下不不不不不下不不不不不不不不不不不不下不不不不不下不不不不不不不不不不不不下不不不不不下不),scanf(d,&sel);switch (sei)case 1: InitStackLS ();break;Empty_LS ();break;Push_LS ();break;Pop_LS ();break;case 5:GetTop ();Break;case 0:exit (); while (sei!=0);