1 / 21

Animation einer sortierten einfach verketteten Liste

Animation einer sortierten einfach verketteten Liste. Frank Göttler (Institut für Informatik, Johannes Gutenberg – Universität Mainz, Praktikum 2007). X. Eine Variable - hier x - ist ein programmiersprachliches Konzept, das aus drei

jela
Download Presentation

Animation einer sortierten einfach verketteten Liste

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Animation einer sortierten einfach verketteten Liste Frank Göttler (Institut für Informatik, Johannes Gutenberg – Universität Mainz, Praktikum 2007)

  2. X Eine Variable - hier x - ist ein programmiersprachliches Konzept, das aus drei Komponenten besteht, dem externen Bezeichner, einer Speicheradresse und einem Wert, auf den man, vom Bezeichner ausgehend, über die Adresse zugreifen kann. Eine geeignete Visualisierung einer („einfachen“) Variablen vom Typ integer könnte so ausschauen:

  3. P Eine Zeigervariable – hier p – hat ebenfalls eine Adresse, aber einen Wert, der eine Adresse ist. Eine geeignete Darstellung einer Variablen vom Typ Zeiger-auf-integer könnte so ausschauen:

  4. X P int int * &X &P *P Die Programmiersprache C erlaubt den expliziten Zugriff auf die Adressen von Variablen mittels des &-Operators. Außerdem kann man mittels des *-Operators auf den Wert einer Variablen zugreifen, vorausgesetzt der Wert der Zeigervariablen ist die Adresse einer (einfachen) Variablen („Dereferenzierung“). Die untere Darstellung zeigt die Komponenten bei einer int-Variablen X und einer int-Zeigervariablen P.

  5. if (kopf->wert > zahl) { hp1 = kopf; kopf = hp; hp->next = hp1; einsortiert = 1; } else { hp1 = kopf; hp2 = kopf->next; while (hp2 != 0) { if (hp2->wert < zahl) { hp1 = hp2; hp2 = hp2->next; } else { hp->next = hp2; hp1->next = hp; einsortiert = 1; break; } } // Element wurde als größtes Element erkannt und wird am Ende einsortiert if (!einsortiert) { hp->next = 0; hp1->next = hp; } } // Nächstes Element einlesen printf ("Gebe Zahl ein!> "); scanf ("%d", &zahl ); einsortiert = 0; } /* Ausgabe der Liste */ hp = kopf; while (hp != 0) { printf ("Wert: %d\n", hp->wert ); hp = hp->next; } return 0; } #include <stdio.h> #include <stdlib.h> typedef struct Eintrag { int wert; struct Eintrag *next; } Eintrag; int main (void) { Eintrag *kopf,*hp, *hp1, *hp2; int zahl, einsortiert = 0; // Abbruch, wenn gleich als erstes Element eine Null eingegeben wird printf ("Gebe Zahl ein!> "); scanf ("%d", &zahl ); if (zahl == 0) return 0; // Einfügen des ersten Elements if ((hp = (Eintrag *) malloc (sizeof(Eintrag))) == 0) { printf("Zu wenig Speicher\n"); exit(1); } hp->wert = zahl; hp->next = 0; kopf = hp; // Einfügen weiterer Elemente printf ("Gebe Zahl ein!> "); scanf ("%d", &zahl ); while (zahl != 0) { if ((hp = (Eintrag *) malloc (sizeof(Eintrag))) == 0) { printf("Zu wenig Speicher\n"); exit(1); } hp->wert = zahl;

  6. hp2 hp kopf hp1 zahl ein- sortiert Stack … … … Heap int main (void) { Eintrag *kopf,*hp, *hp1, *hp2; int zahl, einsortiert = 0; // Abbruch, wenn gleich als erstes Element eine Null eingegeben wird printf ("Gebe Zahl ein!> "); scanf ("%d", &zahl ); if (zahl == 0) return 0; // Einfügen des ersten Elements if ((hp = (Eintrag *) malloc (sizeof(Eintrag))) == 0) { printf("Zu wenig Speicher\n"); exit(1); } hp->wert = zahl; hp->next = 0; kopf = hp; 0

  7. hp2 hp kopf hp1 zahl ein- sortiert Stack … … … Heap int main (void) { Eintrag *kopf,*hp, *hp1, *hp2; int zahl, einsortiert = 0; // Abbruch, wenn gleich als erstes Element eine Null eingegeben wird printf ("Gebe Zahl ein!> "); scanf ("%d", &zahl ); if (zahl == 0) return 0; // Einfügen des ersten Elements if ((hp = (Eintrag *) malloc (sizeof(Eintrag))) == 0) { printf("Zu wenig Speicher\n"); exit(1); } hp->wert = zahl; hp->next = 0; kopf = hp; 0 33

  8. kopf hp hp1 hp2 …malloc(…) zahl ein- sortiert A 0 33 … … … Heap // Einfügen des ersten Elements if ((hp = (Eintrag *) malloc(sizeof(Eintrag))) == 0) { printf("Zu wenig Speicher\n“); exit(1); } hp->wert = zahl; hp->next = 0; kopf = hp; A A 33 0 A

  9. hp hp1 hp2 kopf …malloc(…) zahl B A ? != 0 … … … Heap // Einfügen weiterer Elemente printf ("Gebe Zahl ein!> "); scanf ("%d", &zahl ); while (zahl != 0) { if ((hp = (Eintrag *) malloc (sizeof(Eintrag))) == 0) { …. } hp->wert = zahl; if (kopf->wert > zahl) { …. } else { hp1 = kopf; hp2 = kopf->next; while (hp2 != 0) { ……. } // Element wurde als größtes Element erkannt, wird am Ende einsortiert if (!einsortiert) { hp->next = 0; hp1->next = hp; } } ….. } A B 45 45 0 33 B A

  10. // Einfügen weiterer Elemente printf ("Gebe Zahl ein!> "); scanf ("%d", &zahl ); while (zahl != 0) { if ((hp = (Eintrag *) malloc (sizeof(Eintrag))) == 0) { …. } hp->wert = zahl; if (kopf->wert > zahl) { …. } else { hp1 = kopf; hp2 = kopf->next; while (hp2 != 0) { ……. } // Element wurde als größtes Element erkannt, wird am Ende einsortiert if (!einsortiert) { hp->next = 0; hp1->next = hp; } } ….. } hp hp1 hp2 kopf A = 0 … … … Heap B A 0 0 45 33 B A

  11. // Einfügen weiterer Elemente printf ("Gebe Zahl ein!> "); scanf ("%d", &zahl ); while (zahl != 0) { if ((hp = (Eintrag *) malloc (sizeof(Eintrag))) == 0) { …. } hp->wert = zahl; if (kopf->wert > zahl) { …. } else { hp1 = kopf; hp2 = kopf->next; while (hp2 != 0) { ……. } // Element ist größtes und wird am Ende einsortiert if (!einsortiert) { hp->next = 0; hp1->next = hp; } } ….. } hp hp1 hp2 kopf ein- sortiert 0 A 0 A … … … Heap B B A 0 0 45 33 B A

  12. while (zahl != 0) { …. // Nächstes Element einlesen printf ("Gebe Zahl ein!> "); scanf ("%d", &zahl ); einsortiert = 0; } hp hp1 hp2 kopf zahl ein- sortiert 0 A 0 A … … … Heap B B A 0 56 0 B 45 33 B A

  13. hp2 hp2 kopf …malloc(…) hp1 C 0 A zahl ? 56 != 0 != 0 … … … Heap while (zahl != 0) { if ((hp = (Eintrag *) malloc (sizeof(Eintrag))) == 0) { … } hp->wert = zahl; if (kopf->wert > zahl) { … } else { hp1 = kopf; hp2 = kopf->next; while (hp2 != 0) { if (hp2->wert < zahl) { hp1 = hp2; hp2 = hp2->next; } else { … } } // Element ist größtes Element, wird am Ende einsortiert if (!einsortiert) { hp->next = 0; hp1->next = hp; } } … } hp A B 0 A C 56 0 B B 45 33 C B A

  14. hp2 hp2 kopf hp1 0 A zahl ? 56 … … … Heap while (zahl != 0) { if ((hp = (Eintrag *) malloc (sizeof(Eintrag))) == 0) { … } hp->wert = zahl; if (kopf->wert > zahl) { … } else { hp1 = kopf; hp2 = kopf->next; while (hp2 != 0) { if (hp2->wert < zahl) { hp1 = hp2; hp2 = hp2->next; } else { … } } // Element ist größtes Element, wird am Ende einsortiert if (!einsortiert) { hp->next = 0; hp1->next = hp; } } … // Nächstes Element einlesen …. } hp B A C C B B A 0 56 56 0 0 0 B B 45 33 C B A

  15. while (zahl != 0) { …. // Nächstes Element einlesen printf ("Gebe Zahl ein!> "); scanf ("%d", &zahl ); einsortiert = 0; } kopf hp2 hp2 ein- sortiert hp1 0 zahl != 0 … … … Heap hp B 0 C A 0 56 50 0 C B 33 45 56 C A B

  16. hp2 hp2 kopf …malloc(…) ein- sortiert hp1 D 0 zahl ? ? != 0 … … … Heap while (zahl != 0) { if ((hp = (Eintrag *) malloc (sizeof(Eintrag)))==0) {......} hp->wert = zahl; if (kopf->wert > zahl) { ... } else { hp1 = kopf; hp2 = kopf->next; while (hp2 != 0) { if (hp2->wert < zahl) { hp1 = hp2; hp2 = hp2->next } else { hp->next = hp2; hp1->next = hp; einsortiert = 1; break; } } .... } .... } hp B 0 C A A D 0 50 50 0 C B B 33 45 56 C D A B

  17. hp2 hp2 kopf ein- sortiert hp1 0 zahl ? ? != 0 … … … Heap while (zahl != 0) { if ((hp = (Eintrag *) malloc (sizeof(Eintrag)))==0) {......} hp->wert = zahl; if (kopf->wert > zahl) { ... } else { hp1 = kopf; hp2 = kopf->next; while (hp2 != 0) { if (hp2->wert < zahl) { hp1 = hp2; hp2 = hp2->next } else { hp->next = hp2; hp1->next = hp; einsortiert = 1; break; } } .... } .... } hp A B D A A B 0 50 50 0 C C B B 33 45 56 50 C D A B

  18. hp hp2 kopf hp2 ein- sortiert D D hp1 0 zahl ? while (zahl != 0) { if ((hp = (Eintrag *) malloc (sizeof(Eintrag)))==0) {......} hp->wert = zahl; if (kopf->wert > zahl) { ... } else { hp1 = kopf; hp2 = kopf->next; while (hp2 != 0) { if (hp2->wert < zahl) { hp1 = hp2; hp2 = hp2->next } else { hp->next = hp2; hp1->next = hp; einsortiert = 1; break; } } .... } .... } D C B C A A 0 1 50 50 0 B B C 33 45 56 50 … … … C Heap D A B

  19. while (zahl != 0) { …. // Nächstes Element einlesen printf ("Gebe Zahl ein!> "); scanf ("%d", &zahl ); einsortiert = 0; } hp hp2 kopf ein- sortiert hp1 zahl != 0 B A C D 0 1 50 17 0 C B D 33 45 56 50 … … … C Heap D A B

  20. hp hp2 kopf …malloc(…) ein- sortiert hp1 E zahl ? while (zahl != 0) { if ((hp = (Eintrag *) malloc (sizeof(Eintrag)))==0) {......} hp->wert = zahl; if (kopf->wert > zahl) { ...wird gleich relevant …. } else { hp1 = kopf; hp2 = kopf->next; while (hp2 != 0) { if (hp2->wert < zahl) { hp1 = hp2; hp2 = hp2->next } else { hp->next = hp2; hp1->next = hp; einsortiert = 1; break; } } .... } .... } B A C D E 0 17 17 0 C B D 33 45 56 50 … … … C Heap D A B E

  21. if (kopf->wert > zahl) { hp1 = kopf; kopf = hp; hp->next = hp1; einsortiert = 1; } …. hp kopf hp2 ein- sortiert hp1 zahl A C E E B A A 1 0 17 0 C B D 17 33 45 56 50 … … … C Heap D A B E

More Related