2013-11-15 44 views
5

我一直在考慮的任務創建爲C.鏈表我停留在交換方法,它只是似乎亂了整個鏈表的各種方法。有人對我出錯的地方有任何建議嗎?乾杯!交換位置用C

這是我的代碼。

int main(int argc, char* argv[]) 
{ 
    // A list of pointers to Reminders 
    const int MAX_ENTRIES = 10; 
    int numOfEntries = 0 ; 
    reminder_t* pFirst = (reminder_t*) malloc (sizeof(reminder_t)); 
    reminder_t* pSecond = (reminder_t*) malloc (sizeof(reminder_t)); 
    reminder_t* pThird = (reminder_t*) malloc (sizeof(reminder_t)); 
    reminder_t* pStart = NULL; 
    if (pFirst != NULL) 
    { 
     strcpy(pFirst->message, "Mikes Birthday"); 
     pFirst->dateOfEvent.day= 1; 
     pFirst->dateOfEvent.month= 1; 
     pFirst->dateOfEvent.year= 2013; 
     pFirst->pNext = NULL; 
    } 

    if (pSecond != NULL) 
    { 
     strcpy(pSecond->message, "Als Soccer Match"); 
     pSecond->dateOfEvent.day= 2; 
     pSecond->dateOfEvent.month= 2; 
     pSecond->dateOfEvent.year= 2013; 
     pSecond->pNext = NULL; 
    } 

    if (pThird != NULL) 
    { 
     strcpy(pThird->message, "School Concert"); 
     pThird->dateOfEvent.day= 3; 
    pThird->dateOfEvent.month= 3; 
    pThird->dateOfEvent.year= 2013; 
    pThird->pNext = NULL; 
} 

pFirst->pNext = pSecond; 
pSecond->pNext = pThird; 
pThird->pNext = NULL; 
pStart = pFirst; 

printf("\n------Before------\n"); 
listEntries(pStart); 
swapPositonOf(pFirst,pThird); 

printf("\n------After-aa-----\n"); 
listEntries(pStart); 

getchar(); 
return 0; 
} 

void listEntries(reminder_t * pList) 
{ 
    printf("\n"); 
    while (pList != NULL) 
    { 
      printf("%s\n", pList->message); 
     pList = pList->pNext; 
    } 
} 

void swapPositonOf(reminder_t* first , reminder_t* second) 
{ 
    reminder_t* pFirst = (reminder_t*) first; 
reminder_t* pSecond = (reminder_t*) second; 
reminder_t* temp = second->pNext; 

pSecond->pNext = pFirst->pNext; 
pFirst->pNext = temp; 
temp = pSecond; 
pSecond = pFirst; 
pFirst = temp; 
} 

預期輸出:

------Before------ 

Mikes Birthday 
Als Soccer Match 
School Concert 

------After-aa----- 
School Concert 
Als Soccer Match  
Mikes Birthday 

輸出:

------Before------ 

Mikes Birthday 
Als Soccer Match 
School Concert 

------After-aa----- 

Mikes Birthday 
+0

請提供更多信息:確切地說,當您對列表進行排序時會發生什麼?什麼是投入,產出和預期產出? – razlebe

+0

交換功能和提醒的定義以外的代碼是否真的有必要? – BrainSteel

+0

爲什麼在'swapPositionOf'的開頭多餘的強制轉換? (爲什麼首先賦值爲'pFirst'&second?) – Kninnug

回答

1

如果你想交換的的內容列表節點,那麼這並不困難:您可以在兩個節點中爲messagedateOfEvent字段進行交換。

但是,如果你想交換這些節點的位置(如函數的名稱所示),那麼你必須注意pNext數據成員。
實際上,只交換節點指針是不夠的。
您需要firstlast找到節點的位置,並且這樣做:

/* reminder_t* beforeFirst, reminder_t* beforeSecond */ 
beforeFirst->pNext = second; 
beforeSecond->pNext = first; 

和交換first->pNextsecond->pNext

此外,在這些列表實現中,通常重要的是注意特殊情況,比如頭節點和尾節點。

+0

我必須交換位置好,抱歉,我應該在問題中說明。稍後需要使用此方法進行排序。 – user2993328

+0

沒問題,讀取交換函數的名字已經足夠清楚了。交換指針比交換有效負載更快;鏈接列表節點交換確實有意義。 –

+0

好我的講師給了空函數: 無效swapPositonOf(reminder_t *第一,reminder_t *秒){} 或可能 INT swapPositonOf(reminder_t *第一,reminder_t *秒){} 那麼,怎樣才能LinkedList的獲得以前(根據我知道我會在星期一上課時要求確定,它不能是一個雙鏈表) – user2993328

2

你是不是修改pNext指針的節點只是first之前和second節點。

您需要將位於「first節點」之前的節點的pNext指向「second節點」,反之亦然。

假設鏈表:

Node_A -> Node_B -> Node_C -> Node_D -> Node_E

你必須交換節點_B和Node_D:
鏈接總數突破和形式:

  1. 舊鏈接:Node_A -> Node_B ....新鏈接:Node_A -> Node_D
  2. 舊鏈接:Node_B -> Node_C ....新建鏈接:Node_D -> Node_C
  3. 舊鏈接:Node_C -> Node_D ....新建鏈接:Node_C -> Node_B
  4. 舊鏈接:Node_D -> Node_E ....新建鏈接:Node_B -> Node_E

還記得角落情況下,像NULL指針和連續的節點。

2

您還沒有交換是正確的,那該節點的第一個節點之前和第二個節點之前的節點?

2

憑藉單鏈表,你不能直接找到列表元素之前的要交換的元素。你有第一,第二,你可以直接操縱它們,但是你沒有first.prev和second.prev。

您需要遍歷列表,並找出哪些是以前的你要交換(first_previous,second_previous)兩個節點的節點。那麼你節點交換也需要交換下一個這些以前的節點。

reminder_t* first_prev, *second_prev; 
first_prev = second_prev = pStart; 
reminder_t* iter; 
for(iter = pStart; iter; iter=iter->next) 
{ 
    if(iter->next == first) first_prev = iter; 
    if(iter->next == second) second_prev = iter; 
} 

您將需要修復的上述處理空列表,一個元素的列表,以及第一或第二,在合格名單的頭...

+0

構造單鏈表的一種方法是讓最後一個元素的下一個指針指向第一個元素(循環列表)。然後你可以遍歷給定的任何一個元素的整個列表。 – ChuckCottrill