【问题标题】:char (contain words) array buble sort problem (c)char(包含单词)数组冒泡排序问题(c)
【发布时间】:2018-11-16 12:44:32
【问题描述】:

输出是 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();
}

【问题讨论】:

  • 编译时启用所有警告并将警告视为错误。我收到 10 个相当错误的警告。你混淆了charchar*
  • 你的结构和你的插入函数想要char data,但是你传递了字符串文字,它们是char *。按照 Jabberwocky 的建议去做,编译器会告诉你的。
  • 在 C char 格式中指定为 %c,对于字符串则为 %s。您可以使用 malloc() 动态分配 char 的大小,并确保 free() 或使用数组。

标签: c char bubble-sort word


【解决方案1】:

您应该阅读指针并在 c 中使用字符串。

如 cmets 中所述。要包含字符串,您应该使用 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();
}

【讨论】:

  • 同样简单地将指向字符串的指针复制到结构中肯定是一个非常糟糕的主意。
  • 谢谢大家。只有冒泡排序方法不对 A -> Z 数组进行排序。我看这个。
  • 请重新复制代码。我已经解决了你的问题。只需要将 current->data > next->data 更改为 current->key > next->key。
  • @KurokawaMasato 显然它在您编辑后有效。但我的第二条评论仍然有效。您应该在将指针复制到结构之前复制字符串,或者使用char data[100] 代替char *data 并使用strcpy
  • @Jabberwocky 感谢您的纠正 :) 我粘贴了一个旧代码,它存储在缓冲区中。
猜你喜欢
  • 1970-01-01
  • 2020-12-19
  • 2021-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 2012-11-03
相关资源
最近更新 更多