gpt4 book ai didi

将列表连接成列表并在c中打印列表的列表(链表的链表)

转载 作者:行者123 更新时间:2023-11-30 19:39:10 25 4
gpt4 key购买 nike

$ 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;
}
}

关于将列表连接成列表并在c中打印列表的列表(链表的链表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36829525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com