char(包含单词)数组冒泡排序问题(三)

标签 c char bubble-sort cpu-word

C 中的输出错误。 如果您编译并运行,则不会对数组中的单词进行排序。我的C信息很少。你能看到我的代码中的错误吗?

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

struct node {
   char data;
   int key;
   struct node *next;
};


struct node *head = NULL;
struct node *current = NULL;

//display the list
void printList() {
   struct node *ptr = head;
   printf("\n");

   //start from the beginning
   while(ptr != NULL) {
      printf("(%d -> %c)",ptr->key,ptr->data);
      printf("\n");
      ptr = ptr->next;
   }
}

//insert link at the first location
void insertFirst(int key, char data) {
   //create a link

   struct node *link = (struct node*) malloc(sizeof(struct node));

   link->key = key;
   link->data = data;

   //point it to old first node
   link->next = head;

   //point first to new first node
   head = link;
}

//is list empty
bool isEmpty() {
   return head == NULL;
}

int length() {
   int length = 0;
   struct node *current;

   for(current = head; current != NULL; current = current->next) {
      length++;
   }

   return length;
}

void buble_sort() {

   int i, j, k, tempKey;
   char tempData;
   struct node *current;
   struct node *next;

   int size = length();
   k = size ;

   for ( i = 0 ; i < size - 1 ; i++, k-- ) {
      current = head;
      next = head->next;

      for ( j = 1 ; j < k ; j++ ) {   

         if ( current->data > next->data ) {
            tempData = current->data;
            current->data = next->data;
            next->data = tempData;

            tempKey = current->key;
            current->key = next->key;
            next->key = tempKey;
         }

         current = current->next;
         next = next->next;
      }
   }   
}    

void main() {
   insertFirst(1,"Papatya");
   insertFirst(2,"DortKardes");
   insertFirst(3,"Toroslu");
   insertFirst(4,"PostEdu");
   insertFirst(5,"Adana");

   buble_sort();

   printf("Buble Sort ile Siralanmis Hali : ");
   printList();
}

最佳答案

您应该阅读有关指针的内容并在 c 中使用字符串。

正如评论中提到的。要包含字符串,您应该使用 char *。

这是工作代码的示例:

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

struct node {
   char *data;
   int key;
   struct node *next;
};


struct node *head = NULL;
struct node *current = NULL;

//display the list
void printList() {
   struct node *ptr = head;
   printf("\n");

   //start from the beginning
   while(ptr != NULL) {
      printf("(%d -> %s)",ptr->key,ptr->data);
      printf("\n");
      ptr = ptr->next;
   }
}

//insert link at the first location
void insertFirst(int key, char *data) {
   //create a link

   struct node *link = (struct node*) malloc(sizeof(struct node));

   link->key = key;
   link->data = data;

   //point it to old first node
   link->next = head;

   //point first to new first node
   head = link;
}

//is list empty
bool isEmpty() {
   return head == NULL;
}

int length() {
   int length = 0;
   struct node *current;

   for(current = head; current != NULL; current = current->next) {
      length++;
   }

   return length;
}

void buble_sort() {

   int i, j, k, tempKey;
   char *tempData = NULL;
   struct node *current;
   struct node *next;

   int size = length();
   k = size ;

   for ( i = 0 ; i < size - 1 ; i++, k-- ) {
      current = head;
      next = head->next;

      for ( j = 1 ; j < k ; j++ ) {   

      if ( strcmp(current->data, next->data) > 0 ) {
            tempData = current->data;
            current->data = next->data;
            next->data = tempData;

            tempKey = current->key;
            current->key = next->key;
            next->key = tempKey;
         }

         current = current->next;
         next = next->next;
      }
   }   
}    

void main() {
   insertFirst(1,"Papatya");
   insertFirst(2,"DortKardes");
   insertFirst(4,"PostEdu");
   insertFirst(5,"Adana");
   insertFirst(3,"Toroslu");

   buble_sort();

   printf("Buble Sort ile Siralanmis Hali : ");
   printList();
}

关于char(包含单词)数组冒泡排序问题(三),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53338187/

相关文章:

c - 如何安全地将两个可变大小的数据类型(结构)放在一个结构中?

c - 函数 invertNumerSubstrings 不起作用

c - 结构中的 const 变量会在结构变量初始化时进入 RAM 吗?

c - getchar() 不返回或继续

string - 序言 : Remove extra spaces in a stream of characters

c - 将 char 存储在数组 C 中无法正常工作

Erlang 冒泡排序

arrays - Delphi中的数组(对象帕斯卡)使用变量?

objective-c - 使用 Cocoa 类进行冒泡排序

c - 我是否需要为 MPI::Isend 提供相应的 MPI::Irecv?