Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct Entry{
- int data;
- struct Entry *next;
- };
- struct Entry *root = NULL;
- void printListe()
- {
- struct Entry *rootK;
- rootK = root;
- if(rootK == NULL)
- printf("Kein Eintrag\n");
- else
- {
- for(rootK;rootK != NULL;rootK=rootK->next)
- printf("Adresse: %p Daten: %d\n", rootK, rootK->data);
- }
- printf("\n");
- }
- void ElementSuche(int Suche)
- {
- int Gefunden = 0;
- struct Entry *rootK;
- rootK = root;
- if(rootK == NULL)
- {
- printf("Kein Eintrag\n\n");
- return;
- }
- else
- {
- for(rootK;rootK != NULL;rootK=rootK->next)
- {
- if((rootK->data) == Suche)
- {
- printf("Adresse: %p Daten: %d\n", rootK, rootK->data);
- Gefunden = 1;
- }
- }
- }
- printf("\n");
- if(Gefunden == 0)
- printf("Kein Eintrag unter dieser Suche\n\n");
- }
- void ElementEinfuegen(int Daten)
- {
- struct Entry *Neu = (struct Entry *)malloc(sizeof(struct Entry)); //malloc liefert einen Void Pointer -> Typecast
- Neu->data = Daten;
- Neu->next = root;
- root = Neu;
- printf("\n");
- }
- void ElementLoeschen(int SucheL)
- {
- int Gefunden = 0;
- struct Entry *rootK;
- struct Entry *Buffer = NULL;
- rootK = root;
- if(rootK == NULL)
- {
- printf("Kein Eintrag\n\n");
- return;
- }
- else
- {
- for(rootK;rootK != NULL; Buffer=rootK, rootK=rootK->next)
- {
- if((rootK->data) == SucheL)
- {
- if(root==rootK)
- {
- root = rootK->next;
- }
- else if(Buffer != NULL)
- {
- Buffer->next=rootK->next;
- rootK->next=NULL;
- rootK=Buffer->next;
- }
- else if((rootK->next) == NULL)
- {
- Buffer->next=NULL;
- }
- else
- {
- root=rootK->next;
- }
- Gefunden = 1;
- }
- }
- }
- printf("\n");
- if(Gefunden == 0)
- printf("Kein Eintrag unter dieser Suche\n\n");
- }
- int main()
- {
- int i, Daten, Suche, SucheL;
- /*
- struct Entry DatenA; //Neue Struktur wird erstellt
- DatenA.data = 20;
- root = &DatenA; //In den Pointer root wird die Adresse von DatenA geschrieben
- struct Entry DatenB; //Neue Struktur wird erstellt
- DatenB.data = 10;
- DatenB.next = NULL;
- DatenA.next = &DatenB; //In den Pointer root wird die Adresse von DatenB geschrieben
- */
- do
- {
- printf("-------------------------\n");
- printf("Ausgeben ............ 1\n");
- printf("Einfuegen ............ 2\n");
- printf("Suchen ............ 3\n");
- printf("Loeschen ............ 4\n");
- printf("Beenden ............ 0\n");
- printf("-------------------------\n\n");
- scanf("%d%*c", &i);
- if(i < 0 || i > 4)
- {
- printf("Nicht in der Auswahl !\n\n");
- }
- else
- {
- switch(i)
- {
- case(1):
- {
- printListe();
- break;
- }
- case(2):
- {
- printf("Daten (int):");
- scanf("%d%*c", &Daten);
- ElementEinfuegen(Daten);
- break;
- }
- case(3):
- {
- printf("Suche nach (int):");
- scanf("%d%*c", &Suche);
- ElementSuche(Suche);
- }
- case(4):
- {
- printf("Suchen und Loeschen (int):");
- scanf("%d%*c", &SucheL);
- ElementLoeschen(SucheL);
- }
- }
- }
- }while(i != 0);
- return 0;
- }
Add Comment
Please, Sign In to add comment