STRUCT c 编程中的字母冒泡排序

Alphabetical Bubble Sort in a STRUCT c programming

我一直在努力使它起作用。我设法切换了排序 1 名称,但是它不会更改任何其他名称顺序。我知道您必须进行字符串比较才能执行此操作,但我无法弄清楚。请提供代码片段来解决此问题。我在这里包括了整个程序。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Define a doubly linked list type
typedef struct node {
    char name[100];
    int age;
    float weight;
    struct node *next;
} node;

void print_list(node* list, int count) {
    int j = 0;
    node temp;
    // walk the list to print out the contents
    for (int i = 0; i < count; i++) {
        j = i - 1;
        while (j >= 0 && strcmp(list[j + 1].name, list[j].name) < 0) {
            temp = list[j + 1];
            list[j + 1] = list[j];
            list[j] = temp;
            j--;
        }
    }
    while (list) {
        printf("%s%d\n%f\n", list->name, list->age, list->weight);
        list = list->next;
    }
    printf("\n");
}

node* new_node(char *value, int a, float w) {
    node* t = (node *)malloc(sizeof(node));
    strcpy(t->name, value);
    t->age = a;
    t->weight = w;
    t->next = NULL;
    return t;
}

node* add(node* list) {
    node* t = list;

    char name[100];
    int a;
    float w;
    printf("Enter name: ");
    fgets(name, 100, stdin);
    printf("Enter age: ");
    scanf("%d", &a);
    printf("Enter weight: ");
    scanf("%f", &w);
    while (getchar() != '\n');

    node* s = new_node(name, a, w);

    // special case: starting with an empty list
    if (t == NULL)
        return s;

    s->next = list;
    return s;
}

int getChoice() {
    int ch;
    printf("1. Add a Record\n2. Display All Records\n");
    printf("3.Quit\nEnter choice: ");
    scanf("%d", &ch);
    while (getchar() != '\n');
    return ch;
}

int main() {
    node* my_list = NULL;

    int ch;
    int count = 0;
    while ((ch = getChoice()) != 3) {
        if (ch == 1) {
            my_list = add(my_list);
            count++;
        }
        else if (ch == 2) {
            print_list(my_list, count);
        }
        printf("\n");
    }
}

任何帮助将不胜感激!!!!

您可以像这样将节点添加到排序列表中:

node * add_node_to_sort_list(node * list, node * newNode)
{
    /* should the node be inserted as the head? */
    if( list == NULL || strncmp(list->name, newNode->name, 100) < 0 )
    {
        newNode->next = list;
        return newNode;
    }

    /* search for the location the node should be at */
    while(list->next != NULL && strncmp(list->next->name, newNode->name) > 0 )
    {
        /* move to the next node */
        list = list->next;
    }

    /* we have found the spot to insert the node */
    newNode->next = list->next;
    list->next = newNode;

    return list;
}

使用它来更新您的代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Define a doubly linked list type
typedef struct node {
    char name[100];
    int age;
    float weight;
    struct node *next;
} node;

void print_list(node* list) {

    // walk the list to print out the contents
    while(list != NULL)
    {
        printf("%s%d\n%f\n", list->name, list->age, list->weight);
        list = list->next;
    }
}

node* new_node(char *value, int a, float w) {
    node* t = (node *)malloc(sizeof(node));
    strcpy(t->name, value);
    t->age = a;
    t->weight = w;
    t->next = NULL;
    return t;
}

node * add_node_to_sort_list(node * list, node * newNode)
{
    /* should the node be inserted as the head? */
    if( list == NULL || strncmp(list->name, newNode->name, 100) > 0 )
    {
        newNode->next = list;
        return newNode;
    }

    /* search for the location the node should be at */
    while(list->next != NULL && strncmp(list->next->name, newNode->name, 100) < 0 )
    {
        /* move to the next node */
        list = list->next;
    }

    /* we have found the spot to insert the node */
    newNode->next = list->next;
    list->next = newNode;

    return list;
}

node* add(node* list) {

    char name[100];
    int a;
    float w;
    printf("Enter name: ");
    fgets(name, 100, stdin);
    printf("Enter age: ");
    scanf("%d", &a);
    printf("Enter weight: ");
    scanf("%f", &w);
    while (getchar() != '\n');

    node* s = new_node(name, a, w);

    return add_node_to_sort_list(list, s);
}

int getChoice() {
    int ch;
    printf("1. Add a Record\n2. Display All Records\n");
    printf("3.Quit\nEnter choice: ");
    scanf("%d", &ch);
    while (getchar() != '\n');
    return ch;
}

int main() {
    node* my_list = NULL;

    int ch;
    while ((ch = getChoice()) != 3) {
        if (ch == 1) {
            my_list = add(my_list);
        }
        else if (ch == 2) {
            print_list(my_list);
        }
        printf("\n");
    }
}