Chinaunix首页 | 论坛 | 博客
  • 博客访问: 86434
  • 博文数量: 21
  • 博客积分: 547
  • 博客等级: 中士
  • 技术积分: 217
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-22 09:41
文章分类

全部博文(21)

文章存档

2013年(3)

2012年(2)

2011年(10)

2010年(6)

分类: C/C++

2011-06-19 18:46:18

前几天去一家公司面试被问到如何将一个单链表的内容反转,当时只是给了一个思路并没有最终完成程序的编写,今天突然觉得我有必要将它实现一下,以备以后面试的时候在被问到,呵呵。
对于这个问题我当时的想法就是用栈的思想去实现的,也就是我首先遍历原来的链表将链表中节点取出来并将它从原来的链表中删除,然后将这个取出来的节点添加到另外一个链表中,在这并不是简单的添加到链表的最后,而是将节点添加到head的后面,这样当原来的链表遍历到最后一个节点的时候,这个节点将被添加到新链表中的第一个节点的位置,这另外一个链表就是反转以后的链表,并且反转后的链表对于原来的链表只是从新分配了一个链表头而已。这样当你将原来的链表遍历了一遍以后,那么反转以后的链表也就生成了。具体的代码如下:
  1. #include    <stdio.h>
  2. #include    <stdlib.h>

  3. struct Node{
  4.     char data;
  5.     struct Node *next;
  6. };

  7. #define NODE_LEN sizeof(struct Node)

  8. struct Node *insert_behind(struct Node *head, struct Node *new_node)
  9. {
  10.     if(NULL == head || NULL == new_node)
  11.     {
  12.         return NULL;
  13.     }

  14.     struct Node *temp = NULL;

  15.     temp = head->next;
  16.     head->next = new_node;
  17.     new_node->next = temp;
  18.     
  19.     return head;
  20. }

  21. struct Node *get_and_delete(struct Node *head)
  22. {
  23.     if(NULL == head)
  24.     {
  25.         printf("get_and_delete error\n");
  26.         return NULL;
  27.     }

  28.     struct Node *temp = NULL;
  29.     temp = head->next;
  30.     head->next = temp->next;
  31.     return temp;
  32. }

  33. struct Node *inversion(struct Node *head)
  34. {
  35.     if(NULL == head)
  36.     {
  37.         printf("inversion error\n");
  38.         return NULL;
  39.     }
  40.     struct Node *new_head = NULL;
  41.     new_head = (struct Node *)malloc(NODE_LEN);
  42.     if(NULL == new_head)
  43.     {
  44.         printf("malloc error\n");
  45.         return head;
  46.     }
  47.     new_head->next = NULL;

  48.     while(NULL != head->next)
  49.     {
  50.         struct Node *temp = get_and_delete(head);
  51.         insert_behind(new_head, temp);
  52.     }
  53.     free(head);
  54.     return new_head;
  55. }

  56. void display(struct Node *head)
  57. {
  58.     
  59.     if(NULL == head)
  60.     {
  61.         printf("display error\n");
  62.         return;
  63.     }

  64.     struct Node *temp_head = head;

  65.     while(NULL != temp_head->next)
  66.     {
  67.         struct Node *temp =temp_head->next;
  68.         printf("the data = %c\n", temp->data);
  69.         temp_head = temp;
  70.     }
  71. }

  72. void free_list(struct Node *head)
  73. {
  74.     if(NULL == head)
  75.     {
  76.         printf("free error\n");
  77.         return;
  78.     }
  79.     while(NULL != head->next)
  80.     {
  81.         struct Node *temp = head->next;
  82.         head->next = temp->next;
  83.         free(temp);
  84.     }
  85.     free(head);
  86. }

  87. int main(int argc, char *argv[])
  88. {
  89.     struct Node *head = NULL;
  90.     struct Node *a = NULL, *b = NULL, *c = NULL;

  91.     head = (struct Node *)malloc(NODE_LEN);
  92.     a = (struct Node *)malloc(NODE_LEN);
  93.     b = (struct Node *)malloc(NODE_LEN);
  94.     c = (struct Node *)malloc(NODE_LEN);

  95.     a->data = 'a';
  96.     b->data = 'b';
  97.     c->data = 'c';

  98.     head->next = a;
  99.     a->next = b;
  100.     b->next = c;
  101.     c->next = NULL;
  102.     
  103.     display(head);
  104.     head = inversion(head);
  105.     display(head);
  106.     free_list(head);
  107.     
  108.     return 0;    
  109. }

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