链接列表中的链接列表

时间:2015-03-25 17:19:33

标签: c linked-list

我试图使用嵌套链接列表,但我似乎无法访问链接列表的内部部分。

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

int option;

struct nodeAddr
{
  int idAddress;
  char address[50];
  struct nodeAddr *nextAddr;
}*headAddr,*tailAddr,*newAddr,*helpAddr;

struct nodePrs
{
    int idPersonel;
    char personel[50];
    struct nodeAddr *headAddr,*tailAddr,*newAddr,*helpAddr ;
    struct nodePrs *nextPrs;
}*headPrs,*tailPrs,*newPrs,*helpPrs;

void signUp()
{
    clrscr();
    printf("\t============================\n");
    printf("\t=   Sign Up New Personel   =\n");
    printf("\t============================\n\n");
    newPrs=(nodePrs*)malloc(sizeof(struct nodePrs));
    newPrs->newAddr=(nodeAddr*)malloc(sizeof(struct nodeAddr));
    printf("Name : ");
    scanf("%s",newPrs->personel);
    printf("Address : ");
    scanf("%s",newPrs->newAddr->address);
    newPrs->nextPrs = NULL;
    newPrs->newAddr->nextAddr = NULL;
    if (headPrs==NULL)
    {
        headPrs = newPrs;
        headPrs->headAddr = newPrs->newAddr;
    }
    else
    {
        tailPrs->nextPrs=newPrs;
    }
    tailPrs=newPrs;
    tailPrs->tailAddr=newPrs->newAddr;
}

void printList()
{
    helpPrs = headPrs;
    nodePrs *temp = headPrs;
    if (headPrs==NULL)
        printf("Empty");
    else
    {
        printf("the Outputs:\n");
        while(helpPrs!=NULL)
        {
            printf("%s", helpPrs->personel);
            printf(" ");
            helpPrs->helpAddr = temp->headAddr;
            while (helpPrs->helpAddr !=NULL)
            {
                printf("%s", helpPrs->helpAddr->address);
                printf(" ");
                helpPrs->helpAddr = helpPrs->helpAddr->nextAddr;
            }
            helpPrs = helpPrs->nextPrs;
            temp=temp->nextPrs;
            printf("\n");
       }
    }
}

void main()
{
    do
    {
        clrscr();
        printf("\t==================================\n");
        printf("\t= Linked list inside Linked List =\n");
        printf("\t==================================\n\n");
        printf("1. Sign\n");
        printf("2. Show\n");
        printf("3. Exit\n\n");
        printf("Input (1-3) : ");
        scanf("%d",&option);
        if (option == 1)
        {
            signUp();
        }
        else if (option == 2)
        {
            printList();
            getch();
        }
        else if(option>2 || option <1)
        {
            printf("Wrong choice!!!");
        }
    } while(option!=5);
    getch();
}

问题出在printList()函数中。它只打印第一个列表。之后,它无法显示第二个条目的地址,依此类推。

0 个答案:

没有答案