简体   繁体   English

在双链表C编程中使用反向查看/搜索功能的麻烦

[英]trouble in with reverse viewing/search function in double linked list C programming

hey guys this is my first time doing double linked list so I'm not very sure what I'm doing here ,need some help to check the code,thanks, here is what I have done with comments included. 大家好,这是我第一次做双链表,所以我不太确定我在这里做什么,需要一些帮助来检查代码,谢谢,这是我在做评论时所做的。 The functions I have done here are print,print reverse,count elements in the linked list,and search function to determine whether this node exist 我在这里执行的功能是打印,打印反向,在链表中计数元素以及搜索功能以确定该节点是否存在

 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;

}

thanks! 谢谢!

In context to printListrev() : printListrev()上下文中:

Unless this is a circular doubly linked list, in which case last element is preceded by the first, start would have previous element NULL. 除非这是一个循环的双向链表,否则在最后一个元素之后是第一个元素的情况下, start将具有上一个元素NULL。 So, there is no point in accessing the previous field of start , as you do here: 因此,没有必要像在这里那样访问start的上一个字段:

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

You can keep another pointer to end of the list for this purpose. 为此,您可以将另一个指针保留在列表末尾。

Other alternatives include: 其他替代方案包括:

recursive function: 递归函数:

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

iterative function: 迭代功能:

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

Your getNode is of limited use. 您的getNode用途有限。 Suppose, if you want to delete element, your getnode would only return whether, element is present or not. 假设,如果要删除元素,则getnode将仅返回是否存在元素。 Say it is present, your deleteNode function would still have to iterate to the appropriate element in the list before deleting it. 假设存在,则deleteNode函数在删除之前仍必须迭代到列表中的适当元素。

This could be solved by getNode returning pointer to the node: 这可以通过getNode返回指向节点的指针来解决:

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

Now, you can code delete as follows: 现在,您可以编写删除代码,如下所示:

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

And call as follows: 并调用如下:

node *n;

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

I've assumed that you struct is: 我假设您的struct是:

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

typedef struct node * node;
  • In printListrev() you are printing only one node not going reverse. 在printListrev()中,您仅打印一个不会反转的节点。
  • One major problem you are doing is in getNode() ,you are changing the address of a local pointer but the original pointer still points where it previously. 您正在做的一个主要问题是在getNode()中,您正在更改本地指针的地址,但是原始指针仍指向先前的位置。
  • If so then how you are going to delete that node ,as you can't know the node address after this function returns. 如果是这样,那么您将如何删除该节点,因为此函数返回后您将无法知道该节点的地址。
  • Are you going to call the getNode() for all the node ,if so that is not a good if you have many nodes. 您是否要为所有节点调用getNode(),如果这样的话,如果您有许多节点,那就不好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM