双链表C编程中的反向查看/搜索功能出现问题

时间:2013-07-26 12:25:15

标签: c linked-list

嘿伙计这是我第一次做双链表,所以我不太清楚我在这里做什么,需要一些帮助来检查代码,谢谢,这是我所做的评论包含的内容。 我在这里完成的功能是打印,打印反向,链表中的计数元素,以及确定此节点是否存在的搜索功能

 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;

}

谢谢!

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

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