Guest User

Untitled

a guest
Jun 17th, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct elem{
  5.     int Messwert;
  6.     int Anz;
  7.     struct elem *next;
  8.     struct elem *prev;
  9. };
  10.  
  11. struct elem *root;
  12. struct elem *tail;
  13.  
  14. void list_init(void){
  15.     root=tail=NULL;
  16. }
  17.  
  18. void list_bind( int m){
  19.     struct elem *ptr, *ptr1;
  20.    
  21.     if(tail==NULL){
  22.         if((tail=malloc(sizeof(struct elem))) == NULL){
  23.             fprintf(stderr, "ERROR");
  24.             return;
  25.         }
  26.     }
  27.  
  28.     if(root==NULL){
  29.         if((root=malloc(sizeof(struct elem))) == NULL){
  30.             fprintf(stderr, "ERROR");
  31.             return;
  32.         }
  33.         root->Messwert=m;
  34.         root->next=NULL;
  35.         tail=root;
  36.         tail->prev=NULL;
  37.     } else {
  38.         ptr=root;
  39.         while(ptr->next != NULL)
  40.             ptr=ptr->next;
  41.         if((ptr->next=malloc(sizeof(struct elem))) == NULL){
  42.             fprintf(stderr, "ERROR");
  43.             return;
  44.         }
  45.         ptr1=ptr;
  46.         ptr=ptr->next;
  47.         ptr->Messwert=m;
  48.         ptr->next=NULL;
  49.         tail=ptr;
  50.         ptr->prev=ptr1;
  51.         ptr1->next=ptr;
  52.     }
  53. }
  54.  
  55. void eingabe(void){
  56.     int mess;
  57.     printf("Bitte geben sie Eine Messwert: ");
  58.     scanf("%d", &mess);
  59.     list_bind(mess);
  60. }
  61. void ausgabe(void){
  62.     struct elem *ptr;
  63.     ptr=root;
  64.     while(ptr != NULL){
  65.             printf("MESSWERT %d\n", ptr->Messwert);
  66.     ptr=ptr->next;
  67.     }
  68. }
  69.  
  70. struct elem *LstSuchInc(struct elem *wrz, int ulWert){
  71.     struct elem *currentElement=wrz;
  72.     currentElement=root;
  73.     if(currentElement != NULL){
  74.     while (currentElement != NULL) {
  75.         if (currentElement->Messwert == ulWert) {
  76.             (currentElement->Anz)++;
  77.             return currentElement;
  78.             }
  79.             currentElement = currentElement->next;
  80.         }
  81.     } else return 0;
  82. }
  83. void SuchErgebniss(void){
  84.     struct elem *mwert;
  85.     int data;
  86.     printf("Bitte geben sie Ein wert: ");
  87.     scanf("%d", &data);
  88.     mwert=LstSuchInc(root, data);
  89.     if(mwert){
  90.       printf("Element %d Found\n", mwert->Messwert);
  91.       printf("Element %d %d mal aufgetreten!\n", mwert->Messwert, mwert->Anz);
  92.       } else {
  93.           printf("Element Not Found\n");
  94.           }
  95. }  
  96.  
  97. int getLast(struct elem *wrz){
  98.     int current=wrz->Messwert;
  99.     int next;
  100.     if(wrz->next==NULL){
  101.         return current;
  102.     } else {
  103.         next=getLast(wrz->next);
  104.     }
  105.     if(current>next)
  106.         return current;
  107.     else
  108.         return next;
  109. }
  110.  
  111. void getLastErg(void){
  112.     int erg;
  113.     erg=getLast(root);
  114.     printf("The last Big Element ist: %d", erg);
  115. }
  116.  
  117. void LstInsert(struct elem *neu, struct elem *last){
  118.     struct elem *ptr;
  119.     if(root==NULL){
  120.         printf("Keine Elemente vorhanden!\n");
  121.         return;
  122.     } else {
  123.     ptr=root;
  124.     while(ptr!=NULL){
  125.         ptr=ptr->next;
  126.         if(ptr==NULL){
  127.             ptr=last;
  128.             last->next=neu;
  129.             neu=malloc(sizeof(struct elem));
  130.             neu->Messwert=ptr->Messwert;
  131.             neu->next=NULL;
  132.                        
  133.             }
  134.         }
  135.     }
  136. }
  137. int CalcMittel(void){
  138.     struct elem *ptr;
  139.     int temp=0, summe=0;
  140.     ptr=root;
  141.     while(ptr!=NULL){
  142.         ptr=ptr->next;
  143.         summe=summe+ptr->Messwert;
  144.         temp++;
  145.     }
  146.     return summe/temp;
  147. }
  148.  
  149. void mittelwerterg(void){
  150.     int m;
  151.     m=CalcMittel();
  152.     printf("Das Mittelwert ist %d", m);
  153. }
  154.    
  155. int main(void) {
  156.    int wahl;
  157.    list_init();
  158.    
  159.  
  160.    
  161.    
  162.    do {
  163.       printf("\n1 : Eingabe\n");
  164.       printf("2 : Ausgabe\n");
  165.       printf("3 : List Suche\n");
  166.       printf("4 : Get Last\n");
  167.       printf("5 : Mittelwert\n");
  168.       printf("9 : Ende\n");
  169.       printf("Ihre Wahl : ");
  170.       scanf("%d",&wahl);
  171.       getchar();
  172.       switch(wahl) {
  173.          case 1 : eingabe();
  174.                   break;
  175.          case 2 : ausgabe();
  176.                   break;    
  177.          case 3 : SuchErgebniss();
  178.                   break;   
  179.          case 4 : getLastErg();
  180.                   break;
  181.          case 5 : mittelwerterg();  
  182.          case 9 : break;
  183.          default: printf("Falsche Eingabe!!!\n");
  184.       }
  185.    } while(wahl != 9);
  186.    
  187.    return EXIT_SUCCESS;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment