gpt4 book ai didi

c - 在 C 语言中,对每个节点包含一个字符串的单链表进行冒泡排序?

转载 作者:行者123 更新时间:2023-11-30 20:58:38 27 4
gpt4 key购买 nike

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

最佳答案

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

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

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

关于c - 在 C 语言中,对每个节点包含一个字符串的单链表进行冒泡排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51356778/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com