Thread: Linked lists questions

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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;
    }

  3. #3
    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"

  4. #4
    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.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I write like this:
    Code:
    int function(paramters)
    {
        do something;
        if (stuff)
        {
             do others
        }
        else if (x)
        {
            switch(y)
            {
               case 'a':
                  Some code here
                  break;
               default:
                  break;
        }
        else
        {
            something else. 
        }
    }
    However, the most important part is that each new level is "deeper" than the previous level, and that the code is consistent. Most editors have a "auto-indent" feature that can be used to format your code (and that will maintain correct format for you, if you like - I use Emacs, and it's near on impossible to get it wrong, but you probably want to set "stroustrup" style instead of the default).

    --
    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.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    ok, I found the indent feature on the Dev-CPP compiler and the "find matching braces " feature and enabled it.

    For the program, I am trying to fill the HeadPtr only if it is empty.
    My questions are.
    1. Is a new node an empty node?
    2. Is a node pointing to NULL an empty node?
    3. If 1, or 2 arent empty nodes what is an empty node?


    My program below does not output what is in the if statement. The goal is to fill it only if the HeadPtr is empty.

    Code:
    #include <iostream>
    using namespace std;
    
    struct node
    {
       int item;
       node *next;
    };
    
    void insertion( node *&HeadPtr, int NumberToInsert)
    {    
         if (HeadPtr == new node)
         {
    	  	cout << "Inside first if " << endl;
         } //end if
    		
    }// end insertion
    
    int main() 
    {   
        int number= -1;
        node *HeadPtr=new node;
       
        insertion (HeadPtr, number);
        system("pause");
        return 0;
    } //end main

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if (HeadPtr == new node)
    This is almost certain NEVER be true - only if you recently did "delete HeadPtr" and you didn't set HeadPtr to 0 as well as being "lucky" enough to get the same address that you just deleted.

    Your check should be:
    Code:
    if (HeadPtr == 0)
    ...
    --
    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.

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