冒泡排序单链表,其中每个节点在C中都包含一个字符串?

时间:2018-07-16 07:37:14

标签: c string singly-linked-list bubble-sort

排序链表(其中EA节点包含其他字符串)的正确方法是什么?

与stackoverflow上的冒泡排序有关的所有内容都只考虑一个字符或数字。如何对字符串调整冒泡排序。

void bubble_sort( struct dictionary_entry * head )
{
    struct dictionary_entry * selector;
    int swapd;
    int i; 
    int ooseven;
    char tword[5];
    char tword2[5]; 

    while ( swapd != 0 )
    {
        swapd = 0;
        selector = head;
        while ( selector->next_entry != NULL )
        { 
            memcpy( tword, selector->next_entry->four_letters, 5 ); 
            memcpy( tword2, selector->next_entry->next_entry->four_letters, 5 ); 
        }

        for ( i = 0; i < 4; ++i )
        {
            ooseven = tolower( tword[i] ) - tolower( tword2[i] );

            if ( ooseven > 0 )
            {
                swap( selector,
                      selector->next_entry,
                      selector->next_entry->next_entry,
                      selector->next_entry->next_entry->next_entry );

                i = 5;
            }
            else if ( ooseven < 0 )
            {
                i = 5;
            }
        }
        selector = selector->next_entry;
    }
}

void swap( struct dictionary_entry * a,
           struct dictionary_entry * b,
           struct dictionary_entry * c,
           struct dictionary_entry * d )
{
    *a = *c;
    *c = *b;
    *b = *d;
}

如果您认为该问题可以通过包含带有单个字符和数字的冒泡排序的答案来回答,那么您可以解释该格式如何成为带有字符串的链表的基础。

如果您想看一下,这是我的完整代码。请注意,它确实包含很多评论。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

typedef struct dictionary_entry
{
    //int number_of_letters;
    char four_letters[5];
    struct dictionary_entry * next_entry;
} dictionary_entry;

char sentence[120][120];
char word[120];
char temp_word;
char four_letters[5] = {'\0'};
int word_number;
int number_of_words = -1;
int number_of_letters;
int letter_a;
int letter_b;
void scan();
void print();
void reverse();
void alphabetized();
void display( struct dictionary_entry * head );
void initialize();
void add( struct dictionary_entry ** head, char * four_letters );
void sentence_into_list();
void swap( struct dictionary_entry * a, struct dictionary_entry * b, struct dictionary_entry * c, struct dictionary_entry * d );
void bubble_sort( struct dictionary_entry * head );
struct dictionary_entry * head = NULL;

int main()
{
    //Prompt for user and organization of visual output.
    printf( "Type a sentence then press enter:\n\n" );
    scan();//
    printf( "\nA." ); //
    print();////
    printf( "\n\nB." ); //
    reverse();//
    printf( "\n\nC." ); //
    alphabetized();//
    printf( "\n\n" ); //
    //initialize();//
    sentence_into_list();//
    display( head );
    bubble_sort( head );
    display( head );
    return 0;
}

void scan()
{
    for ( word_number = 0;; word_number++ )
    {
        scanf( "%s", sentence[word_number] );
        number_of_words++;

        if ( getchar() == '\n' )
        {
            break;
        }
    }
}

void print()
{
    for ( word_number = 0; word_number <= number_of_words; word_number++ )
    {
        printf( "%s ", sentence[word_number] );
    }
}

void reverse()
{
    for ( word_number = number_of_words; word_number >= 0; word_number-- )
    {
        printf( "%s ", sentence[word_number] );
    }
}

void alphabetized()
{
    for ( word_number = 0; word_number <= number_of_words; word_number++ )
    {
        strcpy( word, sentence[word_number] );
        number_of_letters = strlen( word );

        for ( letter_a = 0; letter_a < number_of_letters - 1; letter_a++ )
        {
            for ( letter_b = letter_a + 1; letter_b < number_of_letters; letter_b++ )
            {
                // This section compares the two letters.
                int  sentence = tolower( word[letter_a] ) - tolower( word[letter_b] );

                if ( sentence == 0 )
                {
                    sentence = word[letter_a] - word[letter_b];
                }

                if ( sentence > 0 )
                {
                    temp_word = word[letter_a];
                    word[letter_a] = word[letter_b];
                    word[letter_b] = temp_word;
                }
            }
        }

        printf( "%s ", word );
    }
}

//
// PART TWO OF THE KEIL PROGRAMMING CHALLENGE
//

void add( struct dictionary_entry ** head, char * four_letters ) // do I need wordl and fletters in here?
{
    // I changed add from struct to void
    struct dictionary_entry * new_entry = malloc( sizeof( struct dictionary_entry ) );
    // I have to have code that passes first four letter and the amount of letters here but can i use two portions for both
    // the original sentence and the new additions?

    //new_entry->number_of_letters = number_of_letters;//_temp;?
    memcpy( new_entry->four_letters, four_letters, 5 );
    new_entry->next_entry = *head;
    *head = new_entry;
    // error return head;
}

void sentence_into_list()
{
    for ( word_number = 0; word_number <= number_of_words; word_number++ )
    {
        // number_of_letters = strlen(word);
        four_letters[4] = '\0';
        memcpy( four_letters, sentence[word_number], 4 );
        add( &head, four_letters );
    }
}

void display( struct dictionary_entry * head )
{
    struct dictionary_entry * current;
    current = head;

    if ( current != NULL )
    {
        printf( "Stack:" );

        do
        {
            printf( "%s ", current->four_letters );
            current = current->next_entry;
        }
        while ( current != NULL );

        printf( "\n" );
    }
    else
    {
        printf( "The Stack is empty\n" );
    }
}


void bubble_sort( struct dictionary_entry * head ) //alphabetized
{
    //declaration of used variables
    struct dictionary_entry * selector; // for node selection
    int swapd; // for complete alphabetized contidion lol condition*
    int i; //for progression in word comparison
    int ooseven; // trying for the alphabetization result
    char tword[5], tword2[5]; //for word comparison

    //until alphabetized( when there are 0 swaps)
    while ( swapd != 0 )
    {
        swapd = 0;

        //list start
        selector = head;

        while ( selector->next_entry != NULL ) //list progression
        {
            memcpy( tword, selector->next_entry->four_letters, 5 );
            memcpy( tword2, selector->next_entry->next_entry->four_letters, 5 );
            //list pair comparison
            //2nd, dont need this->//if () //a little lost going top down so ill do bottom up, 1st

        }

//goes trhough word
        for ( i = 0; i < 4; i++ )
        {
            ooseven = tolower( tword[i] ) - tolower( tword2[i] );

            //compares two letters //3 cases
            if ( ooseven > 0 )
            {
                swap( selector, selector->next_entry, selector->next_entry->next_entry, selector->next_entry->next_entry->next_entry );
                //end word comparison
                i = 5;
            }
            else if ( ooseven < 0 )
            {
                //end word comparison
                i = 5;
            }

            //dont need this caseif(tword[i]=tword2[i])
            //continue through word
        }

        selector = selector->next_entry; //progression abstraction
    }

}

void swap( struct dictionary_entry * a, struct dictionary_entry * b, struct dictionary_entry * c, struct dictionary_entry * d )
{
    *a = *c; //do i need to *a *c?
    *c = *b;
    *b = *d;
}

1 个答案:

答案 0 :(得分:1)

您可以借助以下方法将实际选择的节点与下一个节点进行比较:

if((strcmp(node.String1,node.String2) >0 ){
  //Statements
}

使用此功能时,您可以按字母顺序对列表进行排序