用顺序栈的5种运算使所有的硬座车厢排列到所有的软座车厢的前面。

[复制链接]
查看11 | 回复1 | 2009-8-26 16:55:41 | 显示全部楼层 |阅读模式
main(){int a[N]={0,1,0,1,0,0,1,0,1,1};//0硬座,1软卧int Temp,b[N],j=0;for(int i=0;i#include //定义栈及其操作#define MAXSIZE 20typedef int ElemType;typedef struct{ ElemType stack[MAXSIZE]; int top;//栈顶指针} st_Stack;void initstack(st_Stack *S){ S->top=-1;}void push(st_Stack *S,ElemType x){ if ( S->top == (MAXSIZE-1) ) {printf("栈上溢出!\n");return; } else {S->top++;S->stack[S->top]=x;}}void pop(st_Stack *S){ if (S->top==-1) {printf("栈下溢出!\n");} else {S->top--; }}ElemType gettop(st_Stack *S){ if (S->top==-1) {printf("栈空!\n");return NULL; } else {return(S->stack[S->top]); }}void clear(st_Stack* S){ int i = 0; for (; i top; i++) {S->stack = 0; } S->top = -1;}int empty(st_Stack *S){ if (S->top==-1) return(1); else return(0);}void display(st_Stack *S){ int i; printf("栈中元素:"); for (i=S->top;i>=0;i--)printf("%d, ",S->stack);// printf("\n");}//功能:重排所有车厢void func(st_Stack* S){ ElemType aTempY[MAXSIZE]; ElemType aTempR[MAXSIZE]; int n=0,m=0; int i = 0; for (; i top; i++) {if (S->stack == 1){ aTempY[m++] = S->stack;}if (S->stack == 2){ aTempR[n++] = S->stack;} } clear(S); for (i = 0; i < m; i++) {push(S, aTempY); } for (i = 0; i < n; i++) {push(S, aTempR); }}//存储约定:(或枚举)1硬座,2软座,3硬卧,...int main(){ st_Stack s1; initstack(&s1); push(&s1,1); push(&s1,2); push(&s1,2); push(&s1,1); push(&s1,1); push(&s1,2); push(&s1,2); push(&s1,1); push(&s1,2); push(&s1,2); display(&s1); func(&s1); display(&s1); return 0;}
回复

使用道具 举报

千问 | 2009-8-26 16:55:41 | 显示全部楼层
繁琐的小程序。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行