Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

A bit of C++ help

Options
  • 05-05-2009 4:31pm
    #1
    Registered Users Posts: 929 ✭✭✭


    Im writing a small program and am having a problem with a section of it. I know exactly what i want it to do but dont know how to go about doing it.
    Basically I have to create a method of a class as such:

    cLinkedList::addAt(int n, int value)

    this is meant to add a new node with any value at the location n in a linked list. If n is invalid, I have to return an error.

    What i want to do is move the end elements to the beginning of the list until it reaches the location i need. Once it reaches it, put in the value i have entered, then put them back. I will keep trying but if anyone has any ideas please let me know :)

    mHead is the head name i have used, and mTail is the tail!


Comments

  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    basically in a nut shell loop through the list, until the tail pointer is equal to null.

    have a read of this explains it in quite some detail

    <Snipped link as it pretty much gives you the code>

    ok i'll sum up what you got to do

    in the addAt function

    create two temporary nodes.
    store data that's part of the node in the first temporary node (temp) ie. temp->data = value or whatever.
    you need to check that you aren't at the starting node if you dont have a start node then make temp equal to this.
    otherwise make the other temporary node (temp2) equal to the start node.
    loop through the nodes checking the next pointer, if that is not null, you move to the next node
    otherwise, make the next pointer of the temp2 node equal to the temp node.


  • Registered Users Posts: 929 ✭✭✭sternn


    thanks! going to give that a try now :)


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    just re-read your post again.

    you'll need to put a condition in your loop of the node to see if n is equivalent to 0 and decrement it inside the loop, that way you wont loop through the whole list when you've found where you wish to place the new node.


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    Would it not be easier to have an integer (pos) keeping track of the position and loop through the list until pos == n-1 then just get the current node to point to the new node and get the new node to point to the current nodes next element? Is it a single or double/multi linked list? For the code below I've made a few assumptions:
    1. addAt returns nothing
    2. cLinkedList object has a pointer to the next element called next
    3. the list is a single linked list
    4. there exists a node constructor (which I'm calling cListNode) which takes in an integer value and points the new object to null
    5. the cLinkedList object keeps track of the current node
    6. the cLinkedList object has a means of returning to the head of the list - which I'm calling goToHead()
    7. the head node contains no data

    -RD
    
    void cLinkedList::addAt(int n, int value) {
     cListNode* temp = new cListNode(value);
    
     cListNode* curNode = new cListNode();
     int i = 0;
     goToHead();
     curNode = head->next;
     if(curNode != NULL) {
     (while i < (n-1) && curNode != NULL) {
      curNode = curNode->next;
      i++;
     }
     temp->next = curNode->next;
     curNode->next = temp;
     }
    }
    
    

    I'm sure there will be disagreements but I think this is probably the simplest solution. You will need to adapt it to make it compatible with the code you already have written.

    Regards
    RD


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    i just assumed it was a college assignment/exam question as he/she has been given the function prototype to work with.


  • Advertisement
  • Closed Accounts Posts: 7,346 ✭✭✭Rev Hellfire


    Just use the stl, there's no need to be writing stuff like lists.


  • Users Awaiting Email Confirmation Posts: 351 ✭✭ron_darrell


    I'd have to disagree there Rev. When learning, it can be a very worthwhile exercise to build some of these STL objects from scratch yourself to understand the theory behind them.


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    exactly building the likes of a list from scratch teaches structures and pointers not to mention the mechanics behind these primitive data structures.

    it's all well and good using STL stuff when you aren't learning.


Advertisement