【问题标题】:Why does this C code print only the first & last node entered?为什么这个 C 代码只打印输入的第一个和最后一个节点?
【发布时间】:2010-12-22 13:04:41
【问题描述】:
#include <stdio.h>
#include <stdlib.h>

typedef struct 
{
  char *Name;
  int grade;
  int cost;
}Hotel;    /*This is data element in each node*/

typedef struct hinfo
{
  Hotel h;
  struct hinfo *next;
}Hinfo;   /*This is a single node*/

typedef struct
{
  Hinfo *next; /*This is the head pointer of the linked list*/
}HotelHead;

void createHotel(HotelHead *h);
void DisplayHotel(HotelHead h);

int main()
{
  HotelHead *list=(HotelHead *)malloc(sizeof(HotelHead));
  list->next=NULL;
  createHotel(list);
  DisplayHotel(*list);
  return(0);
}

void createHotel(HotelHead *h)  /*This function creates the list of hotels*/
{
  char ans='y';
  while(ans=='y' || ans=='Y')
  {
    char *name=(char *)malloc(20*sizeof(char));
    Hinfo *new=(Hinfo *)malloc(sizeof(Hinfo));
    printf("\nEnter hotel name: ");
    scanf("%[A-Za-z0-9 ]",name);
    printf("\nEnter hotel grade & cost: ");
    scanf("%d %d",&new->h.grade,&new->h.cost);
    new->h.Name=name;
    new->next=NULL;
    if(h->next==NULL){h->next=new;}
    else
    {
      Hinfo *current=h->next;
      while(current->next!=NULL){current->next=current->next->next;}
      current->next=new;
    }
    printf("\nEnter another hotel?(Y/N): ");
    scanf("%s",&ans);
    getchar();          /*dummy getchar to eat unwanted character*/
  }
}

void DisplayHotel(HotelHead h)  /*This function displays all hotels in the list*/
{
  Hinfo *current=h.next;
  printf("\nHotel list:\n");
  while(current!=NULL)
  {
    printf("\n%s %d %d\n",current->h.Name,current->h.grade,current->h.cost);
    current=current->next;
  }
}

【问题讨论】:

  • 请暂时忽略“内存泄漏”
  • 听说过标签按钮吗?

标签: c linked-list


【解决方案1】:

您想在遍历列表时移动current,而不是更改current-&gt;next 的值。改变这个:

while (current->next != NULL) {
    current->next = current->next->next;
}

到这里:

while (current->next != NULL) {
    current = current->next;
}

也就是说,最好在添加新节点的同时移动current,而不是每次都从头开始遍历链表。例如(骨架代码):

Hinfo *current;

while (...) {
    Hinfo *new = malloc(sizeof(Hinfo));

    // initialize new node

    if (current != NULL) {
        current->next = new;
    }

    current = new;

    // prompt to enter more nodes
}

【讨论】:

    【解决方案2】:

    DisplayHotel 功能正常!问题在于 createhotel 功能。 当你这样做时:

    while( current->next != NULL ){
      current->next = current->next->next;
    }
    

    在这里,您实际上是在更改列表,删除一个元素。 尝试做:

    while( current->next != NULL ){
      current = current->next;
    }
    

    另外,最好的方法是总是在头部有一个指向列表最后一个元素的指针,这样你就可以直接添加新元素,而不是总是遍历整个列表! (添加新元素时记得更新头部)

    【讨论】:

      【解决方案3】:

      这是不正确的:

      char *name=(char *)malloc(sizeof(20));

      您分配的是 (sizeof(int)) 个字节,而不是 20 个字节。

      不管你在做什么,这都会导致问题。

      【讨论】:

      • 其实sizeof(20)和sizeof(int)是一样的,因为20默认是整数类型,幸好:)
      • 这就是答案的重点。 sizeof(int) 是错误的。他想要我假设的 20 个字符。
      • 所以代码应该是char *name = malloc(20);?这么说可能会让这里的观点更清楚。
      猜你喜欢
      • 2019-11-17
      • 2011-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 2020-01-04
      相关资源
      最近更新 更多