如何用我已有的元素填充链接列表?

时间:2019-05-05 10:02:37

标签: c linked-list

我正在尝试使用我拥有的元素创建一个链表,但是我需要24个节点,而我不想这样结束:

head->next->next->next->next->next->next->next->id = 1;

如何防止这种情况?

我尝试创建类似对象,但所有节点(显然)都指向相同的数据。

void init_board(block **head)
{
    block temp;

    temp.id=0;
    temp.name="Start";
    temp.price=0;
    temp.rent=0;
    temp.next = NULL;

    *head = &temp;
    (*head)->next = NULL;
    (*head)->next = (block*) malloc(sizeof(block*));
    temp = head->next;

    temp.id=1;
    temp.name="End";
    temp.price=16000;
    temp.rent=800;
    temp.next = NULL;
}

2 个答案:

答案 0 :(得分:0)

  

我正在尝试使用我拥有的元素创建一个链表,但是我需要24个节点,而我不想这样结束:

     

head-> next-> next-> next-> next-> next-> next-> next-> id = 1;   我该如何预防?

更新 head (不仅*head

 block temp;
 ...
 temp = head->next;

您不能这样做,因为 temp 是s struct ,但是 next 是指针

  

我尝试创建类似对象,但所有节点(显然)都指向相同的数据。

您需要为所有新元素分配一个新的单元格,包括您当前放入堆栈中的第一个单元格(从不返回存储在堆栈中的内容的地址)

  

head)-> next =(block )malloc(sizeof(block *));

这不是您想要的,您需要分配一个 block ,而不是一个 block *

使用两个单元格初始化的示例:

void init_board(block **plast)
{
  *plast = malloc(sizeof(block));
  (*plast)->id=0;
  (*plast)->name="Start";
  (*plast)->price=0;
  (*plast)->rent=0;
  (*plast)->next = malloc(sizeof(block));
  plast = &(*plast)->next;
  (*plast)->id=1;
  (*plast)->name="End";
  (*plast)->price=16000;
  (*plast)->rent=800;
  (*plast)->next = NULL;
}

int main()
{
   block * l;

   init_board(&l);
}

当然,如果您有20个块进行初始化以扩展每种情况是不实际的,则可能值来自文件或类似的数组:

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

typedef struct block {
  int id;
  const char * name;
  int price;
  int rent;
  struct block * next;
} block;

const block Boards[] = {
  { 0, "Start", 0, 0, NULL },
  { 2, "Intermediate", 123, 456, NULL },
  { 1, "End", 16000, 800, NULL }
};

void init_board(block **plast)
{
  for (const block * b = Boards; b != Boards + sizeof(Boards)/sizeof(Boards[0]); ++b) {
    *plast = malloc(sizeof(block));

    (*plast)->id = b->id;
    (*plast)->name = b->name;
    (*plast)->price = b->price;
    (*plast)->rent = b->rent;
    (*plast)->next = NULL;
    plast = &(*plast)->next;
  }
}

int main()
{
  block * blocks;

  init_board(&blocks);

  /* debug */
  for (block * b = blocks; b != NULL; b = b->next)
    printf("%d %s %d %d\n", b->id, b->name, b->price, b->rent);

  /* free resources */
  while (blocks != NULL) {
    block * b = blocks;

    blocks = blocks->next;
    free(b);
  }

  return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall l.c
pi@raspberrypi:/tmp $ ./a.out
0 Start 0 0
2 Intermediate 123 456
1 End 16000 800

valgrind 下执行:

pi@raspberrypi:/tmp $ valgrind ./a.out
==6819== Memcheck, a memory error detector
==6819== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==6819== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==6819== Command: ./a.out
==6819== 
0 Start 0 0
2 Intermediate 123 456
1 End 16000 800
==6819== 
==6819== HEAP SUMMARY:
==6819==     in use at exit: 0 bytes in 0 blocks
==6819==   total heap usage: 4 allocs, 4 frees, 1,084 bytes allocated
==6819== 
==6819== All heap blocks were freed -- no leaks are possible
==6819== 
==6819== For counts of detected and suppressed errors, rerun with: -v
==6819== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
pi@raspberrypi:/tmp $ 

答案 1 :(得分:-1)

struct link 
{
  int data;
  int dataOne;
  // data n....
   struct link* link;
};
// HeadNode
struct link* pHeadLink = NULL;
//add the link and return the current.
struct link* appendLink()
{
   if(pHeadLink == NULL)
   {
     pHeadLink = (struct link*) malloc(sizeof(struct link));
     pHeadLink->link = NULL;
     return pHeadLink;
  }
  struct link *pTempLink = pHeadLink;
  while(pTempLink->link != NULL)
  {
     pTempLink = pTempLink->link;
  }
  pTempLink->link = (struct link*) malloc(sizeof(struct link));
  pTempLink->link->link = NULL;
  return pTempLink;
}
// calling function:
int fun()
{

 loop() // loop for 24 times. 
{ 
  struct link* pFillDataLink = appendLink();
   // here you can fill rest the items like below.
   pFillDataLink->data = 34;
   pFillDataLink->dataOne = 334; 
   // etc.... 
}
}