如何使用数组将冒泡排序转换为双向链表?

时间:2022-11-15 19:55:02

How can I change the below arrays into a doubly-linked list with struct and pointers and still have this program work? I assume that is possible correct? The program I need to create needs to store the name and age into nodes on the doubly linked list.

如何将下面的数组更改为带有结构和指针的双向链表,并且仍然可以使用此程序?我认为这可能是正确的吗?我需要创建的程序需要将名称和年龄存储到双向链表上的节点中。

#include <stdio.h>
#include <string.h>
#include <stdio.h>
#define SIZE 10


void input (char fullname[][25], int age[]);
void output (char fullname[][25], int age[]);
void bubblesortname (char fullname[][25], int *age, int size);
void bubblesortage (char fullname[][25], int *age, int size);
void fflush_stdin();

//Input function handles prompt and user input.
void input (char fullname[][25], int age[])
{
    int i = 0;
    size_t nchr = 0;
    for (i = 0; i < SIZE; i++) {
        printf ("\nEnter a full name: ");
        if (fgets (fullname[i], 24, stdin) != NULL)
        {
            nchr = strlen (fullname[i]);
            while (nchr > 0 && (fullname[i][nchr -1] == '\n' || fullname[i][nchr -1] == '\r'))
                fullname[i][--nchr] = 0;
        }
        printf ("Enter the age    : ");
        scanf ("%d", &age[i]);
        fflush_stdin();
    }
}

//output function prints out name and age array
void output (char fullname[][25], int age[])
{
    int i;
    for (i = 0; i < SIZE; i++)
        printf (" %-30s, %d\n", fullname[i], age[i]);
}            



int main (void)
{
    //array for user entered names
    char fullname[SIZE][25];
    //array for user enter ages
    int age[SIZE];

    // promt user for names and ages
    input (fullname, age);

    //output unsorted names and ages

    output (fullname, age);

    // sorts by name
    bubblesortname (fullname, age, SIZE);

   // output sorted names
    output (fullname, age);

    //sorts age
    bubblesortage (fullname, age, SIZE);

    //output sorted ages with corresponding names
    output (fullname, age);


    return 0;
}



// sorts the fullname array with bubblesort
void bubblesortname (char fullname[][25], int *age, int size)
{
    int j = 0, i = 0;
    int temp_age = 0;
    char temp_name[25] = {0};

    for (i = 0; i < size - 1; ++i) {

        for (j = 0; j < size - 1 - i; ++j) {
            if (strcmp (fullname[j], fullname[j + 1]) > 0) {
                temp_age = age[j + 1];
                age[j + 1] = age[j];
                age[j] = temp_age;

                strcpy (temp_name, fullname[j + 1]);
                strcpy (fullname[j + 1], fullname[j]);
                strcpy (fullname[j], temp_name);
            }           
        }           
    }               
}               

//sorts age array
void bubblesortage (char fullname[][25], int *age, int size)
{
    int j = 0, i = 0;
    int temp_age = 0;
    char temp_name[25] = {0};

    for (i = 0; i < size - 1; ++i) {

        for (j = 0; j < size - 1 - i; ++j) {
            if (age[j] > age[j + 1]) {
                temp_age = age[j + 1];
                age[j + 1] = age[j];
                age[j] = temp_age;

                strcpy (temp_name, fullname[j + 1]);
                strcpy (fullname[j + 1], fullname[j]);
                strcpy (fullname[j], temp_name);
            }           
        }           
    }               
}

void fflush_stdin()
{ int c; while ((c = getchar()) != '\n' && c != EOF); }

1 个解决方案

#1


2  

It's not clear what your C level expertise is. Some of the below is fairly basic and not all issues are covered. Please ask further if required.

目前尚不清楚你的C级专业知识是什么。下面的一些是相当基本的,并不涵盖所有问题。如有需要,请进一步询问。

Most data structures would make use of pointer and the C struct construct. For example, a doubly linked list node in your case would be something like this:

大多数数据结构都会使用指针和C结构构造。例如,您的案例中的双向链接列表节点将是这样的:

struct person_node {
    struct person_node *next;
    struct person_node *prev;
    char name[25];
    int age;
};

/* This holds the start of the list */
struct person_node *list_head = NULL;

Then you would have functions to add to the list, remove from the list, search the list etc. Some of these functions (add and remove at least) will deal with dynamically allocating and freeing nodes as part of their operation.

然后你将有功能添加到列表,从列表中删除,搜索列表等。其中一些功能(至少添加和删除)将处理动态分配和释放节点作为其操作的一部分。

#1


2  

It's not clear what your C level expertise is. Some of the below is fairly basic and not all issues are covered. Please ask further if required.

目前尚不清楚你的C级专业知识是什么。下面的一些是相当基本的,并不涵盖所有问题。如有需要,请进一步询问。

Most data structures would make use of pointer and the C struct construct. For example, a doubly linked list node in your case would be something like this:

大多数数据结构都会使用指针和C结构构造。例如,您的案例中的双向链接列表节点将是这样的:

struct person_node {
    struct person_node *next;
    struct person_node *prev;
    char name[25];
    int age;
};

/* This holds the start of the list */
struct person_node *list_head = NULL;

Then you would have functions to add to the list, remove from the list, search the list etc. Some of these functions (add and remove at least) will deal with dynamically allocating and freeing nodes as part of their operation.

然后你将有功能添加到列表,从列表中删除,搜索列表等。其中一些功能(至少添加和删除)将处理动态分配和释放节点作为其操作的一部分。