数据结构方面的两个程序 很简单哈,呵呵
/*这是个堆栈操作*/
#include "stdio.h"
#include "alloc.h"
typedef struct node{
int data;
struct node *link;
}NODE;
NODE * top="NULL";
push(int i)
{NODE * p;
p=(NODE *)malloc(sizeof(NODE));
p->data=i;
p->link=top;
top="p";
}
main()
{NODE * p;
int i;
scanf("%d",&i);
while(i!=0)
{push(i);
scanf("%d",&i);
}
printf("now the pop action\n");
while((top->link)!=NULL)
{printf("%d\n",top->data);
p="top";
top="top-">link;
free(p);
}
printf("%d\n",top->data);
printf("all the numbers have been poped out\n");
getch();
}
/*递归建立一棵树和前的遍历*/
#include "stdio.h"
#include "alloc.h"
typedef struct node {
int data;
struct node * lch,* rch;
}binode,* bitree;
bitree CreatTree()
{bitree t;
int c;
t=(bitree)malloc(sizeof(binode));
scanf("%d",&c);
if(c==0)
return NULL;
else{t->data=c;
t->lch=CreatTree();
t->rch=CreatTree();
return t;
}
}
void preorder(bitree t)
{if(t)
{printf("%d\n",t->data);
preorder(t->lch);
preorder(t->rch);
}
}
main()
{bitree t;
t="CreatTree"();
printf("****************/\n");
preorder(t);
}
用户99147 2007-12-20 17:01
都怪 以前没学好
数据结构都忘了差不多了