Gcc 删除列表中相同元素的重复出现

Gcc 删除列表中相同元素的重复出现,gcc,data-structures,linked-list,Gcc,Data Structures,Linked List,我引用上面的代码从列表中删除一个元素,但它不能删除所有重复的值。请更正以上代码或给出任何简单的代码。列表是单线性的 期待 struct node * del(struct node * temp1,int num) { struct node *temp2; temp2=NULL; if(temp1==NULL) { return NULL; } i

我引用上面的代码从列表中删除一个元素,但它不能删除所有重复的值。请更正以上代码或给出任何简单的代码。列表是单线性的 期待

struct node * del(struct node * temp1,int num)
{
         struct node *temp2;
         temp2=NULL;

         if(temp1==NULL)
         {
                  return NULL;
         }

         if(temp1->data==num)
         {
                  temp2=temp1->next;
                  free(temp1);
                  return temp2;
         }
         else
           {
                  temp1->next=del(temp1->next,num);
           }

           return temp1;
}
input list=11 22 11 33 11 44
输出-删除11个列表后=22 33 44
void del()
{
int i,d;
结构节点*列表,*临时;
printf(“输入要删除的数据”);
scanf(“%d”、&d);
列表=开始;
对于(i=1;idata=d)
{
温度=启动;
开始=开始->下一步;
列表=开始;
免费(临时);
}
其他的
{
如果(列表->下一步->数据==d)
{
如果(列表->下一步->下一步==NULL)
{
临时=列表->下一步;
列表->下一步=空;
免费(临时);
打破
}
临时=列表->下一步;
列表->下一步=临时->下一步;
列表=列表->下一步;
免费(临时);
继续;
}
列表=列表->下一步;
}
}
}

我也遵循这段代码,但它在gcc编译器上无法正常工作。其中n是节点总数。

以下是代码。使用
insertattend()
插入重复的元素,使用
removeAll()
删除所有出现的键

input list = 11 22 11 33 11 44 

output - after deletion of 11 list = 22 33 44

void del()
{
         int i,d;
     struct node *list,*temp;
         printf("Enter data to delete\t");
         scanf("%d",&d);
         list=start;
         for(i=1;i<n;i++)
         {
                  if(start->data==d)
                  {
                     temp=start;
                     start=start->next;
                     list=start;
                     free(temp);
                  }
                  else
                  {
                           if(list->next->data==d)
                           {
                             if(list->next->next==NULL)
                             {
                                 temp=list->next;
                                 list->next=NULL;
                                 free(temp);
                                 break;
                             }
                             temp=list->next;
                             list->next=temp->next;
                             list=list->next;
                             free(temp);
                             continue;
                           }

                           list=list->next;
                  }
         }
}
#包括
#包括
#包括
结构节点
{
无符号长密钥;
结构节点*下一步;
};
结构seekRecord
{
结构节点*prev;
结构节点*curr;
};
结构节点*R;
结构seekRecord*seekRecord;
结构节点*createNode(无符号长键)
{
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
新建节点->键=键;
newNode->next=NULL;
}
void createSentineNode()
{
R=createNode(ULONG_MAX);
}
无效搜索(无符号长键)
{
结构节点*prev;
结构节点*curr;
结构节点*下一步;
prev=NULL;
curr=R;
next=R->next;
while(下一步!=NULL&&curr->key!=key)
{
上一次=当前;
curr=next;
下一步=下一步->下一步;
}
参见记录->上一页=上一页;
参见记录->当前=当前;
返回;
}
布尔搜索(无符号长键)
{
寻找(关键);
返回seekRecord->curr->key==key;
}
布尔插入(无符号长键)
{
结构节点*curr;
寻找(关键);
curr=参见记录->curr;
如果(当前->键==键)
{
返回false;
}
curr->next=createNode(键);
返回true;
}
bool remove(无符号长键)
{
结构节点*prev;
结构节点*curr;
寻找(关键);
curr=参见记录->curr;
prev=参见记录->prev;
如果(当前->键!=键)
{
返回false;
}
上一个->下一个=当前->下一个;
免费(货币);
返回true;
}
void insertattend(无符号长键)
{
结构节点*curr;
curr=R;
while(当前->下一步!=NULL)
{
当前=当前->下一步;
}
curr->next=createNode(键);
返回;
}
void removeAll(无符号长键)
{
结构节点*prev;
结构节点*curr;
while(true)
{
寻找(关键);
curr=参见记录->curr;
prev=参见记录->prev;
如果(当前->键!=键)
{
返回;
}
上一个->下一个=当前->下一个;
免费(货币);
}
}
作废打印列表()
{
结构节点*curr;
curr=R->next;
while(curr!=NULL)
{
printf(“%lu\t”,curr->key);
当前=当前->下一步;
}
printf(“\n”);
返回;
}
int main()
{
seekRecord=(struct seekRecord*)malloc(sizeof(seekRecord));
createSentineNode();
插入者(11);
插入者(22);
插入者(33);
插入者(11);
插入者(44);
打印列表();
removeAll(11);
打印列表();
}

一个简单的解决方案是不断调用delete函数,直到它找不到键为止。@arunmoezhi-先生,我正在尝试,但没有成功。请给出一个代码。为什么有递归代码。尽量避免递归。在这种情况下,很容易为列表编写迭代代码walk@arunmoezhi-我使用for循环编写了一个代码,该代码在TC上正常工作,但在GCC上无法工作,所以我正在尝试其他方法,比如递归。然后发布你尝试过的解决方案GCC@arunmoezhi-我认为这是一个更简单的逻辑。我第一次无法理解。你基本上是在尝试做同样的事情。
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>

struct node
{
    unsigned long key;
    struct node* next;
};

struct seekRecord
{
    struct node* prev;
    struct node* curr;
};

struct node* R;
struct seekRecord* seekRecord;

struct node* createNode(unsigned long key)
{
    struct node* newNode = (struct node*) malloc(sizeof(struct node));
    newNode->key = key;
    newNode->next = NULL;
}

void createSentinelNode()
{
    R = createNode(ULONG_MAX);
}

void seek(unsigned long key)
{
    struct node* prev;
    struct node* curr;
    struct node* next;
    prev = NULL;
    curr = R;
    next = R->next;
    while(next != NULL && curr->key != key)
    {
        prev = curr;
        curr = next;
        next = next->next;
    }
    seekRecord->prev = prev;
    seekRecord->curr = curr;
    return;
}

bool search(unsigned long key)
{
    seek(key);
    return seekRecord->curr->key == key;
}

bool insert(unsigned long key)
{
    struct node* curr;
    seek(key);
    curr = seekRecord->curr;
    if(curr->key == key)
    {
        return false;
    }
    curr->next = createNode(key);
    return true;
}

bool remove(unsigned long key)
{
    struct node* prev;
    struct node* curr;
    seek(key);
    curr = seekRecord->curr;
    prev = seekRecord->prev;
    if(curr->key != key)
    {
        return false;
    }
    prev->next = curr->next;
    free(curr);
    return true;
}

void insertAtEnd(unsigned long key)
{
    struct node* curr;
    curr = R;
    while(curr->next != NULL)
    {
        curr = curr->next;
    }
    curr->next = createNode(key);
    return;
}

void removeAll(unsigned long key)
{
    struct node* prev;
    struct node* curr;
    while(true)
    {
        seek(key);
        curr = seekRecord->curr;
        prev = seekRecord->prev;
        if(curr->key != key)
        {
            return;
        }
        prev->next = curr->next;
        free(curr);
    }
}

void printList()
{
    struct node* curr;
    curr=R->next;
    while(curr != NULL)
    {
        printf("%lu\t",curr->key);
        curr = curr->next;
    }
    printf("\n");
    return;
}

int main()
{
    seekRecord = (struct seekRecord*) malloc(sizeof(seekRecord));
    createSentinelNode();
    insertAtEnd(11);
    insertAtEnd(22);
    insertAtEnd(33);
    insertAtEnd(11);
    insertAtEnd(44);
    printList();
    removeAll(11);
    printList();
}
void delall(int del)
{
                  struct node *t1,*temp;
                  t1=NULL;
                  temp=start;
                  while(temp!=NULL&&temp->data==del)
                  {
                           t1=temp;
                           temp=temp->next;
                           start=temp;
                           free(t1);
                  }
                  while(temp!=NULL)
                  {
                           if(temp->data==del)
                           {
                                    t1->next=temp->next;
                                    free(temp);
                                    temp=t1->next;

                           }
                           else
                           {
                                    t1=temp;
                                    temp=temp->next;
                           }
                  }
}