| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Linked List Error....
I've tried making a simple implementation of a linked list in C. I havent posted the whole code here, only the part relevant to my query.
Here is the routine to add a node in the beginning of a list. struct Node *head,*tail; //addInFront void addInFront(char *name) { //1. Making a pointer and allocating memory. //******************************** struct Node *newNode; newNode = (Node *)malloc(sizeof(Node)); newNode->name = name; newNode->next = NULL; //******************************** //OR //2. Making an instance of the struct. //******************************** struct Node newNode; newNode.name = name; newNode.next = NULL; //******************************** if ( isEmpty() ) { head = newNode; tail = newNode; } else { newNode->next = head; head = newNode; } } Now when i use 1. (i.e. Making a pointer and allocating memory) code seems to work fine ahead. However, when i use 2. (i.e. Making an instance of the struct) there seems to be a memory alocation error (or some runtime error) when i try to access the tail pointer in any other method. Personally i dont see any difference between 1 and 2. In the first case we're creating a pointer and then allocating memory whereas in the second case we're simple creating an instance of the struct so memory is already allocated. Are they both doing the same thing? Does anyone have any idea why the program crashes if i use no. 2 and try to access the tail pointer after adding the node? Thanks Neville (PS: I can post the entire code if someone needs to have a look. Also. i have NO problems with the logic of adding a node. All that is working fine.) |
|
#2
|
|||
|
|||
|
entire code please
|
|
#3
|
|||
|
|||
|
Linked List Code....
Hello,
Please have a look at code 1 and code 2. Code 1 gives an error. Code 2 works fine. Could someone explain me why? Note, the 'major' difference lies in the addInFront(). // Code 1 // ********************** // This code gives a run time error // when we call the printHeadTail() // ********************** #include <stdio.h> #include <malloc.h> struct Node { char *name; struct Node *next; }; struct Node *head; struct Node *tail; int isEmpty() { if ( head == NULL ) return 1; else return 0; } //addInFront void addInFront(char *name) { struct Node newNode; newNode.name= name; newNode.next = NULL; if ( isEmpty() ) { head = &newNode; tail = &newNode; } else { newNode.next = head; head = &newNode; } } void printHeadTail() { printf("Head: %s, Tail: %s\n",head->name,tail->name); } void main() { addInFront("Neville"); addInFront("Shahlbh"); addInFront("Swapnil"); printHeadTail(); } // Code 2 // ********************** // This code runs just fine // The difference is in the addInFront() // ********************** #include <stdio.h> #include <malloc.h> struct Node { char *name; struct Node *next; }; struct Node *head; struct Node *tail; int isEmpty() { if ( head == NULL ) return 1; else return 0; } //addInFront void addInFront(char *name) { struct Node *newNode; newNode = (Node *)malloc(sizeof(Node)); newNode->name = name; newNode->next = NULL; if ( isEmpty() ) { head = newNode; tail = newNode; } else { newNode->next = head; head = newNode; } } void printHeadTail() { printf("Head: %s, Tail: %s\n",head->name,tail->name); } void main() { addInFront("Neville"); addInFront("Shahlbh"); addInFront("Swapnil"); printHeadTail(); } Thanks Neville |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Linked List Error.... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|