Thread: I have a problem in linked list code

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    9

    I have a problem in linked list code

    It doesn't link the list here is the code I don't know how to fix it !
    Code:
    #include <iostream>
    #include <conio.h>
    #define UINT unsigned int 
    
    
    using namespace std;
    struct SCPU
    {
    	char sCommand[256];
    	UINT uPriority;
    	SCPU *pNext;
    
    
    };
    SCPU* AddInstruction(char *sCommand,UINT uPriority)
    {
    	int	iCounter;
    	SCPU *pCurrNode;
    
    
    	pCurrNode = new SCPU;
    
    
    	for(iCounter = 0;sCommand[iCounter];iCounter++)
    		pCurrNode->sCommand[iCounter] = sCommand[iCounter];
    
    
    	pCurrNode->sCommand[iCounter] = 0;
    	pCurrNode->uPriority = uPriority;
    	pCurrNode->pNext     = NULL;
    	return pCurrNode;
    }
    
    
    void insert(SCPU *pointer, int data)
    {
    	/* Iterate through the list till we encounter the last node.*/
    	while(pointer->pNext!=NULL)
    	{
    		pointer = pointer -> pNext;
    	}
    	/* Allocate memory for the new node and put data in it.*/
    	pointer->pNext = (SCPU *)malloc(sizeof(SCPU));
    	pointer = pointer->pNext;
    	pointer->uPriority = data;
    	pointer->pNext = NULL;
    }
    
    
    int main(void)
    {
    	char SName[] = "abcde123";
    	SCPU	*pHead = new SCPU;
    	SCPU	*pTemp = NULL,*pTemp2;
    
    
    
    
    	pHead->pNext = NULL;
    	
    	for(int i = 0; i < 10;i++)
    	{
    		
    		pTemp = AddInstruction("hello",i);
    		if(i == 0)
    			pHead->pNext = pTemp;
    		pTemp = pTemp->pNext;
    	}
    
    
    	while(pTemp){
    		cout << "name is " << pTemp->sCommand << " and priority is " << pTemp->uPriority;
    		pTemp = pTemp->pNext;
    		_getch();
    	}
    	_getch();
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    I acctually fixed it but I was wondering is their a better solution to this?
    Code:
    #include <iostream>
    #include <conio.h>
    #define UINT unsigned int 
    
    
    using namespace std;
    struct SCPU
    {
    	char sCommand[256];
    	UINT uPriority;
    	SCPU *pNext;
    
    
    };
    SCPU* AddInstruction(char *sCommand,UINT uPriority)
    {
    	int	iCounter;
    	SCPU *pCurrNode;
    
    
    	pCurrNode = new SCPU;
    
    
    	for(iCounter = 0;sCommand[iCounter];iCounter++)
    		pCurrNode->sCommand[iCounter] = sCommand[iCounter];
    
    
    	pCurrNode->sCommand[iCounter] = 0;
    	pCurrNode->uPriority = uPriority;
    	pCurrNode->pNext     = NULL;
    	return pCurrNode;
    }
    
    
    void insert(SCPU *pointer, int data)
    {
    	/* Iterate through the list till we encounter the last node.*/
    	while(pointer->pNext!=NULL)
    	{
    		pointer = pointer -> pNext;
    	}
    	/* Allocate memory for the new node and put data in it.*/
    	pointer->pNext = (SCPU *)malloc(sizeof(SCPU));
    	pointer = pointer->pNext;
    	pointer->uPriority = data;
    	pointer->pNext = NULL;
    }
    
    
    int main(void)
    {
    	char SName[] = "abcde123";
    	SCPU	*pHead = new SCPU;
    	SCPU	*pTemp = NULL,*pTemp2;
    
    
    
    
    	pHead->pNext = NULL;
    	
    	for(int i = 0; i < 10;i++)
    	{
    		
    		pTemp = AddInstruction("hello",i);
    		if(i == 0)
    			pHead->pNext = pTemp;
    		else
    		{
    			pTemp2 = pHead->pNext;
    			while(pTemp2->pNext)
    				pTemp2 = pTemp2->pNext;
    			pTemp2->pNext = pTemp;
    		}
    		pTemp = pTemp->pNext;
    	}
    	pTemp = pHead->pNext;
    
    
    	while(pTemp){
    		cout << "name is " << pTemp->sCommand << " and priority is " << pTemp->uPriority;
    		pTemp = pTemp->pNext;
    		_getch();
    	}
    	_getch();
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    You could use a structure or class that includes a head pointer to the first node of a list, and a tail pointer to the last node of a list, which would eliminate having to scan through the list to find the last node. Comment - the function named insert would be better named as append (or push_back), since it appends to the end of a list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. Linked list - why this bit of code?
    By chris1985 in forum C Programming
    Replies: 2
    Last Post: 10-04-2005, 06:17 AM
  3. my linked list code - comments please
    By cdave in forum C Programming
    Replies: 3
    Last Post: 04-28-2005, 12:37 AM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. What's wrong with this linked-list code?
    By helplz in forum C Programming
    Replies: 4
    Last Post: 06-15-2003, 04:22 PM