Thread: Linked lists questions

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    so i would insert the first number in the head outside of the insertion function. Then in the insertion function it would take that head in, then add more numbers to the list until 0 is entered. is this the order?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Emeighty View Post
    so i would insert the first number in the head outside of the insertion function. Then in the insertion function it would take that head in, then add more numbers to the list until 0 is entered. is this the order?
    No, you would have a loop in your MAIN, that calls your insert function once for each number entered, and the loop ends when 0 is entered.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    ok, i think i get that. for the sort, how would i get it repeat itself ?


    Code:
    do 
    {
    cout << "Please insert the first number : " ;
    cin >> number ;
    insertion(HeadPtr, number);
     
    } while (number!=0);

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that looks good.

    As to sorting, you need an outer loop and an inner loop. In the outer loop, you just set up the pointer to the start of the list. In the inner loop, you swap items that are out of order and move to the next item. The inner loop ends when you have gone through the list completely. The outer loop ends when you haven't swapped anything [which means that all items are in order].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    I know this is getting repetitive. Pretty ridiculous, i need a Live breathing teacher, If anyone is out there PLEASE pm me. I am willing to pay you for your time. Me paying you will save me money actually. I am not sure on the rules of the website about asking for help, im very desperateactually. have 2 assignments and a final in about 3 weeks. and still dont see everything the book is trying to teach, and the online teacher doesnt clarify.

    anyway,

    My program is falling apart. I'm talking very bad. i scrapped the whole thing because i had to change the insertion.
    The goal of what i am looking to do is one thing, then i can move on to something else .
    Insert a number in if the HeadPtr is empty or NULL.

    I've tried
    if (HeadPtr==NULL)
    {
    do something;
    }

    and

    if (HeadPtr==0)
    {
    do something;
    }

    what is it that is needed to get to the inside of the if statement?

    this is the whole program i am starting over with.

    Code:
    #include <iostream>
    using namespace std;
    
    /* Recursive functions
    insertion
    retrieval
    pointer based sorted linked list of integers
    also if 0 is entered it keeps inserting until 0 is entered.
    */
    
    
    struct node
    {
       int item;
       node *next;
    };
    
    
    // takes in the head pointer 
    // item to be inserted
    //building the list here
    
    void insertion(node *HeadPtr, int NumberToInsert) 
    {
    
    if (HeadPtr=NULL) //If the HeadPtr is empty insert the number
    {
    
    HeadPtr->item=NumberToInsert;
    cout << "The HeadPtr->item is : " << HeadPtr->item << endl;
    } //end if
    
    }
    
    void traverser (node *HeadPtr) {
         cout << "Using Traverser the HeadPtr->item is :" << HeadPtr->item << endl;
    }
    
    int main() {
    int number;
    node *HeadPtr=new node;
    HeadPtr==NULL;
    
    while (number!=0) 
    {
    cout << "Please insert the first number: "; 
    cin >> number;
    }
    insertion( HeadPtr, number);
    //traverser(HeadPtr);
       system("pause");
        return 0;
    }

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you don't learn to indent your code, as you've already been asked to, then I wont be reading it, and neither will a lot of other people.

    It's like closing your eyes and saying "I can't see the bug!".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    80

    clueless

    Since i am not hearing or understanding anything, I am going to start from scratch. I am aware there are pretty talented people on this site. If it takes me making myself look very ignorant to learn something, so be it. I don't now what questions to aks and how to ask them, so i will a question worded a few times.
    So is the book grammer different than actually grammer used?

    indent....is it?
    Code:
         
    
    function()
    {        something;
    }
    
    or 
    
              function() {
           
             something;
    }
    
    
    or is it 
    
    
      function () 
    {
                something
    
    }
    Last edited by Emeighty; 09-09-2008 at 01:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  2. Question On Linked Lists
    By Zildjian in forum C Programming
    Replies: 8
    Last Post: 10-23-2003, 11:57 AM
  3. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. Linked Lists -- Again!!! Help!!
    By Leeman_s in forum C++ Programming
    Replies: 4
    Last Post: 01-22-2002, 08:27 PM