Guest User

Untitled

a guest
Jan 12th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Entry{
  5.     int data;
  6.     struct Entry *next;
  7. };
  8.  
  9.  
  10. struct Entry *root = NULL;
  11.  
  12.  
  13. void printListe()
  14. {
  15.     struct Entry *rootK;
  16.     rootK = root;
  17.  
  18.     if(rootK == NULL)
  19.         printf("Kein Eintrag\n");
  20.     else
  21.     {
  22.         for(rootK;rootK != NULL;rootK=rootK->next)
  23.             printf("Adresse: %p Daten: %d\n", rootK, rootK->data);
  24.     }
  25.     printf("\n");
  26. }
  27.  
  28.  
  29. void ElementSuche(int Suche)
  30. {
  31.     int Gefunden = 0;
  32.     struct Entry *rootK;
  33.     rootK = root;
  34.  
  35.     if(rootK == NULL)
  36.     {
  37.         printf("Kein Eintrag\n\n");
  38.         return;
  39.     }
  40.  
  41.     else
  42.     {
  43.         for(rootK;rootK != NULL;rootK=rootK->next)
  44.         {
  45.             if((rootK->data) == Suche)
  46.             {
  47.                 printf("Adresse: %p Daten: %d\n", rootK, rootK->data);
  48.                 Gefunden = 1;
  49.             }
  50.         }
  51.     }
  52.  
  53.     printf("\n");
  54.  
  55.     if(Gefunden == 0)
  56.         printf("Kein Eintrag unter dieser Suche\n\n");
  57. }
  58.  
  59.  
  60. void ElementEinfuegen(int Daten)
  61. {
  62.     struct Entry *Neu = (struct Entry *)malloc(sizeof(struct Entry)); //malloc liefert einen Void Pointer -> Typecast
  63.  
  64.     Neu->data = Daten;
  65.     Neu->next = root;
  66.     root = Neu;
  67.     printf("\n");
  68. }
  69.  
  70.  
  71. void ElementLoeschen(int SucheL)
  72. {
  73.     int Gefunden = 0;
  74.     struct Entry *rootK;
  75.     struct Entry *Buffer = NULL;
  76.  
  77.     rootK = root;
  78.  
  79.  
  80.     if(rootK == NULL)
  81.     {
  82.         printf("Kein Eintrag\n\n");
  83.         return;
  84.     }
  85.     else
  86.     {
  87.         for(rootK;rootK != NULL; Buffer=rootK, rootK=rootK->next)
  88.         {
  89.             if((rootK->data) == SucheL)
  90.             {
  91.                 if(root==rootK)
  92.                 {
  93.                     root = rootK->next;
  94.                 }
  95.                     else if(Buffer != NULL)
  96.                     {
  97.                         Buffer->next=rootK->next;
  98.                         rootK->next=NULL;
  99.                         rootK=Buffer->next;
  100.                     }
  101.                         else if((rootK->next) == NULL)
  102.                         {
  103.                             Buffer->next=NULL;
  104.                         }
  105.                         else
  106.                         {
  107.                             root=rootK->next;
  108.                         }
  109.                 Gefunden = 1;
  110.             }
  111.         }
  112.     }
  113.  
  114.     printf("\n");
  115.  
  116.     if(Gefunden == 0)
  117.         printf("Kein Eintrag unter dieser Suche\n\n");
  118. }
  119.  
  120.  
  121. int main()
  122. {
  123.     int i, Daten, Suche, SucheL;
  124.  
  125. /*
  126.     struct Entry DatenA;        //Neue Struktur wird erstellt
  127.     DatenA.data = 20;
  128.  
  129.  
  130.     root = &DatenA;             //In den Pointer root wird die Adresse von DatenA geschrieben
  131.  
  132.     struct Entry DatenB;        //Neue Struktur wird erstellt
  133.     DatenB.data = 10;
  134.     DatenB.next = NULL;
  135.  
  136.     DatenA.next = &DatenB;      //In den Pointer root wird die Adresse von DatenB geschrieben
  137. */
  138.  
  139.     do
  140.     {
  141.         printf("-------------------------\n");
  142.         printf("Ausgeben  ............  1\n");
  143.         printf("Einfuegen ............  2\n");
  144.         printf("Suchen    ............  3\n");
  145.         printf("Loeschen  ............  4\n");
  146.         printf("Beenden   ............  0\n");
  147.         printf("-------------------------\n\n");
  148.  
  149.         scanf("%d%*c", &i);
  150.  
  151.         if(i < 0 || i > 4)
  152.         {
  153.             printf("Nicht in der Auswahl !\n\n");
  154.         }
  155.         else
  156.         {
  157.             switch(i)
  158.             {
  159.                 case(1):
  160.                 {
  161.                     printListe();
  162.                     break;
  163.                 }
  164.                 case(2):
  165.                 {
  166.                     printf("Daten (int):");
  167.                     scanf("%d%*c", &Daten);
  168.  
  169.                     ElementEinfuegen(Daten);
  170.                     break;
  171.                 }
  172.                 case(3):
  173.                 {
  174.                     printf("Suche nach (int):");
  175.                     scanf("%d%*c", &Suche);
  176.  
  177.                     ElementSuche(Suche);
  178.                 }
  179.                 case(4):
  180.                 {
  181.                     printf("Suchen und Loeschen (int):");
  182.                     scanf("%d%*c", &SucheL);
  183.  
  184.                     ElementLoeschen(SucheL);
  185.                 }
  186.             }
  187.         }
  188.     }while(i != 0);
  189.  
  190.     return 0;
  191. }
Add Comment
Please, Sign In to add comment