# Inserting random numbers into a linked list in a random position

Page 1 of 1

## 1 Replies - 9638 Views - Last Post: 03 March 2010 - 04:29 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=159622&amp;s=b165bc11c281df0253875555d8995ad4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 apartbyhorses

Reputation: 1
• Posts: 4
• Joined: 28-October 09

# Inserting random numbers into a linked list in a random position

Posted 03 March 2010 - 03:28 PM

I've been stuck on it for a while now. Basically I need to insert a random number into a random position on a linked list and then delete them. Here's what I have so far:

```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX  30

typedef struct nodeTag  {
int number;
}  NODE;

NODE *insert_node(NODE *list_head, NODE *list_prev, int data, int index);
NODE *delete_node(NODE *list_head, NODE *list_prev, NODE *to_delete);

NODE *pPre[MAX];

int main (void)  {
int i;
int store;
int newlocation;
NODE *pList;

srand(time(NULL));

pList = NULL;
pPre[0] = NULL;

for (i = 1; i < 11; i++)  {
store = rand()%100 + 100;
newlocation = rand()% i;
pList = insert_node(pList, pPre[newlocation - 1],store, newlocation);
}

printf("\nCompleted insertions --- \n\n");

for (i = 0; i < MAX; i++)  {
pList = delete_node(pList, pPre[MAX-1-i], pPre[MAX - i]);
}
printf("\nCompleted deletions --- \n\n");

return 0;

}  /* end main  */

NODE *insert_node(NODE *pList, NODE *pPrev, int num, int index)  {

NODE *pNew;
int i;

if (!(pNew = (NODE *)malloc(sizeof(NODE))))
printf("\a Memory overlfow in insert!!!\n"), exit (100);

pNew->number = num;

if (pPrev == NULL)  {
/*  insert before first node or to empty list  */
printf("Inserting first node:  value = %d, ptr = %p\n", pNew->number, pNew);
pList = pNew;
}
else  {
/*  insert into middle or end of list  */
printf("Inserting node:  value = %d, ptr = %p\n", pNew->number, pNew);
}
pPre[num + 1] = pNew;

return pList;
}  /*  end insert_node  */

NODE *delete_node (NODE *pList, NODE *pPre, NODE *pCur)  {

if (pPre == NULL)  {
/*  delete first node of list  */
printf("Deleting first node:  value = %d, ptr = %p\n", pList->number, pList);
}
else  {
/*  delete from middle or end of list  */
printf("Deleting node:  value = %d, ptr = %p\n", pCur->number, pCur);

}

free(pCur);
return pList;
}  /*  end delete_node  */
```

The program currently crashes when it calls the delete function. Any help would be greatly appreciated. Thank you!

Is This A Good Question/Topic? 0

## Replies To: Inserting random numbers into a linked list in a random position

### #2 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Inserting random numbers into a linked list in a random position

Posted 03 March 2010 - 04:29 PM

I don't see any calls to delete_node in the code you posted, so we don't know how you are calling it. I see nothing in delete_node that checks to see that pCur is not NULL so my guess is that you may be giving it a pCur node that has no memory allocated to it and then you are freeing memory that doesn't exist (but that's just a guess).

I don't understand your use of the global array pPre. Why do you put pointers to preceding nodes in a global array, rather than have a pPre pointer as a member of each NODE?

Finally, am I mistaken or are you inserting nodes into random positions in a list that doesn't yet exist? How can you, for example, create a node to be the 5th node in a list that contains only 1 node? Aren't you creating multiple "first" nodes?