2013-07-26 43 views
0

嘿家伙这是我第一次做双链表,所以我不是很确定我在这里做什么,需要一些帮助来检查代码,谢谢,这里是我所做的评论。 我在这里做的功能是打印,打印相反,算上链表的元素,和搜索功能,以确定这个节点是否存在在双向链表C编程中反向查看/搜索功能的麻烦

void printListfow() //print the list in forward manner 
{ 
    CLR; 
    struct node *tmpval; //declare a temp storage 
    if(start==NULL) //if 1st node = null,then nth is inside,nth to print 
    { 
     printf("List is empty\n"); 
     return; 
    } 
    tmpval=start; //assign the head/start to temp storage to retrieve data in 1st node 
    printf("List of customer details: \n"); 
    while(tmpval!=NULL) //keep doing till it is NULL/the end 
    { 
     printf("%d ", tmpval->detail); //print the 'detail' which is in the node temp is pointing at 
     tmpval=tmpval->next; //assign next node to the temp storage so that it can be printed again 
    } 

} 


void printListrev() //print in reverse manner 
{ 
     CLR; 
     struct node *tmpval; //temp storage 
     if(start==NULL) // 
     { 
      printf("List is empty\n"); 
      return; 
     } 
     tmpval=start; //assign start to tmpval to retrieve value 
     printf("List of customer details: \n"); 
     tmpval=tmpval->prev //move backward and assign the data to tmpval 
     printf("%d",tmpval->detail) //print it 

} 

void count() //count total number of records 
{ struct node *x; 
    x=start; //assign value of start to temp storage 
    int ctr=0; //initialize counter 
    while(x!=NULL) 
    { 
    x=x->next; //keep going to next node and then increase the counter 
    ctr++; 
    } 
    printf("Number of customer records are %d\n",ctr); 
} 



int getNode(node *tmp ,int cust) //when user wants to delete a customer ID and its details, this will search through the list,then if found,pass the value to another function for deletion 
{ 
    tmp=tmp->cust; 
    while(tmp!=NULL) 
    { 
     if(tmp->detail == cust) //check if detail[ID stored] is same as requested[cust] 
     { 
      return 1; 
     }tmp=tmp->next; //if not same,then move to next one 
    }return 0; 

} 

的感谢!

+1

也许这更适合于:http://codereview.stackexchange.com/ – phoxis

+0

在'printListrev()'你忘了添加循环返回我想! ?? –

+2

你也应该发布结构节点声明 – nio

回答

0

在上下文printListrev()

除非这是一个循环双向链表,在这种情况下,最后一个元素是由第一之前,start将有一个元素NULL。因此,有在访问的start前一场没有意义的,因为你在这里做的:

tmpval=start; 
... 
tmpval=tmpval->prev; 

你可以把另一个指针到结束列表用于此目的的。

其他替代方案包括:

递归函数:

void printrev(struct node *s) 
{ 
    if (s == NULL) 
    return; 
    printrev(s->next); 
    printf("%d ", s->detail); 
} 

迭代函数:

void printrev() 
{ 
    struct node *end; 
    for (end = start; end->next != NULL; end = end->next) 
    ; 
    for (; end != NULL; end = end->prev) 
    printf("%d ", end->detail); 
} 

getNode存在一定的局限性。假设,如果你想删除元素,你的getnode只会返回元素是否存在。假设它存在,你的deleteNode函数在删除之前仍然需要迭代到列表中相应的元素。

这可以通过getNode返回指针的节点来解决:

node *getNode(int x) 
{ 
    node *t; 
    for (t = start; t != NULL; t = t->next) 
     if (t->detail == x) 
       return t; 
    return t; 
} 

现在,您可以编写删除如下:

void delNode(node *n) 
{ 
    n->prev->next = n->next; 
    n->next->prev = n->prev; 
    free(n); 
} 

并调用如下:

node *n; 

if ((n = getNode(x)) != NULL) 
    delNode(n); 

我假设你struct是:

struct node { 
    int detail; 
    struct node *next; 
    struct node *right; 
}; 

typedef struct node * node; 
+0

递归与迭代的区别? – user2421843

+0

一个递归函数,解决了大小为n-1的第一个子问题,它除了递归地打印所有的n-1个元素外,什么也不做,然后打印第n个元素。因此最终的顺序是相反的。这与堆栈非常相似。它会先推n,然后n-1 ... 1,等等。现在,每个元素都会弹出并打印出来。所以,顺序变为1,2 ... n,与原始顺序相反 – mohit

+0

另一方面,迭代函数只是迭代到列表的末尾。然后,再次迭代到开头,打印其间的所有元素。 – mohit

0
  • 在printListrev()要打印只有一个节点不会反转。
  • 您正在做的一个主要问题是在getNode()中,您正在更改本地指针的地址,但原始指针仍指向之前的位置。
  • 如果是这样,那么您将如何删除该节点,因为您无法知道此函数返回后的节点地址。
  • 你打算为所有的节点调用getNode(),如果这样的话,如果你有很多节点,那么它就不好。