2016-11-06 4 views
-4

Wie umgekehrt meine Liste?Wie umgekehrt meine Liste in C?

Meine Funktion druckt Druck: 0,1,2,3,4,5,6,7,8,9

Aber ich will: 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

Was ist 'Header'? Was ist "Fortschritt"? .... –

+0

Positionskopf (Liste L) { return L; } – Henrix

+0

Vielleicht bearbeiten Sie die Frage wäre besser –

Antwort

-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

Diese Antwort ist bedeutungslos –

+0

@EdHeal, ich glaube nicht, dass dies eine Antwort ist, sondern eher das OP, um mehr von seinem Quellcode zur Verfügung zu stellen. Ich habe es der Quelle in der ursprünglichen Frage hinzugefügt und alles neu formatiert. – cdlane

0

Die kurze Antwort:, da Sie eine einfach verkettete Liste haben und wollen, dass es nach hinten drucken, müssen Sie die Liste zu sammeln Gegenstände in eine andere Struktur (ein Array unten) und dann zu Fuß gehen, dass rückwärts:

#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"); 
} 

ERZEUGT

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

Die lange Antwort: pieced ich von diesem list.h & list.c und machte ein paar Änderungen an der Art und Weise, die fehlenden Teile des Codes zusammen:

#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); 
} 

Die oben ist zum Wohle der anderen, die vielleicht gefallen zu Machen Sie einen Sprung bei der Beantwortung Ihrer Frage.