删除具有重复数据的项目

发布于 2024-09-28 11:50:29 字数 855 浏览 7 评论 0原文

我正在编写一个函数来删除具有重复数据的连续项目。 例如 例如传入列表

->a->b->c->c->a->b->b->b->a->null

应该导致

->a->b->c->a->b->a->null

下面给出了列表项定义和函数声明

struct litem { 
char data; 
litem* next; 
}; 

Mo 代码如下所示

int remove_dumplicates(litem *&list)
{
 int count = 0; 
 struct litem * current = NULL;
 current = list;
 struct  litem *deleteNode;
 if (current == NULL ) return;
 while(current->next != NULL)
 {
  if ( current->data == current->next->data) // check for the duplicates 
   {
    count++;
    deleteNode =current->next;
    current>next= current->next->next;
    delete deleteNode;
   }      
  return (count);  
 }
}

这是实现以下功能的正确方法吗想要的结果?

I'm writing a function that removes the consecutive items with duplicate data .
e.g
For example, passing in the list

->a->b->c->c->a->b->b->b->a->null

should result in

->a->b->c->a->b->a->null

The list item definition and function declaration are given below

struct litem { 
char data; 
litem* next; 
}; 

Mo code looks like

int remove_dumplicates(litem *&list)
{
 int count = 0; 
 struct litem * current = NULL;
 current = list;
 struct  litem *deleteNode;
 if (current == NULL ) return;
 while(current->next != NULL)
 {
  if ( current->data == current->next->data) // check for the duplicates 
   {
    count++;
    deleteNode =current->next;
    current>next= current->next->next;
    delete deleteNode;
   }      
  return (count);  
 }
}

Is this a correct way of achieving the desired result ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

听风念你 2024-10-05 11:50:29

我没有看到 current 增加到 current->next

以包含所有唯一元素 a ->; 的列表为例。 b-> c 并查看您的程序如何工作。

要解决此问题,您需要:

while(current->next != NULL) {
   if ( current->data == current->next->data) {
     // delete duplicates .
   } else {
     current = current -> next;
   }
}// end-while
return (count);

I don't see current being incremented to current->next.

Take as an example a list with all unique elements a -> b -> c and see how your program works.

To fix this you need:

while(current->next != NULL) {
   if ( current->data == current->next->data) {
     // delete duplicates .
   } else {
     current = current -> next;
   }
}// end-while
return (count);
九厘米的零° 2024-10-05 11:50:29

您需要在 while 循环内添加 else 以前进到下一个节点:

if( current-> data == current->next->data ) {
....
} else {
    current = current->next;
}

还需要修复返回值(第一个应返回 0,第二个应移至 while 循环之外)。

You need to add an else inside the while loop to advance to the next node:

if( current-> data == current->next->data ) {
....
} else {
    current = current->next;
}

Also the returns need to be fixed (the first should return 0 and the second should be moved outside the while loop).

南巷近海 2024-10-05 11:50:29

一些快速观察:

return (count) 语句可能位于 while 循环之外,否则循环将提前终止。

while 循环内需要像 current = current->next; 这样的语句。否则,循环将变成无限循环。

Some quick observations:

The return (count) statement might be outside the while loop, otherwise the loop would be terminated prematurely.

A statement like current = current->next; is required inside the while loop. Otherwise, the loop would become an infinite loop.

悲喜皆因你 2024-10-05 11:50:29
  1. 如果没有重复匹配,当前应该移动到当前->下一个。
  2. 传递给函数的参数应该只是 *list (即指向 struct litem 类型元素的指针)
  3. delete 是一个 C++ 关键字。请改用free()

修改后的代码:

int remove_duplicates(struct litem *list)
{
    int count = 0; 
    struct litem * current = NULL;
    current = list;
    struct  litem *deleteNode;
    if (current == NULL ) return;
    while(current->next != NULL)
    {
        if ( current->data == current->next->data) 
        {
            count++;
            deleteNode = current->next;
                current->next= current->next->next;
                    free(deleteNode);
        }
        else
        {
            current = current->next;
        }
    }
    return (count);  
}
  1. current should move to current->next on no duplicate match.
  2. Argument passed to the function should be merely *list (ie. a pointer to an element of type struct litem)
  3. delete is a C++ keyword. Use free() instead.

The modified code:

int remove_duplicates(struct litem *list)
{
    int count = 0; 
    struct litem * current = NULL;
    current = list;
    struct  litem *deleteNode;
    if (current == NULL ) return;
    while(current->next != NULL)
    {
        if ( current->data == current->next->data) 
        {
            count++;
            deleteNode = current->next;
                current->next= current->next->next;
                    free(deleteNode);
        }
        else
        {
            current = current->next;
        }
    }
    return (count);  
}
入画浅相思 2024-10-05 11:50:29

试试这个:

int remove_duplicates(litem *&list)
{
   int count = 0; 
   struct litem * current = NULL;
   current = list;
   struct  litem *deleteNode;
   if (current == NULL ) return 0;
   while(current->next != NULL)
   {
     if (current->data == current->next->data) // check for the duplicates 
     {
       count++;
       deleteNode =current->next;
       current->next= current->next->next;
       delete deleteNode;
     }
     else
     {
       current = current->next;
     }
   }
   return (count);
 }

Try this:

int remove_duplicates(litem *&list)
{
   int count = 0; 
   struct litem * current = NULL;
   current = list;
   struct  litem *deleteNode;
   if (current == NULL ) return 0;
   while(current->next != NULL)
   {
     if (current->data == current->next->data) // check for the duplicates 
     {
       count++;
       deleteNode =current->next;
       current->next= current->next->next;
       delete deleteNode;
     }
     else
     {
       current = current->next;
     }
   }
   return (count);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文