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-Exit4
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-Exit4
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-Exit7
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-Exit1
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;
}
}