Advertisement
Guest User

Untitled

a guest
May 25th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. const int STRING_MAX = 100;
  6.  
  7. struct list_node_s {
  8.    char*  data;
  9.    struct list_node_s* prev_p;
  10.    struct list_node_s* next_p;
  11. };
  12.  
  13. struct list_s {
  14.    struct list_node_s* h_p;
  15.    struct list_node_s* t_p;
  16. };
  17.  
  18. void Insert(struct list_s* list_p, char string[]);
  19. void Print(struct list_s* list_p);
  20. void Remove(struct list_s* list_p, char string[]);
  21. void Empty_node(struct list_node_s* node_p);
  22. struct list_node_s* A_node(int size);
  23.  
  24. struct list_node_s* A_node(int size) {
  25.    struct list_node_s* temp_p;
  26.  
  27.    temp_p = malloc(sizeof(struct list_node_s));
  28.    temp_p->data = malloc(size*sizeof(char));
  29.    temp_p->prev_p = NULL;
  30.    temp_p->next_p = NULL;
  31.    return temp_p;
  32. }  
  33.  
  34. void Insert(struct list_s* list_p, char string[]) {
  35.    struct list_node_s* curr_p = list_p->h_p;
  36.    struct list_node_s* temp_p;
  37.  
  38.    while (curr_p != NULL)
  39.       if (strcmp(string, curr_p->data) == 0) {
  40.          printf(" Dieser Name befindet sich bereits in der Liste:%s", string);
  41.          return;  
  42.       } else if (strcmp(string, curr_p->data) < 0) {
  43.          break;  
  44.       } else {
  45.          curr_p = curr_p->next_p;
  46.       }
  47.  
  48.    temp_p = A_node(strlen(string) + 1);
  49.    strcpy(temp_p->data, string);
  50.  
  51.    if ( list_p->h_p == NULL ) {
  52.       /* Liste ist leer */
  53.       list_p->h_p = list_p->t_p = temp_p;
  54.    } else if ( curr_p == NULL) {
  55.       /* am Ende der Liste einfügen */
  56.       temp_p->prev_p = list_p->t_p;
  57.       list_p->t_p->next_p = temp_p;
  58.       list_p->t_p = temp_p;
  59.    } else if (curr_p == list_p->h_p) {
  60.       /* am Anfang der Liste einfügen */
  61.       temp_p->next_p = list_p->h_p;
  62.       list_p->h_p->prev_p = temp_p;
  63.       list_p->h_p = temp_p;
  64.    } else {
  65.       /* middle of list, string < curr_p->data */
  66.       temp_p->next_p = curr_p;
  67.       temp_p->prev_p = curr_p->prev_p;
  68.       curr_p->prev_p = temp_p;
  69.       temp_p->prev_p->next_p = temp_p;
  70.    }
  71. }
  72.  
  73. void Print(struct list_s* list_p) {
  74.    struct list_node_s* curr_p = list_p->h_p;
  75.  
  76.    printf(" Namen:\n");
  77.  
  78.    while (curr_p != NULL) {
  79.       printf("%s", curr_p->data);
  80.       curr_p = curr_p->next_p;
  81.    }
  82.    printf("\n");
  83. }  
  84.  
  85. void Empty_node(struct list_node_s* node_p) {
  86.    free(node_p->data);
  87.    free(node_p);
  88. }
  89.  
  90. void Delete(struct list_s* list_p, char string[]) {
  91.    struct list_node_s* curr_p = list_p->h_p;
  92.  
  93.    /* gesuchten String finden */
  94.    while (curr_p != NULL)
  95.       if (strcmp(string, curr_p->data) == 0) {
  96.          break;
  97.       } else if (strcmp(string, curr_p->data) < 0) {
  98.         /* printf("%s befindet sich nicht in der Liste\n", string)*/;
  99.          return;
  100.       } else {
  101.          curr_p = curr_p->next_p;
  102.       }
  103.    
  104.    if (curr_p == NULL) {
  105.    /*printf("%s  befindet sich nicht in der Liste\n", string)*/;
  106.    } else { /* curr_p != NULL */
  107.       if (curr_p->prev_p == NULL && curr_p->next_p == NULL) {
  108.          list_p->h_p = list_p->t_p = NULL;
  109.       } else if (curr_p->prev_p == NULL) {
  110.          /* Erste node in liste */
  111.          list_p->h_p = curr_p->next_p;
  112.          list_p->h_p->prev_p = NULL;
  113.       } else if (curr_p->next_p == NULL) {
  114.          /* letzter node in der Liste */
  115.          list_p->t_p = curr_p->prev_p;
  116.          list_p->t_p->next_p = NULL;
  117.       } else {
  118.          /* Node in middle of list */
  119.          curr_p->prev_p->next_p = curr_p->next_p;
  120.          curr_p->next_p->prev_p = curr_p->prev_p;
  121.       }
  122.       Empty_node(curr_p);
  123.    }
  124. }  
  125.  
  126. int main()
  127.         {
  128.         char * pch;
  129.         struct list_s list ;  
  130.         list.h_p = list.t_p = NULL;
  131.         FILE *ptr_file;
  132.                 char laenge[100];
  133.                 char * substring;
  134.                 char plus ='+';
  135.                 char minus ='-';
  136.                 int counter = 1;
  137.                
  138.                 ptr_file = fopen("D:/Users/Nima/Desktop/test123.txt","r");
  139.                 if (!ptr_file)
  140.                     return 1;
  141.  
  142.                 while (fgets(laenge,1000, ptr_file)!=NULL)
  143.                 {
  144.                                 if (strlen(laenge)<4)
  145.                                 {
  146.                                     printf("\nName fehlt in Zeile: %d",+counter, "\n");
  147.                                     return -1;
  148.                                 }
  149.                                 else if (strchr(laenge, plus))
  150.                                 {
  151.                                     counter++;
  152.                                     pch = strstr (laenge,"+");
  153.                                     strncpy (pch," ",1);   
  154.                                     Insert(&list,laenge);
  155.                                 }                  
  156.                                 else if (strchr(laenge, minus))
  157.                                 {
  158.                                      counter++;
  159.                                      pch = strstr (laenge,"-");
  160.                                      strncpy (pch," ",1);
  161.                                      Delete(&list,laenge); 
  162.                                 }else{
  163.                                     printf("\nZeichen fuer Add oder Remove fehlt in Zeile: %d",+counter, "\n");
  164.                                     return -1;
  165.                                 }
  166.                 }
  167.                 Print(&list);
  168.                 fclose(ptr_file);
  169.                 return 0;
  170.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement