2012-04-27 98 views
0

我正在尝试打印反向链接列表。但我只获得一个价值。我哪里错了?因为我是新来的C.反向链接列表中的c

#include<stdio.h> 
#include<stdlib.h> 

struct itemlist 
{ 
     int value; 
     struct itemlist *next; 
}; 

typedef struct itemlist item; 

int main(void) 
{ 
     itemlist *curr,*head,*tail; 

     head=NULL; 
     tail=NULL; 

     for(int i=1;i<10;i++) 
     { 
       curr=(itemlist *)malloc(sizeof(itemlist)); 
       curr->value=i; 
       curr->next=tail; 
       tail=curr; 
       if(!head) 
       head=curr; 
     } 

     curr=head; 

     while(curr) 
     { 
       printf("Curr value is:%d\n",curr->value); 
       curr=curr->next; 
     } 
     return 0; 
} 
+0

这是C++,而不是C,因为您不需要将itemlist引用为'struct itemlist'。既然是这样,为什么不使用新的malloc? – 2012-04-27 19:55:23

+2

@ RichardJ.RossIII:代码中有一个'typedef'。 – dirkgently 2012-04-27 19:56:00

+1

@dirkgently是真的,但他没有使用它。 – 2012-04-27 19:57:13

回答

1

此代码打印1至9

#include<stdio.h> 
#include<stdlib.h> 

struct itemlist 
{ 
     int value; 
     struct itemlist *next; 
}; 

typedef struct itemlist item; 

int main(void) 
{ 
     itemlist *curr,*head,*prev; 

     head=NULL; 
     curr=NULL; 
     prev=NULL; 

     for(int i=1;i<10;i++) 
     { 
       curr = new itemlist; 
       curr->value = i; 
       curr->next = NULL; 

       if (head == NULL) 
        head = curr; 
       if(prev != NULL) 
        prev->next = curr; 

       prev = curr; 
     } 

     curr=head; 

     while(curr) 
     { 
       printf("Curr value is:%d\n",curr->value); 
       curr=curr->next; 
     } 
     return 0; 
} 
0

当你退出循环您head(这是唯一的一次更新时加入的第一个元素)指向最后一个元素,请多多包涵。

0

的问题是,在第一次迭代中,当您指定当前项目的下一个元素:

curr->next=tail; 

尾值为NULL,所以你的链表的头不能达到其余部分

1

看起来像你应该开始从尾巴,而不是从头打印你的清单。

变化

curr=head; 

curr = tail; 
1

变化curr=headcurr=tail

下面是一个简单的例子说明一个双向链表应该让你用自己的方式理解链接列表

#include<stdio.h> 
#include<stdlib.h> 

typedef struct 
{ 
     int value; 
     struct itemlist *next; 
     struct itemlist *prev; 
}itemlist; 

void forward(itemlist *head) 
{ 
    itemlist *curr = head; 
    while(curr) 
    { 
     printf("Curr value is: %d\n", curr->value); 
     curr = curr->next; 
    } 
} 

void backward(itemlist *tail) 
{ 
    itemlist *curr = tail; 
    while(curr) 
    { 
     printf("Curr value is: %d\n", curr->value); 
     curr = curr->prev; 
    } 
} 

int main(void) 
{ 
     itemlist *curr,*head,*tail; 

     head=NULL; 
     tail=NULL; 

     for(int i=1;i<10;i++) 
     { 
       curr=(itemlist *)malloc(sizeof(itemlist)); 
       curr->value=i; 
       curr->next = NULL; 
       if(tail) 
       { 
        curr->prev = tail; 
        tail->next = curr; 
       } 
       tail=curr; 
       if(!head) 
        head=curr; 
     } 

     printf("Forwards\n"); 
     forward(head); 
     printf("Backwards\n"); 
     backward(tail); 

     return 0; 
} 
+0

即使我将curr = head改为curr = tail ..我将所有值从9改为1,但是我需要1到9. – Teja 2012-04-27 20:01:26

+0

@Vutukuri所以你想要的值不是以相反的顺序?你在做什么是列表中的附加功能。你需要做一个添加回来以获得另一个方向的列表。 – twain249 2012-04-27 20:03:09

+0

是的,我想他们在升序.... – Teja 2012-04-27 20:04:23

0

headtail混淆在一起:

的第一个节点(节点0):

Value = 1 
next = tail = NULL; 
tail = Node 0 
head = Node 0 

第二个节点(节点1):

Value = 2 
next = tail = Node 0 
tail = Node 1 
head = Node 0 

现在你有

Node 1 - >Node 0 - >NULLhead = Node 0tail = Node 1

所以,当你打印开始时节点0(“头”,但它实际上是尾部)打印的第一个节点然后结束

你必须要么开关头尾部是正确的名称或开始tail

编辑打印:既然你说你要他们为了你可以这样做:

int main(void) 
{ 
    itemlist *curr,*head,*tail; 

    head=NULL; 
    tail=NULL; 

    for(int i=1;i<10;i++) 
    { 
     curr=(itemlist *)malloc(sizeof(itemlist)); 
     curr->value=i; 
     curr->next = NULL; 

     //if there is something in the list add the current node after it    
     if(tail) 
      tail->next = curr; 

     //Update the tails so it's pointing to the current last node   
     tail = curr; 

     //Set the head ONCE 
     //this will happened the first time when if(tail) fails 
     if(!head) 
     head=curr; 
    } 

    //start at the head 
    curr=head; 

    while(curr) 
    { 
     printf("Curr value is:%d\n",curr->value); 
     curr=curr->next; 
    } 
    return 0; 
} 
0

至少据我了解,你的计划是以相反的顺序创建一个链表,然后打印出它的内容。如果是这样,你可能想要这样的东西:

#include <stdlib.h> 
#include <stdio.h> 

struct itemlist { 
    int value; 
    struct itemlist *next; 
}; 

int main() { 

    struct itemlist *head = NULL; 
    struct itemlist *pos; 
    int i; 

    for (i=0; i<10; i++) { 
     struct itemlist *node = malloc(sizeof(*node)); 
     node->value = i; 
     node->next = head; 
     head = node; 
    } 

    for (pos=head; NULL != pos; pos = pos->next) 
     printf("%d\n", pos->value); 
    return 0; 
} 

请注意,你不需要一个指向“尾巴”的指针。基本思想非常简单:以head作为空列表(即空指针)开始。将每个新节点插入列表的开头,方法是将其指针next设置为列表的当前开头,然后将列表的开头设置为指向新节点。

1

你并不需要一个尾巴可言,但只需要一个递归函数。该函数在printf之前指向下一个。

void print_reverse(Itemlist *it){ 
    if(it !=NULL){ 
     print_reverse(it->next); 
     printf("Current value is:%d\n",it->value); 
    } 
}