排序链表在C中工作不正常

排序链表在C中工作不正常,c,linked-list,singly-linked-list,insertion,alphabetical,C,Linked List,Singly Linked List,Insertion,Alphabetical,我对C语言完全陌生,我试图用C语言制作一个链表,按字母顺序对预先存在的字符数组进行排序。每个字符分配一个索引。因此,listInsert函数应该将每个字符串插入到链表中,并按字母顺序对它们进行排序,以便在调用listPrintForward时,链表中的项目将按字母顺序打印 这是我运行程序时的当前输出 正如您所看到的,我的listInsert函数当前所做的就是将它们插入到一个链表中(我想),当调用printForward函数时,它会反转链表的内容,此时它应该按字母顺序打印链表的内容 我希望的输出

我对C语言完全陌生,我试图用C语言制作一个链表,按字母顺序对预先存在的字符数组进行排序。每个字符分配一个索引。因此,listInsert函数应该将每个字符串插入到链表中,并按字母顺序对它们进行排序,以便在调用listPrintForward时,链表中的项目将按字母顺序打印

这是我运行程序时的当前输出

正如您所看到的,我的listInsert函数当前所做的就是将它们插入到一个链表中(我想),当调用printForward函数时,它会反转链表的内容,此时它应该按字母顺序打印链表的内容

我希望的输出应该是:

INSERT:
bravo oscar romeo delta whisky alpha
foxtro sierra yankee lima echo
golf november victor charlie mike
zulu tango kilo quebec hotel
juliet xray papa uniform india

FORWARD: 0 entries
alpha bravo charlie delta echo foxtrot
golf hotel india juliet kilo
lima mike november oscar papa
quebec romeo sierra tango uniform
victor whisky xray yankee zulu 
有人知道如何解决这个问题,或者我做错了什么吗?以下是完整的代码:

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

#define SUCCESS 0
#define FAIL    1

char *phonetic[] = { "alpha", "bravo", "charlie", "delta", "echo", "foxtrot",
                     "golf", "hotel", "india", "juliet", "kilo", "lima", "mike",
                     "november", "oscar", "papa", "quebec", "romeo", "sierra",
                     "tango", "uniform", "victor", "whisky", "xray", "yankee", 
                     "zulu" };

unsigned char indexes[] = { 1, 14, 17, 3, 22, 0, 5, 18, 24, 11, 4, 6, 13, 21,
                            2, 12, 25, 19, 10, 16, 7, 9, 23, 15, 20, 8 };                       

// represents an entry in the linked-list
struct listEntry
{
  char *data_p;               // pointer to the entry's string
  struct listEntry *prev_p;   // pointer to previous entry in the linked-list  
  struct listEntry *next_p;   // pointer to next entry in the linked-list
};

// represents the linked-list
struct list
{
  int entryCount;             // number of entries present in the linked-list
  struct listEntry *head_p;   // pointer to the first entry in the list  
  struct listEntry *tail_p;   // pointer to the last entry in the list
};

// Dynamically allocate & initialise an empty linked list
int listCreate(struct list** list_p2)
{
  // allocate struct list from heap 
  *list_p2 = (struct list*) malloc(sizeof(**list_p2));

  if (*list_p2 != NULL)
  {
    // zero-initialize the list structure 
    memset(*list_p2, 0, sizeof(**list_p2));
    return SUCCESS;    
  }

  return FAIL;
}

// Free all entries in the linked-list and the list structure
int listDestroy(struct list *list_p)
{
  if (list_p != NULL)
  {
    struct listEntry *entry_p = list_p->head_p;

    while (entry_p != NULL)
    {
      struct listEntry *next_p = entry_p->next_p;
      // free the current entry
      free(entry_p);
      // move to the next entry
      entry_p = next_p;
    }

    // free list structure
    free(list_p);
  }

  return FAIL;
}

// Traverse the linked-list from head to tail printing out
// the string data from each list entry
int listPrintForward(struct list *list_p)
{ 
  if (list_p)
  {    
    struct listEntry *entry_p = list_p->head_p;
    int count = 0;

    printf("FORWARD: %d entries\n", list_p->entryCount);
    while (entry_p != NULL)
    {
      if ((count > 0) && (count % 5 == 0))
      {
        printf("%s\n", entry_p->data_p);
      }
      else
      {      
        printf("%s ", entry_p->data_p);
      }

      if (entry_p == list_p->tail_p)
        printf("\n");

      entry_p = entry_p->next_p;
      fflush(stdout);
      count++;         
    }

    return SUCCESS;
  }

  return FAIL;
}

// Traverse the linked-list from tail to head printing out
// the string data from each list entry
int listPrintReverse(struct list *list_p)
{ 
  if (list_p)
  {    
    struct listEntry *entry_p = list_p->tail_p;
    int count = 0;

    printf("REVERSE: %d entries\n", list_p->entryCount);   
    while (entry_p != NULL)
    {
      if ((count > 0) && (count % 5 == 0))
      {
        printf("%s\n", entry_p->data_p);
      }
      else
      {      
        printf("%s ", entry_p->data_p);
      }

      if (entry_p == list_p->head_p)
        printf("\n");

      entry_p = entry_p->prev_p;
      fflush(stdout);
      count++;         
    }

    return SUCCESS;
  }

  return FAIL;
}

// Insert the given string into the linked-list such that the
// entries in the linked-list are in alphabetical order
int listInsert(struct list *list_p, char *string_p)
{ 
    struct listEntry *temp;
    temp=(struct listEntry *)malloc(sizeof(struct listEntry)); 
    temp->data_p = string_p;

    if (list_p->head_p == NULL)
    {
        //List is Empty
        list_p->head_p = temp;
        temp->next_p = NULL;
    }
    else
    {
        temp->next_p =list_p->head_p;
        list_p->head_p = temp;
    }


  return FAIL;  
}

int main(int argc, char **argv)
{
  struct list *list_p = NULL;
  (void) argc;
  (void) argv;

  if (listCreate(&list_p) == SUCCESS)
  {
    unsigned int count;

    // insert every word in the phonetic alphabet into the
    // linked-list.
    printf("INSERT:\n");
    for (count = 0; count < sizeof(indexes); count++)
    {
      if ((count > 0) && (count % 5 == 0))
      {
        printf("%s\n", phonetic[indexes[count]]);
      }
      else
      {
        printf("%s ", phonetic[indexes[count]]);
      }
      listInsert(list_p, phonetic[indexes[count]]);
    }
    printf("\n");

    // print out the list in alphabetical order
    listPrintForward(list_p);
    // print out the list in reverse alphabetical order
    listPrintReverse(list_p); 

    // Destroy the linked list and free all associated memory
    listDestroy(list_p);               
  }

  return SUCCESS;
} 
#包括
#包括
#包括
#定义成功0
#定义失败1
字符*语音[]={“alpha”,“bravo”,“charlie”,“delta”,“echo”,“foxtrot”,
“高尔夫”、“酒店”、“印度”、“朱丽叶”、“基洛”、“利马”、“迈克”,
“十一月”、“奥斯卡”、“爸爸”、“魁北克”、“罗密欧”、“塞拉”,
“探戈”,“制服”,“维克多”,“威士忌”,“X光”,“扬基”,
“祖鲁”};
无符号字符索引[]={1,14,17,3,22,0,5,18,24,11,4,6,13,21,
2, 12, 25, 19, 10, 16, 7, 9, 23, 15, 20, 8 };                       
//表示链接列表中的一个条目
结构列表输入
{
char*data\u p;//指向条目字符串的指针
struct listEntry*prev_p;//指向链接列表中上一个条目的指针
struct listEntry*next\u p;//指向链接列表中下一项的指针
};
//表示链接列表
结构列表
{
int entryCount;//链表中存在的条目数
struct listEntry*head\u p;//指向列表中第一个条目的指针
struct listEntry*tail\u p;//指向列表中最后一个条目的指针
};
//动态分配并初始化空链表
int listCreate(结构列表**list\u p2)
{
//从堆中分配结构列表
*list_p2=(struct list*)malloc(sizeof(**list_p2));
如果(*list_p2!=NULL)
{
//零初始化列表结构
memset(*list_p2,0,sizeof(**list_p2));
回归成功;
}
返回失败;
}
//释放链表和列表结构中的所有条目
int listDestroy(结构列表*列表)
{
if(list_p!=NULL)
{
struct listEntry*entry\u p=list\u p->head\u p;
while(entry_p!=NULL)
{
struct listEntry*next\u p=entry\u p->next\u p;
//释放当前条目
免费(入境);
//移动到下一个条目
条目p=下一个;
}
//自由列表结构
免费(列表p);
}
返回失败;
}
//从头到尾遍历链表打印输出
//来自每个列表项的字符串数据
int listPrintForward(结构列表*列表)
{ 
如果(列表p)
{    
struct listEntry*entry\u p=list\u p->head\u p;
整数计数=0;
printf(“转发:%d个条目”,列表->入口计数);
while(entry_p!=NULL)
{
如果((计数>0)和&(计数%5==0))
{
printf(“%s\n”,条目->数据);
}
其他的
{      
printf(“%s”,条目->数据);
}
如果(条目p==列表p->尾部p)
printf(“\n”);
entry\u p=entry\u p->next\u p;
fflush(stdout);
计数++;
}
回归成功;
}
返回失败;
}
//从尾部到头部遍历链表打印输出
//来自每个列表项的字符串数据
int listPrintReverse(结构列表*列表)
{ 
如果(列表p)
{    
struct listEntry*entry\u p=list\u p->tail\p;
整数计数=0;
printf(“反向:%d条目的\n”,列表\u p->entryCount);
while(entry_p!=NULL)
{
如果((计数>0)和&(计数%5==0))
{
printf(“%s\n”,条目->数据);
}
其他的
{      
printf(“%s”,条目->数据);
}
如果(条目p==列表p->标题p)
printf(“\n”);
条目p=条目p->prev\u p;
fflush(stdout);
计数++;
}
回归成功;
}
返回失败;
}
//将给定字符串插入到链接列表中,以便
//链表中的条目按字母顺序排列
int listInsert(结构列表*列表,字符*字符串)
{ 
结构列表输入*temp;
temp=(struct listEntry*)malloc(sizeof(struct listEntry));
临时->数据\u p=字符串\u p;
如果(列表->标题=NULL)
{
//列表为空
列表\u p->head\u p=temp;
temp->next_p=NULL;
}
其他的
{
temp->next\u p=列表\u p->head\u p;
列表\u p->head\u p=temp;
}
返回失败;
}
int main(int argc,字符**argv)
{
结构列表*列表p=NULL;
(无效)argc;
(无效)argv;
if(listCreate(&list_p)=成功)
{
无符号整数计数;
//将拼音字母表中的每个单词插入
//链表。
printf(“插入:\n”);
对于(count=0;count0)和&(计数%5==0))
{
printf(“%s\n”,拼音[索引[计数]);
}
其他的
{
printf(“%s”,拼音[索引[计数]);
}
listInsert(list_p,拼音[索引[计数]);
}
printf(“\n”);
//按字母顺序把清单打印出来
listPrintForward(列表p);
//按逆字母顺序打印列表
listPrintReverse(列表p);
//销毁链接列表并释放所有相关内存
列表销毁(列表p);
}
回归成功;
} 

您正在按索引顺序插入条目。您应该对temp->p_数据执行strcmp,从头到尾遍历列表并插入节点

给个提示

问候


Johan

运行程序时会得到什么输出?请检查我放在顶部附近的链接。请直接发布输出,而不是输出的屏幕转储。完成。请检查上面。我看不见地图
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SUCCESS 0
#define FAIL    1

char *phonetic[] = { "alpha", "bravo", "charlie", "delta", "echo", "foxtrot",
                     "golf", "hotel", "india", "juliet", "kilo", "lima", "mike",
                     "november", "oscar", "papa", "quebec", "romeo", "sierra",
                     "tango", "uniform", "victor", "whisky", "xray", "yankee", 
                     "zulu" };

unsigned char indexes[] = { 1, 14, 17, 3, 22, 0, 5, 18, 24, 11, 4, 6, 13, 21,
                            2, 12, 25, 19, 10, 16, 7, 9, 23, 15, 20, 8 };                       

// represents an entry in the linked-list
struct listEntry
{
  char *data_p;               // pointer to the entry's string
  struct listEntry *prev_p;   // pointer to previous entry in the linked-list  
  struct listEntry *next_p;   // pointer to next entry in the linked-list
};

// represents the linked-list
struct list
{
  int entryCount;             // number of entries present in the linked-list
  struct listEntry *head_p;   // pointer to the first entry in the list  
  struct listEntry *tail_p;   // pointer to the last entry in the list
};

// Dynamically allocate & initialise an empty linked list
int listCreate(struct list** list_p2)
{
  // allocate struct list from heap 
  *list_p2 = (struct list*) malloc(sizeof(**list_p2));

  if (*list_p2 != NULL)
  {
    // zero-initialize the list structure 
    memset(*list_p2, 0, sizeof(**list_p2));
    return SUCCESS;    
  }

  return FAIL;
}

// Free all entries in the linked-list and the list structure
int listDestroy(struct list *list_p)
{
  if (list_p != NULL)
  {
    struct listEntry *entry_p = list_p->head_p;

    while (entry_p != NULL)
    {
      struct listEntry *next_p = entry_p->next_p;
      // free the current entry
      free(entry_p);
      // move to the next entry
      entry_p = next_p;
    }

    // free list structure
    free(list_p);
  }

  return FAIL;
}

// Traverse the linked-list from head to tail printing out
// the string data from each list entry
int listPrintForward(struct list *list_p)
{ 
  if (list_p)
  {    
    struct listEntry *entry_p = list_p->head_p;
    int count = 0;

    printf("FORWARD: %d entries\n", list_p->entryCount);
    while (entry_p != NULL)
    {
      if ((count > 0) && (count % 5 == 0))
      {
        printf("%s\n", entry_p->data_p);
      }
      else
      {      
        printf("%s ", entry_p->data_p);
      }

      if (entry_p == list_p->tail_p)
        printf("\n");

      entry_p = entry_p->next_p;
      fflush(stdout);
      count++;         
    }

    return SUCCESS;
  }

  return FAIL;
}

// Traverse the linked-list from tail to head printing out
// the string data from each list entry
int listPrintReverse(struct list *list_p)
{ 
  if (list_p)
  {    
    struct listEntry *entry_p = list_p->tail_p;
    int count = 0;

    printf("REVERSE: %d entries\n", list_p->entryCount);   
    while (entry_p != NULL)
    {
      if ((count > 0) && (count % 5 == 0))
      {
        printf("%s\n", entry_p->data_p);
      }
      else
      {      
        printf("%s ", entry_p->data_p);
      }

      if (entry_p == list_p->head_p)
        printf("\n");

      entry_p = entry_p->prev_p;
      fflush(stdout);
      count++;         
    }

    return SUCCESS;
  }

  return FAIL;
}

// Insert the given string into the linked-list such that the
// entries in the linked-list are in alphabetical order
int listInsert(struct list *list_p, char *string_p)
{ 
    struct listEntry *temp;
    temp=(struct listEntry *)malloc(sizeof(struct listEntry)); 
    temp->data_p = string_p;

    if (list_p->head_p == NULL)
    {
        //List is Empty
        list_p->head_p = temp;
        temp->next_p = NULL;
    }
    else
    {
        temp->next_p =list_p->head_p;
        list_p->head_p = temp;
    }


  return FAIL;  
}

int main(int argc, char **argv)
{
  struct list *list_p = NULL;
  (void) argc;
  (void) argv;

  if (listCreate(&list_p) == SUCCESS)
  {
    unsigned int count;

    // insert every word in the phonetic alphabet into the
    // linked-list.
    printf("INSERT:\n");
    for (count = 0; count < sizeof(indexes); count++)
    {
      if ((count > 0) && (count % 5 == 0))
      {
        printf("%s\n", phonetic[indexes[count]]);
      }
      else
      {
        printf("%s ", phonetic[indexes[count]]);
      }
      listInsert(list_p, phonetic[indexes[count]]);
    }
    printf("\n");

    // print out the list in alphabetical order
    listPrintForward(list_p);
    // print out the list in reverse alphabetical order
    listPrintReverse(list_p); 

    // Destroy the linked list and free all associated memory
    listDestroy(list_p);               
  }

  return SUCCESS;
}