Thread: copy array to linked list

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    copy array to linked list

    why this program return null nodes?? thanks

    Code:
    void CopyStudRecordsFromArrayToLinkedList(StudNodePtr *ppList, STUDENT A[],int total)
    {	//Pseudocode:
    	//   Declare a "lastnode" pointer, initialized as NULL
    	//   For each record in the array
    	//      Create a new node and setup the node content
    	//      If this is the first node, update *ppList
    	//      Otherwise append it to the end of the linked list (ie. after "lastnode")
    	//      Update "lastnode" to point to the new node 
    
    	//Your task: Finish this function
    	StudNodePtr p, pLast, pRtn=NULL;
    	int i;
    	for (i=0;i<total;i++)
    	{	if (p==NULL)
    		{	printf("Cannot create new node\n");
    			exit(-1);
    		}
    
    		p = (StudNodePtr)malloc(total*sizeof(struct StudNode));
    		p->data=A[i];
    		p->next=NULL;
    		if (pRtn==NULL)
    		{	pRtn = p;
    			pLast = p;
    		}
    		else
    		{	pLast->next=p;
    			pLast = p;
    		}
    	}
    
    void main()
    {	
    	#define TOTAL 10
    
    	STUDENT A[TOTAL];
    	int i;
    	StudNodePtr pList=NULL;
    	
    	//Setup the data
    	for (i=0;i<TOTAL;i++)
    	{	A[i].StudentID=50000000+i;
    		A[i].ProgramCode=4000+i%3;
    	}
    
    		CopyStudRecordsFromArrayToLinkedList(&pList, A, TOTAL);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    // Declare a "lastnode" pointer, initialized as NULL
    Well for starters, you never have actually done what this line asks you to.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    After I declare pLast = NULL, it still return 0 nodes, thanks

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    p = (StudNodePtr)malloc(total*sizeof(struct StudNode));
    well, what is this? u are expected to create a node of size struct node not 10 * struct studnode. where u are just wasting a hell lot of memory. and with your programe the links are not proprer. and few pointer of type studNote which are not pointing any where.

    its better u go through your teacher material or any good book which guides u from basic of linked list

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing an array to linked list
    By bar338 in forum C Programming
    Replies: 7
    Last Post: 04-08-2009, 09:15 PM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM