2014-09-24 78 views
0
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 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; 
        } 
     } 
} 

我遵循這個代碼還,但它不能正常工作海合會compiler.where n爲節點的總數。

+0

一個簡單的解決辦法是保持調用刪除功能,直到它沒有找到問題的關鍵。 – arunmoezhi 2014-09-24 06:33:57

+0

@ arunmoezhi-先生,我正在嘗試,但無法取得成功。請給出一個代碼。 – SumS 2014-09-24 06:42:25

+0

爲什麼你有一個遞歸的代碼。儘量避免遞歸。在這種情況下,很容易爲列表遍歷編寫迭代代碼 – arunmoezhi 2014-09-24 06:45:44

回答

0
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; 
          } 
        } 
} 
+0

@ arunmoezhi-我認爲這是更簡單的邏輯,我第一次得不到它。 – SumS 2014-09-25 16:18:44

+0

你基本上正在嘗試做同樣的事情。 – arunmoezhi 2014-09-26 03:30:03

0

這是代碼。使用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("%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(); 
} 
相關問題