字符(包含单词)数组布尔排序问题(c)

字符(包含单词)数组布尔排序问题(c),c,char,bubble-sort,word,C,Char,Bubble Sort,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;

输出是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();
}
#包括
#包括
#包括
#包括
结构节点{
字符数据;
int键;
结构节点*下一步;
};
结构节点*head=NULL;
结构节点*current=NULL;
//显示列表
作废打印列表(){
结构节点*ptr=头部;
printf(“\n”);
//从头开始
while(ptr!=NULL){
printf((%d->%c)”,ptr->key,ptr->data);
printf(“\n”);
ptr=ptr->next;
}
}
//在第一个位置插入链接
void insertFirst(int键,字符数据){
//创建链接
结构节点*链接=(结构节点*)malloc(sizeof(结构节点));
链接->键=键;
链接->数据=数据;
//将其指向旧的第一个节点
链接->下一步=头部;
//首先指向新的第一个节点
头=链接;
}
//列表是空的吗
布尔是空的{
返回头==NULL;
}
int-length(){
整数长度=0;
结构节点*当前;
对于(当前=头部;当前!=NULL;当前=当前->下一步){
长度++;
}
返回长度;
}
void buble_sort(){
inti,j,k,tempKey;
字符数据;
结构节点*当前;
结构节点*下一步;
int size=length();
k=尺寸;
对于(i=0;i下一步;
对于(j=1;j数据>下一步->数据){
tempData=当前->数据;
当前->数据=下一个->数据;
下一步->数据=临时数据;
tempKey=当前->键;
当前->键=下一个->键;
下一步->键=临时键;
}
当前=当前->下一步;
下一步=下一步->下一步;
}
}   
}    
void main(){
insertFirst(1,“帕帕蒂亚”);
插图一(2,“DortKardes”);
insertFirst(3,“Toroslu”);
insertFirst(4,“毕业后”);
插图一(5,“阿达纳”);
buble_sort();
printf(“Siralamis Hali的Buble排序:”);
打印列表();
}

您应该阅读有关指针的内容,并在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();
}
#包括
#包括
#包括
#包括
结构节点{
字符*数据;
int键;
结构节点*下一步;
};
结构节点*head=NULL;
结构节点*current=NULL;
//显示列表
作废打印列表(){
结构节点*ptr=头部;
printf(“\n”);
//从头开始
while(ptr!=NULL){
printf((%d->%s)”,ptr->key,ptr->data);
printf(“\n”);
ptr=ptr->next;
}
}
//在第一个位置插入链接
void insertFirst(int键,char*数据){
//创建链接
结构节点*链接=(结构节点*)malloc(sizeof(结构节点));
链接->键=键;
链接->数据=数据;
//将其指向旧的第一个节点
链接->下一步=头部;
//首先指向新的第一个节点
头=链接;
}
//列表是空的吗
布尔是空的{
返回头==NULL;
}
int-length(){
整数长度=0;
结构节点*当前;
对于(当前=头部;当前!=NULL;当前=当前->下一步){
长度++;
}
返回长度;
}
void buble_sort(){
inti,j,k,tempKey;
char*tempData=NULL;
结构节点*当前;
结构节点*下一步;
int size=length();
k=尺寸;
对于(i=0;i下一步;
对于(j=1;j数据,下一步->数据)>0){
tempData=当前->数据;
当前->数据=下一个->数据;
下一步->数据=临时数据;
tempKey=当前->键;
当前->键=下一个->键;
下一步->键=临时键;
}
当前=当前->下一步;
下一步=下一步->下一步;
}
}   
}    
void main(){
insertFirst(1,“帕帕蒂亚”);
插图一(2,“DortKardes”);
insertFirst(4,“毕业后”);
插图一(5,“阿达纳”);
insertFirst(3,“Toroslu”);
buble_sort();
printf(“Siralamis Hali的Buble排序:”);
打印列表();
}

编译时启用所有警告,并将警告视为错误。我收到了10个相当错误的警告。你把
char
char*
混在一起了。你的结构和插入函数需要
char数据
,但你传递的是字符串文本,它们是
char*
。按照Jabberwocky的建议去做,编译器会告诉你的。在C中,指定的字符格式是%C,字符串是%s。您可以使用malloc()动态分配char的大小,并确保使用free()或array。此外,简单地将字符串的指针复制到结构中肯定是一个非常糟糕的主意。谢谢大家。只有buble排序方法不会对->Z数组进行排序。我看这个。请再复制一次代码。我已经安排好了你的那一类。只需将当前->数据->下一步->数据更改为当前->键>下一步->键即可。@KurokawaMasato显然,在您的编辑之后它就可以工作了。但我的第二点意见仍然有效。在将指针复制到结构中之前,您应该复制字符串,或者使用类似于
char data[100]
的内容,而不是
char*data
,并使用
strcpy
@Jabberwocky感谢您的更正:)我粘贴了一个存储在缓冲区中的旧代码。