user311607
user311607

Reputation: 69

How to convert bubble sort using array to doubly linked list?

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); }

Upvotes: 1

Views: 180

Answers (1)

kaylum
kaylum

Reputation: 14044

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.

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:

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.

Upvotes: 2

Related Questions