最近学习C++ 数据结构 尝试代码就放上来吧 初学者适用

都经过编译的

链表的

Code:
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <time.h>   
  4. #define N 10   
  5. typedef struct node *link;   
  6. struct node    
  7. {   
  8.     int item;   
  9.     link next;   
  10. };   
  11. link NODE(int item,link next)   
  12. {      
  13.        
  14.     link t = (link)malloc(sizeof *t);    
  15.        
  16.     t->item = item;   
  17.     t->next = next;   
  18.     return t;   
  19. }   
  20. int main()   
  21. {   
  22.     int i;   
  23.     link head = NULL;   
  24.     link t = NULL;   
  25.     srand(time(NULL));   
  26.     for(i=0;i<N;i++)   
  27.         head = NODE(rand()%100,head);   
  28.     for(t=head;t!=NULL;t=t->next)   
  29.         printf ("%3d",t->item);   
  30.     printf("/n");   
  31.     return 0;   
  32. }  

 

 

非空表头链表

Code:
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <time.h>   
  4. #define N 10   
  5. typedef struct node *link;   
  6. struct node    
  7. {   
  8.     int item;   
  9.     link next;   
  10. };   
  11. link NODE(int item,link next)   
  12. {      
  13.        
  14.     link t = (link)malloc(sizeof *t);    
  15.        
  16.     t->item = item;   
  17.     t->next = next;   
  18.     return t;   
  19. }   
  20. link insert_node(link head,int item)   
  21. {   
  22.     link x,y;   
  23.     for(y=head,x=head;y;x=y,y=y->next)   
  24.         if(item<=y->item) break;   
  25.     if(x==y) head = NODE(item,head);  //判断是不是表头   
  26.     x->next = NODE(item,y);   
  27.   
  28.   
  29. }   
  30. int main()   
  31. {   
  32.     int i;   
  33.     link head = NULL;   
  34.     link t = NULL;   
  35.     srand(time(NULL));   
  36.     for(i=0;i<N;i++)   
  37.         head = insert_node(head,read()%100);   
  38.     for(t=head;t!=NULL;t=t->next)   
  39.         printf ("%3d",t->item);   
  40.     printf("/n");   
  41.     return 0;   
  42. }  

逆波兰表达式

Code:
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <string.h>   
  4. int *S;   
  5. int top;   
  6. void init_stack(int max_stack)   //初始化站   
  7. {   
  8.     S = (int*)malloc(max_stack*sizeof(int));   
  9.     top = 0;   
  10. }   
  11. void push(int item)   //压栈   
  12. {   
  13.     S[top++] = item;   
  14. }   
  15. int pop()           //出栈   
  16. {   
  17.     return S[--top];   
  18. }   
  19. int is_empty()   //判断栈空   
  20. {   
  21.     return top == 0;   
  22. }   
  23. int posteval(char *a)   
  24. {   
  25.     int i,n = strlen(a);   
  26.     init_stack(n);   
  27.     for(i=0;i<n;i++){   
  28.         if(a[i] == '+') push(pop()+pop());   
  29.         if(a[i] == '*') push(pop()*pop());   
  30.         if(a[i]>='0' && a[i]<='9')   
  31.             push(a[i] - '0' );   
  32.     }   
  33.     return pop();      
  34. }   
  35. int main()   
  36. {   
  37.     printf("%d/n",posteval("1 2 + 3 4 + *"));   
  38.     return 0 ;   
  39. }  

BST 树

Code:
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <time.h>   
  4. #define N 8   
  5. typedef struct node *link;   
  6. struct node   
  7. {   
  8.     int item;   
  9.     link l,r;   
  10. };   
  11. link NODE(int item)   
  12. {   
  13.     link t = (link)malloc(sizeof *t);   
  14.     t->item = item;   
  15.     t->l = NULL;   
  16.     t->r = NULL;   
  17.     return t;   
  18. }   
  19. int search(link t,int key)   
  20. {   
  21.     if(t==NULL) return 0;   
  22.     if(key < t->item) return search(t->l,key);   
  23.     if(key > t->item) return search(t->r,key);   
  24.     return 1;   
  25. }   
  26. link insert_node(link t,int item)   
  27. {   
  28.     if(t==NULL)   
  29.         return NODE(item);   
  30.     if(item <= t->item)   
  31.         t->l = insert_node(t->l,item);   
  32.     else if(item > t->item)   
  33.         t->r = insert_node(t->r,item);   
  34.     return t;   
  35. }   
  36. void pre_ord(link t)   
  37. {   
  38.     if(t==NULL) return ;   
  39.     printf("%3d",t->item);   
  40.     pre_ord(t->l);   
  41.     pre_ord(t->r);   
  42. }   
  43. void in_ord(link t)   
  44. {   
  45.     if(t==NULL) return ;   
  46.     in_ord(t->l);   
  47.     printf("%3d",t->item);   
  48.     in_ord(t->r);   
  49. }   
  50. void lost_ord(link t)   
  51. {   
  52.     if(t==NULL) return ;   
  53.     lost_ord(t->l);   
  54.     lost_ord(t->r);   
  55.     printf("%3d",t->item);   
  56. }   
  57. int main()   
  58. {   
  59.     link root = NULL;   
  60.     srand(time(NULL));   
  61.     int i;   
  62.     for(i=0;i<N;i++)   
  63.     {   
  64.         root = insert_node(root,rand()%100);   
  65.     }      
  66.     pre_ord(root);   
  67.     printf("/n");   
  68.     in_ord(root);   
  69.     printf("/n");   
  70.     lost_ord(root);   
  71.     printf("/n");   
  72. return 0;   
  73.   
  74. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值