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


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 
output - after deletion of 11 list = 22 33 44
void del()
{
         int i,d;
     struct node *list,*temp;
         printf("Enter data to deletet");
         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;
                  }
         }
}

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

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

#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("%lut",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;
                           }
                  }
}

相关内容

  • 没有找到相关文章

最新更新