|
发表于 2013-6-10 00:45:40
|
显示全部楼层
本帖最后由 fanki 于 2013-6-10 00:53 编辑
你是不是想这样吖?我只帮你改了改pop的函数
- #include<stdio.h>
- #include<stdlib.h>
- #define MAX 100
- typedef struct tree
- {
- int data[MAX];
- int last;
- }wtree;
- typedef struct
- {
- int data[MAX];
- int top;
- }stack;//栈结构
- stack Nstack()
- {
- stack N;
- //初始化的话,stack N = { 0 };//这样会不会好一点呢?
- N.top = 0;
- N.data[N.top] = 0;
- return N;
- }
- //入栈 这个函数你是通过返回值去回结构体,所以不用传指针
- stack push(stack N,int x)
- {
- N.top++;
- N.data[N.top] = x;
- return N;
- }
- //出栈 这里如果不用指针的话,函数属于值传递,本来的值不会改变,就会导致pop没有拿出来而死循环
- int pop(stack* N)
- {
- int x;
- x = N->data[N->top];
- N->top--;
- return x;
- }
- wtree createtree()
- {
- wtree T = { 0 };
- T.last = 0;
- return T;
- }
- void deal( wtree T )
- {
- stack N = Nstack();
- int i = 1;
- while( T.data[i] !=0 )
- {
- if( T.data[2*i+1] != 0 )
- i = 2 * i + 1;
- else if( T.data[2*i] != 0 )
- i = 2 * i;
- else
- {
- N = push( N, T.data[i] );
- T.data[i] = 0;
- i = i / 2;
- }
- }
- while( N.top != 0 )
- {
- //pop这里传stack的地址,就可以改变它的值。
- printf( "%d\t", pop( &N ) );
- }
- }
- void main()
- {
- wtree T = createtree();
- int i = 0, j = 1, x;
- printf( "please input how many member:");
- scanf("%d", &x);
- T.last = 2 * x + 1;
- while( i <= T.last )
- {
- T.data[i] = 0;
- i++;
- }
- while( j <= x )
- {
- scanf( "%d", &T.data[j] );
- j++;
- }
- deal(T);
- }
复制代码
希望对你有帮助,一起学习,加油加油。{:7_155:}
|
|