0

I am trying to sort the data from my linked list in my last function but for some reason, instead of sorting by the chosen criteria, it simply places the latest entered post data at the top and sorts the data but assigns it to the wrong post. I have no idea why and I also have very little idea what I am doing. I know the code isn't pretty, I haven't had the chance to clean it up yet. All the other functions are working normally. I have included a sample of the run. I am very new to programming at this level. Any ideas?

Welcome! Please make a selection to continue 1-Display the stored posts 2-Display the first post with a given attribute value 3-Display the current total number of stored posts 4-Store the data of a new post 5-Delete a post either by name or by author 6-Delete all the stored posts 7-Sort the post based on one of the following post attributes: number of likes,
number of comments, date, or post size 8-Exit

4

Add a Post Author: Bob Date of Post(YYYYMMDD): 20121205 Number of Likes: 56 Number of Comments: 23 Enter size of post: 222

Welcome! Please make a selection to continue 1-Display the stored posts 2-Display the first post with a given attribute value 3-Display the current total number of stored posts 4-Store the data of a new post 5-Delete a post either by name or by author 6-Delete all the stored posts 7-Sort the post based on one of the following post attributes: number of likes,
number of comments, date, or post size 8-Exit

4

Add a Post Author: Karen Date of Post(YYYYMMDD): 20170513 Number of Likes: 57 Number of Comments: 60 Enter size of post: 2222

Welcome! Please make a selection to continue 1-Display the stored posts 2-Display the first post with a given attribute value 3-Display the current total number of stored posts 4-Store the data of a new post 5-Delete a post either by name or by author 6-Delete all the stored posts 7-Sort the post based on one of the following post attributes: number of likes,
number of comments, date, or post size 8-Exit

7

Sort posts by: 1. Likes 2. Size 3. Date 4. Number of Comments 1

Welcome! Please make a selection to continue 1-Display the stored posts 2-Display the first post with a given attribute value 3-Display the current total number of stored posts 4-Store the data of a new post 5-Delete a post either by name or by author 6-Delete all the stored posts 7-Sort the post based on one of the following post attributes: number of likes,
number of comments, date, or post size 8-Exit

1

Author: Karen Date: 20170513 Likes: 56 Number of Comments: 60 Size: 2222

Author: Bob Date: 20121205 Likes: 57 Number of Comments: 23 Size: 222

    struct post {
    char author[50];
    int date;
    int likes;
    int comments;
    int length;
    struct post *next;
}; 

typedef struct post Post;

int menu();                 
void printList(Post *List);
void searchList(Post *List);
int numPosts(Post *List);
Post *addToList(Post *List);
void deleteAPost(Post **List);
void deleteAllPosts(Post **List);
void sort(Post **List);

int main(void) {

    int menuSelection = 0;
    Post *LIST = NULL;

    menuSelection = menu();

    while(menuSelection >= 1) {
        switch(menuSelection) {
            case 1 : printList(LIST); 
                     break;
            case 2 : searchList(LIST); 
                     break;
            case 3 : printf("Number of stored posts: %d", numPosts(LIST)); 
                     break;
            case 4 : LIST = addToList(LIST); 
                     break;
            case 5 : deleteAPost(&LIST);
                     break;
            case 6 : deleteAllPosts(&LIST); 
                     break;
            case 7 : sort(&LIST); 
                     break;
            case 8 : printf("Program terminated. Have a lovely day!\n"); exit(0);
            default: printf("Invalid!");
        }
         menuSelection = menu();
    }

    if(LIST) free(LIST);
    return 0;
}


int menu(void) {

    int menuSelection = 0;

        printf("\n***Welcome! Please make a selection to continue***\n");
        printf("1-Display the stored posts\n");
        printf("2-Display the first post with a given attribute value\n");
        printf("3-Display the current total number of stored posts\n");
        printf("4-Store the data of a new post\n");
        printf("5-Delete a post either by name or by author\n");
        printf("6-Delete all the stored posts\n");
        printf("7-Sort the post based on one of the following post attributes: number of likes, number of comments, date, or post size\n");
        printf("8-Exit\n");
        printf("\n");
        scanf("%d", &menuSelection);
        printf("\n");

    return menuSelection;
}
    void sort(Post **List){
        int choice = 0;
        printf("Sort posts by:\n1. Likes\n2. Size\n3. Date\n 4. Number of Comments\n");
        scanf(" %d",&choice);

        if (choice == 1){
            if((*List)== NULL || (*List)->next == NULL) {
               return;
            }

            Post *t1 = (*List)->next;

            while(t1 != NULL) { 
                int like = t1->likes;
                int found = 0;
                Post *t2 = *List;

                while(t2 != t1) {
                    if(t2->likes > t1->likes && found == 0) {
                        like = t2->likes;
                        t2->likes = t1->likes;
                        found = 1;
                        t2 = t2->next;
                    } else {
                        if(found == 1) {
                            int temp = like;
                            like = t2->likes;
                            t2->likes = temp;
                        }
                        t2 = t2->next;
                    }
               }
               t2->likes = like;
               t1 = t1->next;
            }
        }
6
  • Aren't there solutions already? So many of them on SO. Btw, you are offtopic. Commented Sep 26, 2017 at 20:42
  • @tilz0R none that solve my problem. The sorting works but is being assigned to the wrong post. Commented Sep 26, 2017 at 20:46
  • 2
    Please read this and edit your question accordingly: stackoverflow.com/help/mcve Commented Sep 26, 2017 at 20:46
  • @Haon210012 There are at least 100 linked list questions on sorting and other problems. Commented Sep 26, 2017 at 20:47
  • @tilz0R I have spent the last 2 days pouring over them and other resources and none of those solutions have helped so I thought I would ask my own. Commented Sep 26, 2017 at 20:49

1 Answer 1

1

Your sort algorithm modifies the nodes: it swaps the likes members but not the rest of the structure. The nodes are corrupted, as you can see in the output.

You should either also swap the othe members (except the next pointer) or use an algorithm that does not modify the objects but only changes their order (ie only changes the next pointers).

You should post the structure definition and the main function that reads, sorts and writes the data so readers on stack overflow can get a full picture of your problem and find bugs and solutions.

Sign up to request clarification or add additional context in comments.

5 Comments

"Help! I sorted all the likes, but now the rest of the data is out of order?"
@chqrlie thank you for your constructive suggestions. I have posted my main function and structure so you can get a better picture of what is going on. How should I modify the function to include all the data or would I be able to copy it without modifying?
@Haon210012 - you could declare a local node in the sort function and use it at a temporary node in order to swap the node data, or as also suggested, use a sort that only modifies the next pointers and the list pointer that points to the first node of the list.
@rcgldr how would I implement something like that?
@Haon210012 - do you mean swap the data or change to a sort that sorts the next pointers without moving the data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.