Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "list.h"
- Node * sort( Node * Head)
- {
- //Write your code here
- //bubble sort is simplest
- int sorted = 0;
- Node ** source = &Head;
- if (Head == NULL) {
- printf("Error, Head pointer points to NULL\n");
- }
- else
- {
- while(!sorted)
- {
- Node * current = Head;
- Node * next = current->next;
- Node * prev = NULL;
- sorted = 1;
- while (next != NULL)
- {
- if (current-> data > next->data)
- {
- sorted = 0;
- if (current == Head)
- {
- Head = next;
- }
- else
- {
- prev->next = next;
- }
- current -> next = next->next; //swapping
- next->next = current;//swapping
- prev = next;//iterating
- next = current ->next; //iterating
- }
- else
- {
- prev = current;
- current = current->next;
- next = current -> next;
- }
- }
- }
- }
- return Head;
- }
- int main(int argc, char* argv[])
- {
- FILE *ifp;
- char *mode = "r";
- Node *head;
- int num,i;
- char inputFilename[50];
- int *input;
- strcpy (inputFilename, argv[1]);
- ifp = fopen(inputFilename, mode);
- if (ifp == NULL)
- {
- fprintf(stderr, "Can't open the input file \n");
- exit(1);
- }
- while (fscanf(ifp, "%d", &num) == 1)
- {
- input = (int *) malloc(num*sizeof(int));
- for(i=0; i<num; i++)
- {
- fscanf(ifp, "%d",&input[i]);
- }
- head = createFullList(input, num);
- printf("Printing linked list...............\n");
- print(head);
- printf("Printing the sorted linked list...............\n");
- print( sort(head));
- free(input);
- }
- fclose(ifp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement