Thread: crashing linked lists

  1. #1
    Registered User
    Join Date
    Nov 2010

    crashing linked lists

    hi everyone.

    im trying to compile a program that creates a 2d linked list where each node has its own value. kinda like a matrix

    but after i put in say two values in a 2*2 structure, the program crashes.

    please tell me of any errors that i might have made and if there is something wrong with the program. i talked to my teacher and he told me there is no need for using insert function or anything like sure the problem is in the loops, but dont know what exactly is wrong.

    and the structure im creating is a square/rectangle and the nodes according to my code should be created one after another in two directions

    thanks in advance.

    #include <stdio.h>
    #include <stdlib.h>
    struct record{
           int value1;
           struct record *next;
           struct record *back;
           struct record *up;
           struct record *down;
    typedef struct record node;
           node *start, *curr, *prev,*pt1,*pt2;
           int i,j,rows,cols;
           printf("\nWelcome to the 2d linked list generator\n");
           printf("Please enter the no. of rows you want:\n");
           printf("Please enter the no. of columns you want:\n");
           start = (node *)malloc(sizeof(node));   /*created the start node*/
           curr = start;
           prev = start;                           /*used as reference*/
           printf("Please type in the value for field:\n");
           for(i=1;i<rows;i++){       /*initialisation of outer  loop*/
                               printf("Please type in the value for col no. %d\n",i);
                               curr->next=(node *)malloc(sizeof(node));
                               curr->back=prev;                 /*creates double link list connected to start*/
                                                   prev->down=(node *)malloc(sizeof(node));
                                                   printf("Please enter value\n");
                                                   pt1->next=pt2;            /*sideways link*/

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > i talked to my teacher and he told me there is no need for using insert function or anything like that
    Yeah, which is why you now have a bloated main() trying to do everything.

    Where does next (for example) point, when the node is on the right hand edge of the grid?
    Is it NULL, or does it point to the first node on the next row?

    Do you have some nice sketches on paper showing the data structure, with pointers between nodes labelled with your variable names.
    Have you modelled on paper what actually happens when you add a node say.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    You didn't happen to have answered any of the questions I put to you in this thread you made on the same topic, did you?

    Also: Your loop is bad. If you want 2 rows, and you enter rows as 2, and you start your loop at 1, what happens at the end of the first loop cycle?

    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists?!
    By hwing91 in forum C Programming
    Replies: 32
    Last Post: 11-08-2010, 06:13 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM