2013-03-24 38 views
0

要求編寫一個函數split(),將鏈接表的內容複製到另外兩個鏈表中。該函數將具有偶數索引(0,2等)的節點複製到EvenList以及具有奇數索引的節點到oddList。原始鏈接列表不應該被修改。假設evenlist和oddlist將作爲空列表傳遞給函數(* ptrEvenList = * ptrOddList = NULL)。將鏈表拆分爲另外兩個表,其中一個具有奇數索引,另一個具有偶數索引

在我的程序中它可以顯示最初的列表。另外兩個列表有問題。它會導致程序的終止。

#include<stdio.h> 
#include<stdlib.h> 

#define SIZE 9 

//定義

typedef struct node{ 
    int item; 
    struct node *next; 
}ListNode; 

//調用函數

int search(ListNode *head,int value); 
void printNode(ListNode *head); 
void split(ListNode *head,ListNode **OddList,ListNode **EvenList); 

//主要功能

int main(){ 
    ListNode *head=NULL; 
    ListNode *temp; 
    ListNode *OddList=NULL; 
    ListNode *EvenList=NULL; 

//在節點列表的結構這個問題,它要求我通過兩個空白列表功能 'slipt'

int ar[SIZE]={1,3,5,2,4,6,19,16,7}; 
    int value=0; 
    int i; 

    head=(struct node*)malloc(sizeof(ListNode)); 
    temp=head; 
    for(i=0;i<SIZE;i++){ 
     temp->item=ar[i]; 
     if(i==(SIZE-1)) //last item 
      break; 
     temp->next=(struct node*)malloc(sizeof(ListNode)); 
     temp=temp->next; 
    } 
    temp->next=NULL; 
    printf("Current list:"); 
    printNode(head); 

    split(head,&OddList,&EvenList); 


return 0; 
} 

**** !!!!!!!!! 我認爲這個部分是這個問題。

void split(ListNode *head,ListNode **ptrOddList,ListNode **ptrEvenList){ 
    int remainder; 
    ListNode *tempO,*tempE,*temp; 



    if (head==NULL) 
     return; 
    else{ 
     temp=head; 

     *ptrOddList=(struct node*)malloc(sizeof(ListNode)); 
     *ptrEvenList=(struct node*)malloc(sizeof(ListNode)); 

     tempO=*ptrOddList; 
     tempE=*ptrEvenList; 

     while(temp!=NULL){ 
      remainder=temp->item%2; 

      if(remainder==0){ 
       tempE->next=(struct node*)malloc(sizeof(ListNode)); 
       tempE->item=temp->item; 
       tempE=tempE->next; 
      } 
      else{ 
       tempO->next=(struct node*)malloc(sizeof(ListNode)); 
       tempO->item=temp->item; 
       tempO=tempO->next; 
      } 
      temp=temp->next; 
     } 
     tempE=NULL; 
     tempO=NULL; 

//我也試過tempE->next=NULL;tempO->next=NULL //程序,如果我修改它像上面,但顯示的最後兩個數字將是兩個隨機數可以運行。

 printf("Even List:"); 
     printNode((*ptrEvenList)); 
     printf("Odd List:"); 
     printNode((*ptrOddList)); 
    } 
} 

//函數用來打印出結果

void printNode(ListNode *head){ 
    if (head==NULL) 
     return; 
    while(head!=NULL){ 
     printf("%d ",head->item); 
     head=head->next; 
    } 
    printf("\n"); 
} 
+1

請縮小它,沒有人會看那麼多的代碼 – 2013-03-24 13:47:54

回答

0

你總是一步太早分配好自己的節點。請注意,在開始時,您總是爲兩個列表分配一個節點 - 但如果列表中沒有奇數,該怎麼辦?那麼你的清單在這一點上已經太長了。每當你分配一個新節點時你都會這樣做。

您需要做的是將指針指針保存到每個列表的節點。這將是指向列表中最後一個節點的「下一個」指針的指針,如果列表爲空,則指向列表頭部的指針。接下來的指針和列表指針都應該以NULL開頭。

當您分配一個新節點時,使用指向節點的指針將最後一個「下一個」指針設置爲指向新節點。

+0

THX!我修復了這個程序,雖然看起來很複雜......我試圖找到一種更有效的方法並對其進行修改。 – Baller 2013-03-25 02:26:33

1
void split(ListNode *head, ListNode **ptrOddList, ListNode **ptrEvenList){ 

    for(; head ; head= head->next) { 
     ListNode *temp; 

     temp = malloc(sizeof *temp); 
     memcpy (temp, head, sizeof *temp); 

     if (temp->item %2) { *ptrOddList = temp; ptrOddList = &temp->next;} 
     else { *ptrEvenList = temp; ptrEvenList = &temp->next;} 
     } 

    *ptrOddList = NULL; 
    *ptrEvenList = NULL; 
} 
+0

抱歉,我無法關注你。什麼'ptrOddList =&temp-> next'mean?它是否與''* ptrOddList = temp-> next'一樣?最後,'* ptrOddList = NULL'的含義是什麼?如果我把它弄空,我怎樣才能打印出列表?我想我仍然需要一個溫和的指針來創建新的列表。其實我解決了這個問題。雖然,功能是複雜的...我會盡力改善它。 – Baller 2013-03-25 02:19:51

+0

我在下面的答案中附加了我的函數:) – Baller 2013-03-25 02:21:32

+0

ptrOddList是一個指向指針的指針。所以它應該指向一個指針。 temp-> next是一個指針。所以'ptrOddList =&temp-> next'分配給ptrOddList,使它指向temp-> next。 – wildplasser 2013-03-25 07:44:46

0
void split(ListNode *head,ListNode **ptrOddList,ListNode **ptrEvenList){ 
int remainder; 
int countO=0; 
int countE=0; 
ListNode *tempO,*tempE; 

if (head==NULL) 
    return; 
else{ 
    (*ptrOddList)=(struct node*)malloc(sizeof(ListNode)); 
    (*ptrEvenList)=(struct node*)malloc(sizeof(ListNode)); 

    while(head!=NULL){ 
     remainder=head->item%2; 

     if(remainder==0){ 
      if(countE>0){ 
       tempE->next=head; 
       tempE=tempE->next; 
      } 
          else 
           tempE=*ptrOddList; 
      tempE->item=head->item; 
      countE++; 

     } 
     else{ 
      if(countO>0){ 
       tempO->next=head; 
       tempO=tempO->next; 
      } 
          else 
           tempO=*ptrOddList; 
      tempO->item=head->item; 
      countO++; 

     } 
     head=head->next; 
    } 
    tempE->next=NULL; 
    tempO->next=NULL; 
    printf("Even List:"); 
    printNode((*ptrEvenList)); 
    printf("Odd List:"); 
    printNode((*ptrOddList)); 
} 
} 
0

我覺得這裏的問題是分裂列表成兩半基於索引。但我可以看到values(item%2)上的實現。

因此,在列表中,第一個節點的索引應爲1,第二個節點的值爲2,依此類推。

使用計數變量,該變量最初設置爲0。

int node_count = 0; 
while(head != NULL) 
{ 
    node_count ++; 

    if(node_count%2) 
    { 
     //prepare odd_list; 
    } 
    else 
    { 
     //prepare even_list; 
    } 
    head = head->next; 
} 
0
void split(ListNode *head, ListNode **pOddList, ListNode **pEvenList) 
{ 
    int remainder; 
    ListNode *tmpO = NULL, *tmpE= NULL, *tmp; 

    if (head == NULL) 
    { 
     *pEvenList = NULL; 
     *pOddList = NULL; 
    } 
    else 
    { 
     tmp = head; 

     while (tmp != NULL) 
     { 
      remainder = tmp->item % 2; 

      if (remainder == 0) 
      { 
       if (tmpE == NULL) 
       { 
        tmpE = tmp; 
        tmp = tmp->next; 
        *pEvenList = tmpE; 
        tmpE->next = NULL; 
       } 
       else 
       { 
        tmpE->next = tmp; 
        tmp = tmp->next; 
        tmpE = tmpE->next; 
        tmpE->next = NULL; 
       } 
      } 
      else 
      { 
       if (tmpO == NULL) 
       { 
        tmpO = tmp; 
        tmp = tmp->next; 
        *pOddList = tmpO; 
        tmpO->next = NULL; 
       } 
       else 
       { 
        tmpO->next = tmp; 
        tmp = tmp->next; 
        tmpO = tmpO->next; 
        tmpO->next = NULL; 
       } 
      } 
     } 
    } 
} 
+0

上面的代碼已經測試了整數列表的許多可能條件 – 2017-05-11 14:26:11

相關問題