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