簡體   English   中英

將列表連接成列表並在c中打印列表列表(鏈表的鏈表)

[英]connecting lists into a list and printing the list of lists in c (linked list of linked lists)

$ cat tester.c
 #include<stdio.h>
 #include<stdlib.h>

 typedef struct node
 {
    int x;
    struct node *next;
 }node;

 typedef struct
 {
    node *p;
 }list;

 typedef struct stack
 {
    list *q;
    struct stack *next;
 }stack;

 int main()
 {
    //fill the list with numbers
    //link multiple stacks
    int counter = 0;

    list *listone = malloc(sizeof(listone));;

    //make a linked list from 0 - 10
    while(counter < 0)
    {
            node *newest = malloc(sizeof(node));
            newest->x = counter;

            if(listone->p == NULL)
            {
                    listone->p = malloc(sizeof(node));
                    listone->p = newest;
            }//end if
            else
            {
                    newest->next = listone->p;
                    listone->p = newest;
            }//end else
    }//end while

    list *listtwo = malloc(sizeof(listtwo));
    counter = 10;
    //make a second list counting from 10-19
    while(counter < 20)
    {
            node *newer = malloc(sizeof(node));
            newer->x = counter;

            if(listtwo->p == NULL)
            {
                    listtwo->p = malloc(sizeof(node));
                    listtwo->p = newer;
            }//end if
            else
            {
                    newer->next = listtwo->p;
                    listtwo->p = newer;
            }//end else
    }//end while

    stack *s = malloc(sizeof(stack));
    s->q = malloc(sizeof(list));
    s->q = listone;

    stack *t = malloc(sizeof(stack));
    t->q = malloc(sizeof(list));
    t->q = listtwo;

    //connect the two lists
    s->next = t;                    //not sure if this is correct

    //print linked list of linked lists
    while(s != NULL)
    {
            list *l = s->q;
            while(l != NULL)
            {
                    printf("\n%d", l->p->x);
                    l->p = l->p->next;
            }//end while
            s = s->next;
    }//end while

    return 0;
}

這個小程序的目的是了解鏈表的鏈表的性質,哈哈。 我盡力了,但我很失落。 基本上在第一部分,制作一個從 0 到 9 的列表。 然后是從 10 到 19 的第二個列表。 然后我嘗試連接這兩個列表並打印出最終列表。 如果有人可以提供一些建議來解決這個問題,我將不勝感激。

簡化修改示例

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

typedef struct node {
    int x;
    struct node *next;
}node;

typedef struct {
    node *p;
}list;

typedef struct lol {
    list *q;
    struct lol *next;
} listOfList;

typedef struct range {
    int start;
    int end;//does not contain this value
    //int step;
} range;

list *make_list(void);
listOfList *make_lol(list *aList);
void rangeTolist(list *aList, range aRange);
void printLoL(listOfList *lol);

int main(void) {
    list *listone = make_list();
    list *listtwo = make_list();

    rangeTolist(listone, (range){ 0, 10});//make a linked list from  0 - 10(10 does not include)
    rangeTolist(listtwo, (range){10, 20});//make a linked list from 10 - 20(20 does not include)

    //connect the two lists
    listOfList *two_lists = make_lol(listone);//listOfList *lol=NULL;listAddLastOfLoL(&lol, listone);
    two_lists->next = make_lol(listtwo);      //listAddLastOfLoL(&lol, listtwo);

    //print linked list of linked lists
    printLoL(two_lists);

    //deallocations

    return 0;
}

static inline void *cmalloc(size_t size, const char *func_name){//malloc with check
    void *p = malloc(size);
    if(p == NULL){
        fprintf(stderr, "malloc error in %s.\n", func_name);
        exit(EXIT_FAILURE);
    }
    return p;
}

list *make_list(void){
    list *new_list = cmalloc(sizeof(*new_list), __func__);
    new_list->p = NULL;
    return new_list;
}
node *make_node(int value){
    node *new_node = cmalloc(sizeof(*new_node), __func__);
    new_node->x    = value;
    new_node->next = NULL;
    return new_node;
}
listOfList *make_lol(list *aList){
    listOfList *new_lol = cmalloc(sizeof(*new_lol), __func__);
    new_lol->q    = aList;
    new_lol->next = NULL;
    return new_lol;
}

void rangeTolist(list *aList, range aRange){
    node anchor = { .x = 0, .next = NULL };
    node *current = &anchor;
    int step = (aRange.start < aRange.end) - (aRange.start > aRange.end);

    for(int i = aRange.start; i != aRange.end; i += step){
        node *new_node = make_node(i);
        current = current->next = new_node;
    }
    aList->p = anchor.next;
}
void printLoL(listOfList *lol){
    while(lol){
        if(lol->q){
            for(node *aList = lol->q->p; aList; aList = aList->next)
                printf("\n%d", aList->x);
        }
        lol = lol->next;
    }
}

暫無
暫無

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

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