2016-04-24 219 views
-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(链接列表的链表中)中列出列表

+0

1)'INT计数器= 0;'...'而(计数器<0)':这个while循环不执行。还需要更新'计数器'。 – BLUEPIXY

+1

'sizeof(listone)'需要'sizeof * listone'或'sizeof list'。并且请更具体地描述你的程序的问题是什么。最后,您是否尝试过使用调试器和/或调试打印语句进行基本调试,以跟踪程序的执行?人们希望在问之前进行基本的调试。 – kaylum

+1

通过将'node'和'list'分开的方式没什么意义,你会过于复杂化。一般来说,如果你要分离列表和数据结构,你会得到类似于'typedef struct list {void * data; struct list * next; };'那么你的'data'可以是任何结构。使用单个'node * p'来创建'list'并不是真的有帮助。 SO上有很多很好的链接列表示例,只要搜索就行。 –

回答

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; 
    } 
} 
+0

谢谢。这帮助我理解了什么是错误的。问题在于链接两个列表,现在一切都很好。我只需要弄清楚如何释放这个该死的东西。干杯 –