-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上有很多很好的鏈接列表示例,只要搜索就行。 –