+ -
当前位置:首页 → 问答吧 → 求助。正想跳槽 去面试遇到的题目

求助。正想跳槽 去面试遇到的题目

时间:2010-06-22

来源:互联网

如何不使用新节点倒置一个非循环单向链表? 想了很久没想出来

作者: 输给钱的男人   发布时间: 2010-06-22

有人会吗。。

作者: 输给钱的男人   发布时间: 2010-06-22

这是“数据结构”的作业题啊,
goole “倒置 单向链表”

作者: c/unix   发布时间: 2010-06-22

有答案?不能引入新的节点啊

作者: 输给钱的男人   发布时间: 2010-06-22

不让使用新节点,让不让用新指针?

作者: 狗气球   发布时间: 2010-06-22

最笨的办法,思路和冒泡相似,一个个的换。

作者: k8king   发布时间: 2010-06-22

我也不会。。。

作者: pandaiam   发布时间: 2010-06-22

呢地神哪,楼主你面的哪?

一个指针解决问题。

作者: zhaohongjian000   发布时间: 2010-06-22



QUOTE:
呢地神哪,楼主你面的哪?

一个指针解决问题。
zhaohongjian000 发表于 2010-06-22 15:38




    不允许用新节点,允许用指针?

作者: asdmonster   发布时间: 2010-06-22



QUOTE:
不允许用新节点,允许用指针?
asdmonster 发表于 2010-06-22 15:52




    不允许创建新节点是为了防止那种傻瓜式的倒置:每访问一个节点,就创建一个节点,然后将这个节点的next置上上个节点的地址,遍历完原先的节点后,就生成了一条新的倒置的链表啊。

    题目的原意应该是这样,没你想的复杂,所以用一个指针,递归置换就ok。

作者: doofy   发布时间: 2010-06-22

贴个测试代码~~
  1. include <stdio.h>
  2. #include <stdlib.h>

  3. struct node_t {
  4.         struct node_t *next;
  5.         int index;
  6. };

  7. struct node_t *malloc_node(int index)
  8. {
  9.         struct node_t *pnode = malloc(sizeof(struct node_t));

  10.         pnode->index = index;
  11.        
  12.         return pnode;
  13. }

  14. struct node_t *init_node(int num)
  15. {
  16.         int i =  0;
  17.         struct node_t *node_head = malloc(sizeof(struct node_t));
  18.         struct node_t *pnode;
  19.         struct node_t *pre_node = node_head;

  20.         for(; i < num; i++) {
  21.                 pnode = malloc_node(i);               
  22.                 pre_node->next = pnode;       
  23.                 pre_node = pnode;
  24.         }

  25.         pnode->next = NULL;

  26.         return node_head;
  27. }

  28. void dump_node_list(struct node_t *phead)
  29. {
  30.         struct node_t *p = phead;

  31.         while(p->next) {
  32.                 p = p->next;
  33.                 printf("index: %d\n", p->index);
  34.         }
  35. }

  36. struct node_t *convert_node(struct node_t *pre_node, struct node_t *pnode)
  37. {
  38.         struct node_t *tmp = pnode->next;
  39.         struct node_t *last = NULL;

  40.         if(pnode->next) {
  41.                 last = convert_node(pnode, pnode->next);       
  42.                 tmp->next = pnode;
  43.                 pnode->next = pre_node;
  44.         } else {
  45.                 pnode->next = pre_node;
  46.                 last = pnode;
  47.         }
  48.         return last;
  49. }

  50. void convert(struct node_t *phead)
  51. {
  52.         struct node_t *last = NULL;

  53.         last = convert_node(NULL, phead->next);       
  54.         phead->next = last;
  55. }

  56. int main()
  57. {
  58.         struct node_t *phead = init_node(10);

  59.         dump_node_list(phead);

  60.         convert(phead);

  61.         dump_node_list(phead);

  62.         return 0;
  63. }
复制代码

作者: doofy   发布时间: 2010-06-22

  1. #include <stdio.h>
  2. struct node
  3. {
  4.     struct node *next;
  5.     int data;
  6. };
  7. int main()
  8. {
  9.     int a[6];
  10.     int i;
  11.     struct node *node;
  12.     struct node *root;
  13.     struct node *tmp;
  14.     void *b;
  15.     void *c;
  16.     void *d;
  17.     int flag;
  18.     flag=0;
  19.     b=NULL;
  20.     c=NULL;
  21.     tmp = NULL;
  22.     root=NULL;
  23.     node=NULL;
  24.     for ( i=0;i<6;i++ )
  25.     {
  26.         a[i]=i;
  27.     }
  28. /*创建六个节点开始*/
  29.     for ( i=0;i<6;i++ )
  30.     {
  31.         node=malloc(sizeof(struct node));
  32.         if ( 0==i )
  33.         {
  34.             root=node;
  35.             node->next=NULL;
  36.             tmp=root;
  37.             node->data=a[i];
  38.             continue;
  39.         }
  40.         node->next=NULL;
  41.         node->data=a[i];
  42.        ((struct node *)tmp)->next = node;
  43.         node = NULL;
  44.         tmp=tmp->next;
  45.     }
  46. /*创建六个节点结束*/
  47.     tmp=root;
  48.     i=0;
  49. /*打印节点*/
  50.     while ( NULL!=tmp )
  51.     {
  52.         fprintf(stderr,"%d ",tmp->data);
  53.         tmp=tmp->next;
  54.         i++;
  55.     }
  56.     i=i-3;
  57.     fprintf(stderr,"\n");
  58. /*倒置节点开始*/
  59.     b=root->next->next;
  60.     root->next->next=root;
  61.     c=root->next;
  62.     root->next->next->next=NULL;
  63.     root=b;
  64.     while ( flag!=i )
  65.     {
  66.         flag++;
  67.         root=b;
  68.         b=root->next;
  69.         root->next=c;
  70.         c=root;
  71.     }
  72.     root=b;
  73.     root->next=c;
  74.     tmp=root;
  75. /*倒置节点结束*/
  76.     while ( NULL!=tmp)
  77.     {
  78.         fprintf(stderr,"%d ",tmp->data);
  79.         tmp=tmp->next;
  80.     }
  81.     fprintf(stderr,"\n");
  82.     tmp=root;
  83.     while ( NULL!=tmp)
  84.     {
  85.         d=tmp->next;
  86.         free(tmp);
  87.         tmp=d;
  88.     }
  89.     tmp =  NULL;
  90.     root = NULL;

  91. }
复制代码
大家帮忙看看我写的这个对不对!

作者: kanhfshiys   发布时间: 2010-06-22

网上搜的
         typedef struct node   
        {   
                int data;   
                struct node *link;   
        }NODE;
       
        void reverse(NODE head)   
        {   
                NODE temp = null;   
                NODE p = head->link;   
                head->link = null;// 头结点变为尾结点
          
                while(p!=null)   
                {   
                   temp = p->link;   
                   p->link = head;// 当前结点指针倒置
                   head = p;   
                   p = temp;   
                }   
        }

作者: kmindg   发布时间: 2010-06-22

节点和指针是一样的吗?一个是用户定义大小未知,一个4字节的变量

作者: ydfgic   发布时间: 2010-06-22

。。。 递归的做法是有问题的, 会栈溢出的的。

作者: benjiam   发布时间: 2010-06-22



QUOTE:
不允许用新节点,允许用指针?
asdmonster 发表于 2010-06-22 15:52



节点是节点,指针是指针....

作者: zhaohongjian000   发布时间: 2010-06-22

void reverse( struct node *head )
{
        if( head==NULL )
                return;
        struct node *pre, *cur, *nex;
        pre = head;
        cur = pre->next;

       
        while( cur )
        {
                nex = cur->next;

                cur->next = pre;

                pre = cur;
                cur = nex;
        }

        head->next = NULL;
        head = pre;


        return;
}

作者: mgqw   发布时间: 2010-06-22

单向链表这种数据结构,我是从来不用的,链条断裂死的很惨,而且还很不好查。

作者: 没本   发布时间: 2010-06-22