2016-11-06 17 views
-4

如何顛倒我的列表?如何在C中反轉我的列表?

我的函數的printList打印:0,1,2,3,4,5,6,7,8,9

但是,我想:9,8,7,6,5,4,3 ,2,1,0。

struct Node 
{ 
    TElement Element; 
    Position Next; 
}; 

Position Header(List L) 
{ 
    return L; 
} 

Position Advance(Position P) 
{ 
    return P->Next; 
} 

void PrintList(const List L){ 

    Position P = Header(L); 
    if(IsEmpty(L)) 
     printf("Empty list\n"); 
    else 
    { 
     do 
     { 
      P = Advance(P); 
      printf("%d ", Retrieve(P)); 
     } while(!IsLast(P, L)); 
     printf("\n"); 
    } 
} 

int main() 
{ 
    List L1; 
    Position P; 
    int i; 

    P = Header(L1); 
    for(i = 0; i < 10; i++){ 
     Insert(i, L1, P); 
     P = Advance(P); 
    } 
    printf(" List L1: "); 
    PrintList(L1); 
} 
+1

什麼是標題?什麼是「提前」? ... –

+0

位置標題(列表L) { return L; } – Henrix

+0

也許編輯的問題會更好 –

回答

-2
int main() 
{ 
    List L1; 
    Position P; 
    int i; 

    P = Header(L1); 
    for(i = 0; i < 10; i++){ 
     Insert(i, L1, P); 
     P = Advance(P); 
    } 
    printf(" List L1: "); 
    PrintList(L1); 
} 
+0

這個答案沒有意義 –

+0

@EdHeal,我不認爲這是答案,而是提供更多源代碼的OP。我已經將它添加到原始問題的源代碼中,並重新格式化了所有內容。 – cdlane

0

簡短的回答:,因爲你有一個單向鏈表,想向後打印出來,你需要步行列表收集物品到另一個結構(以下數組),然後走這向後:

#define LARGE_ENOUGH 1024 

void PrintList(const List L) { 

    if (IsEmpty(L)) { 
     printf("Empty list\n"); 
     return; 
    } 

    Position P = Header(L); 
    TElement Elements[LARGE_ENOUGH]; 
    int i = 0; 

    do { 
     P = Advance(P); 
     Elements[i++] = Retrieve(P); 
    } while (!IsLast(P, L)); 

    while (--i >= 0) { 
     printf("%d ", Elements[i]); 
    } 

    printf("\n"); 
} 

產生

> ./a.out 
List L1: 9 8 7 6 5 4 3 2 1 0 
> 

長的答案:我拼湊出你的代碼的缺失份從此list.h & list.c並取得沿途的幾個變化:

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

typedef int TElement; 

typedef struct Node *PtrToNode; 
typedef PtrToNode List; 
typedef PtrToNode Position; 

struct Node { 
    TElement Element; 
    Position Next; 
}; 

Position Header(List L) 
{ 
    return L; 
} 

int IsLast(Position P, List L) 
{ 
    return P->Next == NULL; 
} 

int IsEmpty(List L) 
{ 
    return L->Next == NULL; 
} 

Position Advance(Position P) 
{ 
    return P->Next; 
} 

TElement Retrieve(Position P) { 
    return P->Element; 
} 

int FatalError (char *ErrorMessage) { 
    fprintf(stderr, "%s\n", ErrorMessage); 
    exit(1); 
} 

void Insert(TElement X, List L, Position P) { 
    Position TmpCell = malloc(sizeof(struct Node)); 

    if (TmpCell == NULL) { 
     FatalError("Out of space!"); 
    } 

    TmpCell->Element = X; 
    TmpCell->Next = P->Next; 
    P->Next = TmpCell; 
} 

#define LARGE_ENOUGH 1024 

void PrintList(const List L) { 

    if (IsEmpty(L)) { 
     printf("Empty list\n"); 
     return; 
    } 

    Position P = Header(L); 
    TElement Elements[LARGE_ENOUGH]; 
    int i = 0; 

    do { 
     P = Advance(P); 
     Elements[i++] = Retrieve(P); 
    } while (!IsLast(P, L)); 

    while (--i >= 0) { 
     printf("%d ", Elements[i]); 
    } 

    printf("\n"); 
} 

int main() { 
    List L1 = calloc(1, sizeof(struct Node)); 

    Position P = Header(L1); 

    for (int i = 0; i < 10; i++) { 
     Insert(i, L1, P); 
     P = Advance(P); 
    } 

    printf(" List L1: "); 
    PrintList(L1); 
} 

以上是誰可能會喜歡其他人的利益回答你的問題。