Phil 7 rokov pred
rodič
commit
94f1681e19
1 zmenil súbory, kde vykonal 51 pridanie a 24 odobranie
  1. 51 24
      linlist.c

+ 51 - 24
linlist.c

@@ -2,6 +2,9 @@
 #include <stdlib.h>
 #include <string.h>
 
+#define bool int
+#define false 0
+#define true 1
 
 typedef struct linlist{
     char *payload;
@@ -18,7 +21,7 @@ linlist_p linlist_insert_first(linlist_p *anchor, linlist_p newcell);
 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);
 
 int main() {
     linlist_p i;
@@ -26,36 +29,52 @@ int main() {
     char buffer[255];
     char reverse_buffer[255][255];
     FILE *in = stdin;
-    printf("REVERSE INPUT\n");
 
     while (fgets(buffer, 255, in))
     {
-	if(linlistFind(buffer,anchor_adr) != NULL)
-	{
-		printf("adding to list");
-        	linlist_p newcell = linlist_alloc_cell(buffer);
-        	linlist_insert_first(anchor_adr,newcell);
-	}
+        linlist_p newcell = linlist_alloc_cell(buffer);
+       	linlist_insert_first(anchor_adr,newcell);
     }
-    printf("\nNORMAL OUTPUT\n");
-    int j = 0;
+	
+	linlist_sort(anchor);
+
+    printf("\nSORTED OUTPUT\n");
     for (i = anchor; i != NULL; i = i->next)
     {
         fprintf(stdout, "%s", i->payload);
-        strcpy(reverse_buffer[j], i->payload);
-        j++;
-    }
-    printf("REVERSE OUTPUT\n");
-    j--;
-    for (j; j >= 0; j--)
-    {
-        fprintf(stdout, "%s", reverse_buffer[j]);
     }
-
     linlist_free(&anchor);
 }
 
 
+void linlist_sort(linlist_p *anchor)
+{
+	bool swapped = true;
+	linlist_p cell = anchor;
+	linlist_p dump;
+	while(swapped)
+	{
+		swapped = false;
+		cell = anchor;
+		while(cell->next)
+		{
+			if(strcmp(cell->payload,cell->next->payload) > 0)
+			{
+			//	printf("swapping %s and %s",cell->payload,cell->next->payload);
+				dump = cell->next->payload;
+				cell->next->payload = cell->payload;
+				cell->payload = dump;
+				swapped = true;
+			}else
+			{
+				
+			}	
+			cell = cell->next;
+		}
+	}
+}
+
+
 linlist_p linlistExtract(linlist_p *anchor, linlist_p cell)
 {
 	if(*anchor == cell)
@@ -72,17 +91,24 @@ linlist_p linlistFind(char* payload, linlist_p anchor)
 {
 	printf("find\n");
 	linlist_p cell = anchor;
-	while(cell->next || *anchor)
+
+	if(cell == NULL)
 	{
-	printf("in while");
+		printf("anchor is null");
+		return NULL;
+	}
+	
+	while(cell)
+	{
+		printf("to search %s \n",payload);
 		if(strcmp(payload,cell->payload))
 		{	
-		printf("found same");
+		printf("found same\n");
 			return cell;
 		}
 		else
 		{
-		printf("found nothing");
+			printf("found nothing\n");
 			cell = cell->next;
 		}
 	}
@@ -118,13 +144,14 @@ linlist_p linlist_alloc_cell(char* payload)
     return (newcell);
 
 }
+
 linlist_p linlist_insert_first(linlist_p *anchor, linlist_p newcell)
 {
     newcell->next = *anchor;
     *anchor = newcell;
     return(*anchor);
-
 }
+
 linlist_p linlist_extract_first(linlist_p *anchor)
 {
     if (*anchor == NULL)