-1
$ 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算起第二个列表。然后我尝试连接两个列表并打印出最终列表。如果有人可以提供一些建议来解决这个问题,我将非常感激。将列表连接到一个列表中,并在c(链接列表的链表中)中列出列表
1)'INT计数器= 0;'...'而(计数器<0)':这个while循环不执行。还需要更新'计数器'。 – BLUEPIXY
'sizeof(listone)'需要'sizeof * listone'或'sizeof list'。并且请更具体地描述你的程序的问题是什么。最后,您是否尝试过使用调试器和/或调试打印语句进行基本调试,以跟踪程序的执行?人们希望在问之前进行基本的调试。 – kaylum
通过将'node'和'list'分开的方式没什么意义,你会过于复杂化。一般来说,如果你要分离列表和数据结构,你会得到类似于'typedef struct list {void * data; struct list * next; };'那么你的'data'可以是任何结构。使用单个'node * p'来创建'list'并不是真的有帮助。 SO上有很多很好的链接列表示例,只要搜索就行。 –