Reputation:
**I'm trying to understand linked lists and I want to make one that contains 100 nodes containing random numbers.. My codeblocks says that there are no errors but my program crashes when I try to run it. I don't know what I am doing wrong.
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int number;
struct node * next;
}Node;
typedef Node*Nodeptr;
int main() //creating the linked list
{
int i;
Nodeptr head = NULL;
Nodeptr here = head;
Nodeptr newnode = NULL;
head = malloc(sizeof(Node));
if(head == NULL){ //was told build the 1st node out of the loop
return 1;
}
for (i=0; i<100; i++){
newnode->number = rand()%100;
newnode->next = NULL;
here->next = newnode;
here = here->next;
}
printf("%d, %d, %p\n", i, here->number, here->next);
return 0;
}
Upvotes: 0
Views: 6646
Reputation: 84551
Your use of newnode
and head
are a bit reversed. All you need do is allocate newnode
each tine and then either create a new list (e.g. assign head=newnode
or iterate over your list and assign newnode
at the end of list. For example:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct node {
int number;
struct node *next;
} node;
int main (void) {
srand (time (NULL));
node *head = NULL;
for (int i = 0; i < 100; i++) {
node *newnode = malloc (sizeof *newnode);
if (!newnode) {
fprintf (stderr, "error: memory exhausted node '%d'.\n", i);
break;
}
newnode->number = rand() % 100;
newnode->next = NULL;
if (!head) /* new list, assign to head */
head = newnode;
else { /* otherwise iterate and add to end */
node *iter = head;
for (; iter->next; iter = iter->next) {}
iter->next = newnode;
}
}
int j = 0; /* output list (including pointer addresses) */
for (node *iter = head; iter; iter = iter->next)
printf ("node[%3d] : %3d, addr: %p, next: %p\n",
j++, iter->number, (void*)iter, (void*)iter->next);
/* free list memory */
node *victim = NULL; /* node to delete */
for (node *iter = head; iter; iter = iter->next) {
if (victim) /* cannot delete until after loop increment */
free (victim);
victim = iter; /* so save node as victim, delete next iteration */
}
if (victim) /* free last node */
free (victim);
return 0;
}
Note don't forget to free
all memory you allocate.
Example Use/Output
$ ./bin/llmin
node[ 0] : 91, addr: 0x2228010, next: 0x2228030
node[ 1] : 48, addr: 0x2228030, next: 0x2228050
node[ 2] : 18, addr: 0x2228050, next: 0x2228070
node[ 3] : 97, addr: 0x2228070, next: 0x2228090
node[ 4] : 15, addr: 0x2228090, next: 0x22280b0
node[ 5] : 46, addr: 0x22280b0, next: 0x22280d0
node[ 6] : 93, addr: 0x22280d0, next: 0x22280f0
node[ 7] : 22, addr: 0x22280f0, next: 0x2228110
node[ 8] : 17, addr: 0x2228110, next: 0x2228130
node[ 9] : 6, addr: 0x2228130, next: 0x2228150
node[ 10] : 64, addr: 0x2228150, next: 0x2228170
...
node[ 90] : 57, addr: 0x2228b50, next: 0x2228b70
node[ 91] : 64, addr: 0x2228b70, next: 0x2228b90
node[ 92] : 91, addr: 0x2228b90, next: 0x2228bb0
node[ 93] : 66, addr: 0x2228bb0, next: 0x2228bd0
node[ 94] : 13, addr: 0x2228bd0, next: 0x2228bf0
node[ 95] : 89, addr: 0x2228bf0, next: 0x2228c10
node[ 96] : 73, addr: 0x2228c10, next: 0x2228c30
node[ 97] : 42, addr: 0x2228c30, next: 0x2228c50
node[ 98] : 79, addr: 0x2228c50, next: 0x2228c70
node[ 99] : 23, addr: 0x2228c70, next: (nil)
Look it over and let me know if you have any questions.
Upvotes: 0
Reputation: 26647
Creating a linked list with 100 random numbers can be done in the following way.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} node;
node *newNode(int data) {
node *new_node = (node *) malloc(sizeof(node));
new_node->data = data;
new_node->next = NULL;;
return new_node;
}
node *insert_node(node *root, int data) {
if (root == NULL)
return newNode(data);
else {
node *cur;
cur = insert_node(root->next, data);
root->next = cur;
}
return root;
}
void print(node *np) {
if (np) {
printf("(%d)", np->data);
print(np->next);
}
}
int main() {
int T = 100;
node *root = NULL;
while (T-- > 0) {
int r = rand() % 200;
root = insert_node(root, r);
}
print(root);
printf("\n");
return 0;
}
Please try it online.
Upvotes: 1
Reputation: 16540
here is a version of the code that checks for errors, and properly initializes each node
#include <stdio.h> // printf(), NULL
#include <stdlib.h> // malloc()
struct node
{
int number;
struct node * next;
};
int main( void ) //creating the linked list
{
int i;
struct node *head = malloc(sizeof( struct node ));
if(head == NULL)
{ //was told build the 1st node out of the loop
perror( "malloc for for node failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
head->next = NULL;
head->number = rand()%100;
printf("node: %2.2d, %2.2d, %p\n", 0, head->number, head->next);
struct node *here = head;
for (i=1; i<100; i++)
{
here->next = malloc( sizeof( struct node ) );
if( !(here->next) )
{
perror( "malloc failed" );
exit( EXIT_FAILURE );
}
// implied else, malloc successful
here = here->next;
here->number = rand()%100;
printf("node: %2.2d, %2.2d, %p\n", i, here->number, here->next);
}
return 0; // << in modern compilers, this line not needed
// << when returning from 'main()' and value is 0
} // end function: main
and the typical output is:
node: 00, 83, (nil)
node: 01, 86, (nil)
node: 02, 77, (nil)
node: 03, 15, (nil)
node: 04, 93, (nil)
node: 05, 35, (nil)
node: 06, 86, (nil)
node: 07, 92, (nil)
node: 08, 49, (nil)
node: 09, 21, (nil)
node: 10, 62, (nil)
node: 11, 27, (nil)
node: 12, 90, (nil)
node: 13, 59, (nil)
node: 14, 63, (nil)
node: 15, 26, (nil)
node: 16, 40, (nil)
node: 17, 26, (nil)
node: 18, 72, (nil)
node: 19, 36, (nil)
node: 20, 11, (nil)
node: 21, 68, (nil)
node: 22, 67, (nil)
node: 23, 29, (nil)
node: 24, 82, (nil)
node: 25, 30, (nil)
node: 26, 62, (nil)
node: 27, 23, (nil)
node: 28, 67, (nil)
node: 29, 35, (nil)
node: 30, 29, (nil)
node: 31, 02, (nil)
node: 32, 22, (nil)
node: 33, 58, (nil)
node: 34, 69, (nil)
node: 35, 67, (nil)
node: 36, 93, (nil)
node: 37, 56, (nil)
node: 38, 11, (nil)
node: 39, 42, (nil)
node: 40, 29, (nil)
node: 41, 73, (nil)
node: 42, 21, (nil)
node: 43, 19, (nil)
node: 44, 84, (nil)
node: 45, 37, (nil)
node: 46, 98, (nil)
node: 47, 24, (nil)
node: 48, 15, (nil)
node: 49, 70, (nil)
node: 50, 13, (nil)
node: 51, 26, (nil)
node: 52, 91, (nil)
node: 53, 80, (nil)
node: 54, 56, (nil)
node: 55, 73, (nil)
node: 56, 62, (nil)
node: 57, 70, (nil)
node: 58, 96, (nil)
node: 59, 81, (nil)
node: 60, 05, (nil)
node: 61, 25, (nil)
node: 62, 84, (nil)
node: 63, 27, (nil)
node: 64, 36, (nil)
node: 65, 05, (nil)
node: 66, 46, (nil)
node: 67, 29, (nil)
node: 68, 13, (nil)
node: 69, 57, (nil)
node: 70, 24, (nil)
node: 71, 95, (nil)
node: 72, 82, (nil)
node: 73, 45, (nil)
node: 74, 14, (nil)
node: 75, 67, (nil)
node: 76, 34, (nil)
node: 77, 64, (nil)
node: 78, 43, (nil)
node: 79, 50, (nil)
node: 80, 87, (nil)
node: 81, 08, (nil)
node: 82, 76, (nil)
node: 83, 78, (nil)
node: 84, 88, (nil)
node: 85, 84, (nil)
node: 86, 03, (nil)
node: 87, 51, (nil)
node: 88, 54, (nil)
node: 89, 99, (nil)
node: 90, 32, (nil)
node: 91, 60, (nil)
node: 92, 76, (nil)
node: 93, 68, (nil)
node: 94, 39, (nil)
node: 95, 12, (nil)
node: 96, 26, (nil)
node: 97, 86, (nil)
node: 98, 94, (nil)
node: 99, 39, (nil)
so, perhaps you would like to display the address of the current node rather than the contents of the 'next' field.
then the printf()
statements would be:
printf("node: %2.2d, %2.2d, %p\n", 0, head->number, head);
and
printf("node: %2.2d, %2.2d, %p\n", i, here->number, here);
Upvotes: 1