Advertisement
Guest User

linkedlist

a guest
Dec 13th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.36 KB | None | 0 0
  1.  #include <stdio.h>
  2.    #include <stdlib.h>
  3.  
  4.    typedef struct Knoten_ {
  5.            int data;
  6.            struct Knoten_ *next;
  7.    } Knoten;
  8.  
  9.   Knoten *kopf = NULL;
  10.   int anzahl = 0;
  11.  
  12.  
  13.   void einfuegenAmAnfang(int x) {
  14.      Knoten *t;
  15.  
  16.      t = (Knoten *)malloc(sizeof(Knoten));
  17.      anzahl++;
  18.  
  19.     if (kopf == NULL) {
  20.        kopf = t;
  21.        kopf->data = x;
  22.        kopf->next = NULL;
  23.     }
  24.     else {
  25.        t->data = x;
  26.        t->next = kopf;
  27.        kopf = t;
  28.     }
  29.  }
  30.  
  31.  void einfuegenAmEnde(int x) {
  32.     Knoten *t, *temp;
  33.  
  34.     t = (Knoten*)malloc(sizeof(Knoten));
  35.     anzahl++;
  36.  
  37.     if (kopf == NULL) {
  38.        kopf = t;
  39.        kopf->data = x;
  40.        kopf->next = NULL;
  41.     }
  42.     else {
  43.        temp = kopf;
  44.  
  45.        while (temp->next != NULL)
  46.           temp = temp->next;
  47.  
  48.        temp->next = t;
  49.        t->data    = x;
  50.        t->next    = NULL;
  51.     }
  52.  }
  53.  
  54.  void ausgeben() {
  55.     Knoten *t;
  56.  
  57.     t = kopf;
  58.  
  59.     if (t == NULL) {
  60.        printf("Die Linked List ist leer.\n");
  61.     }
  62.     else {
  63.        printf("Die Linked List hat %d Elemente.\n", anzahl);
  64.  
  65.        while (t->next != NULL) {
  66.           printf("%d ", t->data);
  67.           t = t->next;
  68.        }
  69.        printf("%d\n", t->data);
  70.     }
  71.  }
  72.  
  73.  void loeschenAmAnfang() {
  74.     Knoten *t;
  75.     int n;
  76.  
  77.     if (kopf == NULL) {
  78.        printf("Die Linked List ist schon leer.\n");
  79.     }
  80.     else {
  81.        n = kopf->data;
  82.        t = kopf->next;
  83.        free(kopf);
  84.        kopf = t;
  85.        anzahl--;
  86.        printf("%d wurde vom Anfang geloescht.\n", n);
  87.     }
  88.  }
  89.  
  90.  void loeschenAmEnde() {
  91.     Knoten *t, *u;
  92.     int n;
  93.  
  94.     if (kopf == NULL) {
  95.        printf("Die Linked List ist schon leer.\n");
  96.     }
  97.     else {
  98.        anzahl--;
  99.  
  100.        if (kopf->next == NULL) {
  101.           n = kopf->data;
  102.           free(kopf);
  103.           kopf = NULL;
  104.           printf("%d wurde vom Ende geloescht.\n", n);
  105.        }
  106.        else {
  107.          t = kopf;
  108.  
  109.          while (t->next != NULL) {
  110.             u = t;
  111.             t = t->next;
  112.          }
  113.  
  114.       n = t->data;
  115.          u->next = NULL;
  116.          free(t);
  117.          printf("%d wurde vom Ende geloescht.\n", n);
  118.        }
  119.     }
  120.  }
  121.  
  122.  int main() {
  123.     int auswahl, data;
  124.     int weiter = 1;
  125.  
  126.     while (weiter) {
  127.        printf("\nWas wollen Sie mit der Linked List tun?\n\n");
  128.        printf("1. Einfuegen am Anfang\n");
  129.        printf("2. Einfuegen am Ende\n");
  130.        printf("3. Ausgabe der Linked List\n");
  131.        printf("4. Loeschen am Anfang\n");
  132.        printf("5. Loeschen am Ende\n");
  133.        printf("6. Ende\n\n");
  134.        printf("Auswahl: ");
  135.        scanf("%d", &auswahl);
  136.        printf("\n");
  137.  
  138.        switch (auswahl) {
  139.           case 1:
  140.              printf("Wert eingeben: ");
  141.              scanf("%d", &data);
  142.              einfuegenAmAnfang(data);
  143.              break;
  144.           case 2:
  145.              printf("Wert eingeben: ");
  146.              scanf("%d", &data);
  147.              einfuegenAmEnde(data);
  148.              break;
  149.           case 3:
  150.              ausgeben();
  151.              break;
  152.           case 4:
  153.              loeschenAmAnfang();
  154.              break;
  155.           case 5:
  156.              loeschenAmEnde();
  157.              break;
  158.           case 6:
  159.              weiter = 0;
  160.              break;
  161.           default:
  162.              printf("Bitte einen sinnvollen Wert eingeben. \n");
  163.        }
  164.     }
  165.     return 0;
  166.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement