按字母顺序排列的冒泡在STRUCT编程中排序

问题描述 投票:-2回答:1

我一直在努力尝试让它发挥作用。我已设法切换排序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");
    }
}

任何和所有的帮助将不胜感激!!!!

c string struct bubble-sort alphabetical-sort
1个回答
1
投票

您可以执行以下操作将节点添加到已排序列表:

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");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.