构建并打印链接列表,其中每个节点代表C

时间:2018-02-13 01:37:41

标签: c list struct linked-list

正如标题所说,我试图构建一个列表,其中每个节点都指向另一个列表,如下所示:

   5 -> 10 -> 19 -> 28  <--- principal list
   |    |     |     |
   V    V     V     V
   7    20    22    35
   |          |     |
   V          V     V
   8          50    40
   |                |
   V                V
   30               45

每个节点都是另一个列表的头部(如图所示)。 问题是当我尝试打印列表时,每个节点只有最新的元素添加到他自己的列表中,如下所示:

   5 -> 10 -> 19 -> 28  <--- principal list
   |    |     |     |
   V    V     V     V
   7    20    22    35  <--just the last inserted node is added to 
                           every node of the list

我怀疑是添加&#34;页面的功能&#34;节点有点覆盖我写的每个数字,因为只有最后一个显示。在此支持下,我试图手动添加&#34; 2&#34;页面&#34;到每个节点(包含在注释中的代码行),在这种情况下一切正常,所以这应该证明打印功能正常工作。

这是代码

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

struct page
{
    int val;
    struct page *prox;
};

struct node
{
    int date;
    struct node *next;
    struct page *page;
};

typedef struct page* page;
typedef struct node* node;

node newNode()
{
    node Node = malloc(sizeof(node));
    return Node;
}

page newPage()
{
    page page = malloc(sizeof(page));
    return page;
}

void printList(node Node)
{
    node T = Node;
    while(T != NULL)
    {
        printf("%d-->", T->date);
        while(T->page != NULL)
        {
            printf("%d  ", T->page->val);
            T->page = T->page->prox;
        }
        printf("\n §\n");
        T = T->next;
    }

    printf("NULL\n");
}

void inputPage(node Nodee)
{
    node Node = Nodee;
    Node->page = newPage();
    scanf("%d", &Node->page->val);
    for(int x=0; x<1; x++)
    {
        Node->page = Node->page->prox = newPage();
        scanf("%d", &Node->page->val);
    }
}

int main()
{
    node Node = newNode();
    scanf("%d", &Node->date);
     inputPage(Node);
        /*Node->page = newPage();
        Node->page->val = 1;
        Node->page->prox = newPage();
        Node->page->prox->val = 2;*/

    node node = Node;
    for(int y=0; y<1; y++)
    {
        node = node->next = newNode();
        scanf("%d", &node->date);
        inputPage(node);
        /*node->page = newPage();
        node->page->val = 1;
        node->page->prox = newPage();
        node->page->prox->val = 2;*/
    }

    printf("\n");
    printList(Node);
}

2 个答案:

答案 0 :(得分:1)

问题的症结在于,当您尝试迭代pages时,您将node->page设置为下一页,这会覆盖node->page而不是深入潜水

while(T->page != NULL)
{
    // You keep overwriting node->page until it gets null
    T->page = T->page->prox;
}
T = T->next;

迭代通过应该更像是这样:

page p = T->page;
while (page != NULL) {
    // print page->val
    page = page->prox;
}
// Now, go to next node

你的inputPage有类似的问题,但这有点混乱:

node Node = Nodee;
Node->page = newPage();
scanf("%d", &Node->page->val);
// Need to track most recent page here.
for(int x=0; x<1; x++)
{
    // We don't care about Node at this point. Just track the last page
    // since the pages are already attached to the node.
    Node->page = Node->page->prox = newPage();
    scanf("%d", &Node->page->val);
}

答案 1 :(得分:0)

EmptyArsenal已经突出了你的一些错误。我在这里补充一些建议。总是正确地初始化,如下面特别指针: -

node newNode()
{
  node newNode = malloc(sizeof(node));
  newNode->next = NULL;
  newNode->page = NULL;
  return newNode;
}

page newPage()
{
  page newpage = malloc(sizeof(page));//avoid using duplicate variable name
  newpage->prox = NULL;//code should not be confusing
  return newpage;
}

现在修改您的inputPage功能,如下所示: -

void inputPage(node Node)//don't write code which may make you more confuse
{
   if(Node->page == NULL)//check whether list has been created already or not
   {
     Node->page = newPage();//if node list is empty
     scanf("%d", &Node->page->val);
     return;
   }

    //node temp = Node;// we don't need this line of code 
   //we need a page pointer here
   page p = Node->page;//we are pointing to the first page of a Node

   //for(int x=0; x<1; x++)//not a proper loop at all we don't need it
   //here you need to traverse through the page list
   while(p->prox != NULL) //traverse till you reach last page node
   {
       p = p->prox;
   }
       p->page= newPage();//p is now pointing to the last node of page sub list
       scanf("%d", &Node->page->val);
}

现在相应地修改你的主要功能并检查。如果您需要更多帮助,请告诉我们。祝你好运!