EDN首页   博客首页

日志档案

发表于 2007-12-18 22:17:25

1

标签: c  数据结构  

发两个今天编的c 程序

数据结构方面的两个程序  很简单哈,呵呵

/*这是个堆栈操作*/

#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);
 }


系统分类: 汽车电子   |   用户分类: C language   |   来源: 原创   |   【推荐给朋友】   |   【添加到收藏夹】

    阅读(822)    回复(1)  

投一票您将和博主都有获奖机会!

最新评论

  • dipdianzi

    2007-12-20 17:01:14

    都怪 以前没学好

    数据结构都忘了差不多了