Adding To The Middle Of A Linked List

This is a discussion on Adding To The Middle Of A Linked List within the C++ Programming forums, part of the General Programming Boards category; Hi I have a singly linked list and know how to add to the end of it but am unsure ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    23

    Adding To The Middle Of A Linked List

    Hi I have a singly linked list and know how to add to the end of it but am unsure of how to add it to the linked list so that a node is placed inbetween nodes of similar CustomerNumber, I even have the locate node function to find a node witha similar customer number, im just unsure how to modify the add function to place it it either before or after that node.. If there are any simple modifications that i could make, it would be much appreciated

    Code:
    class Orders
    {
    	public:
    		long CustomerNumber;
    		string PartCode;
    		string PaymentType;
    		string CCType;
    		string CCNumber;
    		string NameOnCard;			
    		string ExpiryDate;
    		bool CompletionStatus;
    		long OrderNumber;
    	
    		Orders* next;
    		Orders(long, string, string, string, string, string, string, bool, long, Orders*);
    		~Orders();
    	private:
    };
    
    Orders::Orders(long tempCustomerNumber, string tempPartCode, string tempPaymentType, 
    				 string tempCCType, string tempCCNumber, string tempNameOnCard, 
    				 string tempExpiryDate, bool tempCompletionStatus, long tempOrderNumber,
    				 Orders* tempNext):CustomerNumber(tempCustomerNumber), PartCode(tempPartCode), 
    				 PaymentType(tempPaymentType), CCType(tempCCType), CCNumber(tempCCNumber), 
    				 NameOnCard(tempNameOnCard), ExpiryDate(tempExpiryDate), 
    				 CompletionStatus(tempCompletionStatus), OrderNumber(tempOrderNumber),
    				 next(tempNext){}
    
    Orders::~Orders(){}
    
    typedef Orders* OrdersPtr;
    OrdersPtr locateNodeCutomerNumber(OrdersPtr temp, string eCustomerNumber);
    
    //finds a node witha  matching customernumber
    OrdersPtr locateNodeCutomerNumber(OrdersPtr temp, string eCustomerNumber);
    {
    	while (temp != NULL)
    	{
    		if (temp->CustomerNumber== eCustomerNumber)
    		{
    			return temp;
    		}
    		temp = temp->next;
    	}
    	return NULL;
    }
    
    void fnAddOrder(OrdersPtr &root3) //root 3 is the last item in the linked list
    {
    	string ePartCode, ePaymentType, eCCType, eCCNumber, eNameOnCard, eExpiryDate; 
    	long eCustomerNumber, eOrderNumber;
    	bool eCompletionStatus;
    
    	cin.ignore(500,'\n');
    	cout << "Customer Number:\t\t\t";
    	getline(cin, eCustomerNumber, '\n');
    
    	cout << "PartCode:\t\t\t";
    	getline(cin, ePartCode, '\n');
    
    	cout << "Payment Type:\t\t\t";
    	getline(cin,ePaymentType, '\n');
    
    	cout << "Credit Card Type:\t\t\t";
    	getline(cin,eCCType, '\n');
    
    	cout << "Credit Card Number:\t\t\t";
    	getline(cin, eCCNumber, '\n');
    
    	cout << "Name on Credit Card:\t\t\t";
    	getline(cin, eNameOnCard, '\n');
    
    	cout << "Would you like to Complete This Order Now (1-yes, 0 no):\t\t\t";
    	getline(cin, eCompletionStatus, '\n');
    
    	cout << "Order Number:\t\t\t";
    	getline(cin, eOrderNumber, '\n');
    
    	root3 = new Orders(eCustomerNumber, ePartCode, ePaymentType, eCCType, eCCNumber, eNameOnCard, eExpiryDate, eCompletionStatus, eOrderNumber, root3);
    
    	system("cls");
    	
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    To insert a node in a singly linked list, just walk down the list until you find the point you want to add at and insert it, like this:
    Code:
    node *insert ( node *list, int val )
    {
      node *new_node = new node ( val, 0 );
    
      // Empty list
      if ( list == NULL )
        return new_node;
      // Add at the front
      else if ( list->data > val ) {
        new_node->next = list;
        list = new_node;
      }
      // Everything else
      else {
        node *walk;
    
        for ( walk = list; walk->next != NULL; walk = walk->next ) {
          if ( walk->next->data > val )
            break;
        }
    
        new_node->next = walk->next;
        walk->next = new_node;
      }
    
      return list;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 09:38 PM
  2. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 01:13 PM
  3. Adding nodes to a linked list
    By bluescreen in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 01:59 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 06:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21