Chinaunix首页 | 论坛 | 博客
  • 博客访问: 250784
  • 博文数量: 45
  • 博客积分: 802
  • 博客等级: 军士长
  • 技术积分: 470
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-08 12:21
文章分类
文章存档

2014年(4)

2013年(4)

2012年(37)

我的朋友

分类: C/C++

2012-01-07 11:55:40

面试百度、腾讯、阿里的时候多多少少都问到些,要求在纸上写出。

以下代码是自己看数据结构书写出的总结。大家一定要自己动手写一写,画一画算法的过程图,毕竟自己写出来的才是属于自己的,切勿眼高手低啊 : )

 

  1. //排序算法
  2. void BubbleSort(int *a, int len){
  3.     int i, j;
  4.     for(i = 0; i < len - 1; i++){
  5.         for(j = len - 1; j >= i; j--){
  6.             if(a[j+1] < a[j]){
  7.                 swap(&a[j+1], &a[j]);
  8.             }
  9.         }
  10.     }
  11. }

  12. void SelectSort(int *a, int len){
  13.     int i, j;
  14.     for(i = 0; i < len - 1; i++){
  15.         for(j = i + 1; j < len; j++){
  16.             if(a[j] < a[i]){
  17.                 swap(&a[i], &a[j]);
  18.             }
  19.         }
  20.     }
  21. }

  22. void InsertSort(int *a, int len){
  23.     int i, j, k;
  24.     for(i = 2; i <= len; i++){
  25.         a[0] = a[i];
  26.         for(j = i - 1; a[j] > a[i]; j--);
  27.         for(k = i - 1; k > j; k--){
  28.             swap(&a[k+1], &a[k]);
  29.         }
  30.         a[j+1] = a[0];
  31.     }
  32. }

  33. int Partition(int *a, int first, int last){
  34.     int midkey = a[0];
  35.     while(first < last){
  36.         while(first < last && a[last] >= midkey)
  37.             last--;
  38.         a[first++] = a[last];
  39.         while(first < last && a[first] <= midkey)
  40.             first++;
  41.         a[last--] = a[first];
  42.     }
  43.     a[first] = midkey;
  44.     return first;
  45. }

  46. void QuickSort(int *a, int first, int last){
  47.     if(first < last){
  48.         int mid = Partition(a, first, last);
  49.         QuickSort(a, first, mid - 1);
  50.         QuickSort(a, mid + 1, last);
  51.     }
  52. }

  53. int bisearch(int *a, int first, int last, int key){
  54.     if(first <= last){
  55.         int mid = (first + last)/2;
  56.         if(key == a[mid]) return mid;
  57.         else if(key > a[mid])
  58.             return bisearch(a, mid + 1, last, key);
  59.         else if(key < a[mid])
  60.             return bisearch(a, first, mid - 1, key);
  61.     }
  62.     else
  63.         return -1;
  64. }

  65. int bisearch_norecur(int *a, int first, int last, int key){
  66.     int mid;
  67.     while(first <= last){
  68.         mid = (first + last)/2;
  69.         if(key == a[mid]) return mid;
  70.         else if(key < a[mid]) last = mid - 1;
  71.         else if(key > a[mid]) first = mid + 1;
  72.     }
  73.     return -1;
  74. }

  75. //单双链表的基本操作
  76. typedef struct node{
  77.     int val;
  78.     struct node *prev;
  79.     struct node *next;
  80. }node;

  81. node *creatlist(int len){
  82.     if(len == 0)
  83.         return NULL;
  84.     node *head = (node *)malloc(sizeof(node));
  85.     head->next = NULL;
  86.     head->prev = NULL;
  87.     printf("请输入第1个节点的值:");
  88.     scanf("%d", &head->val);
  89.     node *p = head;
  90.     int i;
  91.     for(i = 1; i < len; i++){
  92.         node *new_node = (node *)malloc(sizeof(node));
  93.         new_node->next = NULL;
  94.         new_node->prev = NULL;
  95.         printf("请输入第%d个节点的值:", i+1);
  96.         scanf("%d", &new_node->val);
  97.         p->next = new_node;
  98.         new_node->prev = p;
  99.         p = new_node;
  100.     }
  101.     return head;
  102. }

  103. void showlist(node *head){
  104.     if(!head)
  105.         return;
  106.     node *p = head;
  107.     while(p){
  108.         if(!p->next){
  109.             printf("%d\t", p->val);
  110.             break;
  111.         }
  112.         printf("%d\t", p->val);
  113.         p = p->next;
  114.     }
  115.     printf("\nreverse\n");

  116.     while(p){
  117.         printf("%d\t", p->val);
  118.         p = p->prev;
  119.     }
  120.     printf("\n");
  121. }
  122. node *delnode(node *head, int key){
  123.     if(!head)
  124.         return;
  125.     node *p = head;
  126.     node *tmp = NULL;
  127.     while(p->val == key){
  128.         if(p->next){
  129.             p->next->prev = NULL;
  130.             tmp = p;
  131.             p = p->next;
  132.             free(tmp);
  133.         }
  134.         else{
  135.             free(p);
  136.             return NULL;
  137.         }
  138.     }
  139.     head = p;
  140.     p = p->next;
  141.     while(p){
  142.         if(p->val == key){
  143.             if(!p->next){
  144.                 p->prev->next = NULL;
  145.                 free(p);
  146.                 return head;
  147.             }
  148.             p->prev->next = p->next;
  149.             p->next->prev = p->prev;
  150.             tmp = p;
  151.             p = p->next;

  152.             free(tmp);
  153.         }
  154.         else{
  155.             p = p->next;
  156.         }
  157.     }
  158.     return head;
  159. }

  160. node *addnode(node *head, int key){
  161.     node *new_node = (node *)malloc(sizeof(node));
  162.     new_node->val = key;
  163.     new_node->next = NULL;
  164.     new_node->prev = NULL;
  165.     if(!head)
  166.         return new_node;
  167.     if(key < head->val){
  168.         new_node->next = head;
  169.         head->prev = new_node;
  170.         head = new_node;
  171.         return head;
  172.     }
  173.     node *p = head->next;
  174.     while(p && key > p->val){
  175.         if(!p->next){
  176.             p->next = new_node;
  177.             new_node->prev = p;
  178.             return head;
  179.         }
  180.         p = p->next;
  181.     }

  182.     new_node->prev = p->prev;
  183.     new_node->next = p;
  184.     p->prev->next = new_node;
  185.     p->prev = new_node;

  186.     return head;
  187. }


  188. typedef struct node{
  189.     int val;
  190.     struct node *next;
  191. }node;

  192. node *createlist(int len){
  193.     if(len == 0){
  194.         return NULL;
  195.     }

  196.     node *head = (node *)malloc(sizeof(node));
  197.     head->next = NULL;
  198.     printf("请输入第1个节点的值:");
  199.     scanf("%d", &head->val);
  200.     node *p = head;
  201.     int i;
  202.     for(i = 1; i < len; i++){
  203.         node *new_node = (node *)malloc(sizeof(node));
  204.         new_node->next = NULL;
  205.         printf("请输入第%d个节点的值:", i+1);
  206.         scanf("%d", &new_node->val);
  207.         p->next = new_node;
  208.         p = new_node;
  209.     }

  210.     return head;
  211. }

  212. void showlist(node *head){
  213.     if(!head)
  214.         return;
  215.     node *p = head;
  216.     while(p){
  217.         printf("%d\t", p->val);
  218.         p = p->next;
  219.     }
  220.     printf("\n");
  221. }

  222. node *delnode(node *head, int key){
  223.     if(!head)
  224.         return NULL;
  225.     node *p = head;
  226.     node *tmp = NULL;
  227.     while(p && p->val == key){
  228.         tmp = p;
  229.         p = p->next;
  230.         free(tmp);
  231.     }
  232.     if(!p)
  233.         return NULL;
  234.     head = p;
  235.     node *r = p->next;
  236.     while(r){
  237.         if(r->val == key){
  238.             p->next = r->next;
  239.             tmp = r;
  240.             r = r->next;

  241.             free(tmp);
  242.         }
  243.         else{
  244.             p = r;
  245.             r = r->next;
  246.         }
  247.     }

  248.     return head;
  249. }

  250. node *addnode(node *head, int key){
  251.     node *new_node = (node *)malloc(sizeof(node));
  252.     new_node->next = NULL;
  253.     new_node->val = key;
  254.     if(!head)
  255.         return new_node;
  256.     if(key < head->val){
  257.         new_node->next = head;
  258.         head = new_node;
  259.         return head;
  260.     }
  261.     node *p = head;
  262.     while(p->next && key > p->next->val){
  263.         p = p->next;
  264.     }
  265.     new_node->next = p->next;
  266.     p->next = new_node;
  267.     return head;
  268. }

  269. //链栈与链队列
  270. typedef struct queue{
  271.     node *head;
  272.     node *tail;
  273. }queue;

  274. typedef struct stack{
  275.     node *top;
  276.     node *bottom;
  277. }stack;

  278. queue *insertqueue(queue *q, int x){
  279.     if(!q)
  280.         return NULL;
  281.     node *new_node = (node *)malloc(sizeof(node));
  282.     new_node->val = x;
  283.     new_node->next = NULL;

  284.     if(q->head == NULL){
  285.         q->head = new_node;
  286.         q->tail = new_node;
  287.     }
  288.     else{
  289.         q->tail->next = new_node;
  290.         q->tail = new_node;
  291.     }
  292.     return q;
  293. }

  294. queue *delqueue(queue *q){
  295.     if(!q)
  296.         return NULL;
  297.     if(q->head == NULL){
  298.         printf("队列中无节点\n");
  299.         return q;
  300.     }
  301.     if(q->head == q->tail){
  302.         free(q->head);
  303.         q->head = q->tail = NULL;
  304.     }
  305.     else{
  306.         node *tmp = q->head;
  307.         q->head = q->head->next;
  308.         free(tmp);
  309.     }
  310.     return q;
  311. }

  312. stack *pushstack(stack *s, int x){
  313.     if(!s)
  314.         return NULL;
  315.     node *new_node = (node *)malloc(sizeof(node));
  316.     new_node->val = x;
  317.     new_node->next = NULL;
  318.     if(s->top == NULL){
  319.         s->top = s->bottom = new_node;
  320.     }
  321.     else{
  322.         s->top->next = new_node;
  323.         s->top = new_node;
  324.     }
  325.     return s;
  326. }

  327. stack *popstack(stack *s){
  328.     if(!s)
  329.         return NULL;
  330.     if(s->top == NULL){
  331.         printf("栈中没有元素\n");
  332.         return s;
  333.     }
  334.     if(s->top == s->bottom){
  335.         free(s->top);
  336.         s->top = s->bottom = NULL;
  337.     }
  338.     else{
  339.         node *p = s->bottom;
  340.         while(p->next != s->top)
  341.             p = p->next;
  342.         free(s->top);
  343.         s->top = p;
  344.     }
  345.     return s;
  346. }

 

 

 

 

阅读(2044) | 评论(0) | 转发(6) |
给主人留下些什么吧!~~