1 Replies - 5224 Views - Last Post: 03 March 2010 - 04:29 PM Rate Topic: -----

#1 apartbyhorses  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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;
	struct nodeTag *link;
}  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;
	pNew->link = NULL;

	if (pPrev == NULL)  {
		/*  insert before first node or to empty list  */
		printf("Inserting first node:  value = %d, ptr = %p\n", pNew->number, pNew);		
		pNew->link = pList;
		pList = pNew;
	}
	else  {
		/*  insert into middle or end of list  */
		printf("Inserting node:  value = %d, ptr = %p\n", pNew->number, pNew);		
		pNew->link = pPrev->link;
		pPrev->link = 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);		
		pList = pCur->link;
	}
	else  {
		/*  delete from middle or end of list  */
		printf("Deleting node:  value = %d, ptr = %p\n", pCur->number, pCur);		

		pPre->link = pCur->link;
	}

	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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • 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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1