$ 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;
}
这个小程序的目的是了解链接列表的链接列表的性质lol。我尽我所能,但我迷失了。基本上在第一部分中,制作一个从0到9开始的列表。然后第二个列表从10-19开始计算。然后我尝试连接两个列表并打印出最终列表。如果有人能提出一些建议来解决这个问题,我会非常感激。
答案 0 :(得分:0)
简化修改示例
#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;
}
}