簡體   English   中英

鏈表內的鏈表

[英]linked list inside linked lists

我想實現一個在鏈表中使用鏈表的程序(它模擬超市流量以獲取您的信息)執行添加、刪除、刪除等操作。這是我的代碼:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct cash_queue cash;
typedef struct client client;
typedef struct item item;
int k,c;//k== cash registers,c == customers
struct item{
  int value;
  struct item* next;
};

struct cash_queue{
   int cash_num;
   struct client *first;
   struct cash_que* next;
};  

struct client{
  int client_num;
  int items;
  struct item *fitem;
  struct client* next;
};
void create_item(item* fitem){ 
  item *item,*cur;
  cur=fitem;
  item=malloc(sizeof(item));
  printf("give product value\n");
  int v;
  scanf(" %d",&v);
  item->value=v;
  printf("value: %d\n",item->value);
  item->next=NULL;
  while (cur->next)
   cur=cur->next;
  cur->next=item;
} 

void create_queue(client* first){
  client *client,*cur;
  cur=first;    
  client=malloc(sizeof(client));
  printf("how many items this client has?\n");
  int x,i;
  scanf("%d",&x);
  client->items=x;
  client->next=NULL;
  client->fitem=malloc(sizeof(item));
  for (i=1;i<=x;++i)
    create_item(client->fitem);
  while (cur->next){
    cur=cur->next;
  }
  cur->next=client;
}
int main(){
   cash* ncash;
  ncash=malloc(sizeof(cash));
  ncash->cash_num=1;
  ncash->next=NULL;
  ncash->first=malloc(sizeof(client));
      printf("give the number of starting customers\n");
  scanf("%d",&c);
  int i;
  for(i=1;i<=c;++i)
    create_queue(ncash->first);
  }

當我嘗試執行此代碼時,我的程序被中止。這是確切的輸出:

 give the number of starting customers
 3
 how many items this client has?
 1
 give product value
  2
 value: 2
 aa: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char              *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size        & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
  Aborted

你能告訴我為什么會發生這種情況,我該如何解決? 謝謝。我的代碼中還有其他問題需要更正嗎?

正如我在評論中所寫,錯誤消息可能意味着您以某種方式濫用了動態分配的內存。 查看您的代碼,情況確實如此。 您有此模式的多個實例:

typedef struct foo foo;

struct foo {
    // elements ...
};

void f() {
    foo *foo;
    foo = malloc(sizeof(foo));
    // ... populate foo ..
}

您在其中試圖過於聰明,使您自己或編譯器或兩者都感到困惑。 包含malloc()調用的語句中的兩個foo是同一個東西:變量foo ,它是struct foo *類型的指針。 除非struct foo的大小恰好與系統上指針的大小相同,否則這是錯誤的做法。 如果結構大於指針,那么寫入其某些成員確實會超出分配的邊界。

有幾種方法可以解決這個問題。 我當然會建議您在命名時更加與眾不同。 我建議您也取消 typedef - 您的類型並不復雜,以至於您可以從中獲得很多好處,並且會花費您代碼的清晰度。 此外,我建議您為所有malloc()調用使用此表單:

bar = malloc(n * sizeof(*bar));

請注意,分配字節數是目標指針指向的對象類型大小的倍數時,這一點很清楚,但它並不直接取決於bar實際類型。 總的來說,那么:

struct bar {
    // elements ...
};

void f() {
    struct bar *temp_bar;
    temp_bar = malloc(sizeof(*temp_bar));
    // ... populate temp_bar ..
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM