Ver código fonte

added working extract function and started count method

Phil 7 anos atrás
pai
commit
4587965ae6
1 arquivos alterados com 66 adições e 13 exclusões
  1. 66 13
      mystat.c

+ 66 - 13
mystat.c

@@ -11,6 +11,14 @@ typedef struct linlist{
     struct linlist *next;
 }linlist_cell, *linlist_p ;
 
+typedef struct countlist{
+	char *payload;
+	int occurence;
+    	struct countlist *next;
+}countlist_cell, *countlist_p ;
+
+countlist_p count_anchor;
+
 linlist_p anchor;
 linlist_p *anchor_adr = &anchor;
 
@@ -22,12 +30,13 @@ linlist_p linlist_extract_first(linlist_p *anchor);
 linlist_p linlistFind(char* payload, linlist_p anchor);
 linlist_p linlistExtract(linlist_p *anchor, linlist_p cell);
 void linlist_sort(linlist_p *anchor);
+countlist_p countlist_alloc_cell(char* payload);
+countlist_p linlist_count(linlist_p *anchor);
 
 int main() {
     linlist_p i;
 
     char buffer[255];
-    char reverse_buffer[255][255];
     FILE *in = stdin;
 
     while (fgets(buffer, 255, in))
@@ -36,7 +45,7 @@ int main() {
        	linlist_insert_first(anchor_adr,newcell);
     }
 	
-	linlist_sort(anchor);
+	linlist_count(anchor);
 
     printf("\nSORTED OUTPUT\n");
     for (i = anchor; i != NULL; i = i->next)
@@ -45,7 +54,17 @@ int main() {
     }
     linlist_free(&anchor);
 }
-
+countlist_p linlist_count(linlist_p *anchor)
+{
+	linlist_p cell = *anchor;
+	linlist_p del;
+	char* payload;
+	while(cell)
+		{
+			payload = cell->payload;
+			del = linlist_find(payload, anchor);
+		}
+}
 
 void linlist_sort(linlist_p *anchor)
 {
@@ -142,9 +161,18 @@ linlist_p linlist_alloc_cell(char* payload)
     newcell->payload = strdup(payload);
     newcell->next = NULL;
     return (newcell);
+}
 
+countlist_p countlist_alloc_cell(char* payload)
+{
+    countlist_p newcell = malloc(sizeof(countlist_p));
+    char *value;
+    newcell->payload = strdup(payload);
+    newcell->next = NULL;
+    return (newcell);
 }
 
+
 linlist_p linlist_insert_first(linlist_p *anchor, linlist_p newcell)
 {
     newcell->next = *anchor;
@@ -152,18 +180,43 @@ linlist_p linlist_insert_first(linlist_p *anchor, linlist_p newcell)
     return(*anchor);
 }
 
-linlist_p linlist_extract_first(linlist_p *anchor)
+countlist_p countlist_insert_first(countlist_p *anchor, countlist_p newcell)
 {
-    if (*anchor == NULL)
-    {
+    newcell->next = *anchor;
+    *anchor = newcell;
+    return(*anchor);
+}
+
+LinList_p LinListExtract(LinList_p* anchor,LinList_p cell){
+    if(*anchor == NULL){
+        printf("liste ist leer");
         return NULL;
     }
-    else
-    {
-        linlist_p *return_var;
-        return_var = anchor;
-        *anchor = (*anchor)->next;
-        return (*return_var);
+    if((*anchor)->next == NULL){ //wenn liste nur ein element hat
+        if(*anchor == cell){
+            return NULL;
+        }
+        else{
+            printf("element nicht in der liste enthalten");
+            return *anchor;
+        }
+    }
+    if(*anchor == cell){
+        LinList_p ret = LinListExtractFirst(anchor);
+        return ret;
+    }
+    
+    while(*anchor != NULL && (*anchor)->next != cell){ //springe bis letztes element
+        anchor = &(*anchor)->next;
+    }  
+    LinList_p tmp = *anchor;
+    if((*anchor)->next == cell){ //zelle gefunden
+
+        tmp->next = cell->next;
+        cell->next = NULL;
+        return cell;
+    }
+    else{ //keine zelle gefunden
+        return NULL;
     }
-
 }