crashing linked lists

This is a discussion on crashing linked lists within the C Programming forums, part of the General Programming Boards category; hi everyone. im trying to compile a program that creates a 2d linked list where each node has its own ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    11

    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 that..im 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.

    Code:
    #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;
    
    main(){
           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");
           scanf("%d",&rows);
           
           printf("Please enter the no. of columns you want:\n");
           scanf("%d",&cols);
           
           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");
           scanf("%d",&curr->value1);
           curr->back=NULL;
           curr->up=NULL;
           
           
           
           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));
                               prev=curr;
                               curr=curr->next;
                               curr->back=prev;                 /*creates double link list connected to start*/
                               
                               prev=pt1;
                               
                               for(j=1;j<cols;j++){
                                                   
                                                   prev->down=(node *)malloc(sizeof(node));
                                                   curr=prev;
                                                   curr=curr->down;
                                                   curr->up=prev;
                                                   pt2=curr;
                                                   
                                                   printf("Please enter value\n");
                                                   scanf("%d",&curr->value1);
                                                   
                                                   pt1=pt1->back;
                                                   pt1=pt1->down;
                                                   
                                                   pt1->next=pt2;            /*sideways link*/
                                                   pt2->back=pt1;
                                                   
                                                   }
                                                  
                               
                               
                               }
                               
    
      
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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?


    Quzah.
    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, 05: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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21