Thread: HelpMePlease

  1. #1
    Prncess100
    Guest

    HelpMePlease

    This is homework assignment ...how do i get started

    A linked list has been created in such a way that the values stored in the linked list are stored in sorted order. The head points to a node containing the smallest value. Write a function called INSERT, which accepts 2 arguments: a linked list and an integer value. The function creates a node , inserts the integer value in it and inserts the node in the linked list without disturbing the order. If the list is empty, the node is placed as first node.
    Fill in the missing code:

    void Insert( LinkedList &L, int Value)
    {




    }

    main{
    struct node
    {

  2. #2
    Registered User Pioneer's Avatar
    Join Date
    Dec 2002
    Posts
    59
    Walk down the list until you find a Value greater than the one you have, add the new Value before that.
    Real respect comes from those that have the knowledge to understand what you've done and the experience to appreciate it. It's the Crack Cocaine of programming.

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Angry

    "This is homework assignment ...how do i get started...Fill in the missing code:"

    I can't believe that you thought that you could get other people to write your code for you or do your homework. If you didn't want to write code, why'd you take the class? No one here is going to write your code for you, and if you say that this is for your homework others are much less likely to help you.

    If you need help with something you should first search the internet. Then, search this site to make sure that your question hasn't already been answered. After that, if you still need help, post the code you have so far, and ask a specific question. Never, never, never post without any code or ask others to write your code for you. That's not what this board is about.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If you still want help with your homework, go here: http://www.WeDoYourHomework.com
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Here's one way to do it. Picture the list like this:

    Code:
    head
    address
    2
    ---------->address
                       41
                       -------------->address
                                              173    
                                              ------------>NULL
    where each address is the address of a node in the list, the integer is the data member of the node, and an arrow is the pointer to the address of the next node in the list. then say you want to insert the value 53 into the list.

    First you create a new node to hold 53.

    Then use a node pointer called current and use it to search the list for a value which is greater than 53, in this case it is 173.
    Code:
    head
    address
    2
    ---------->address
                      41                 current
                       -------------->address
                                              173    
                                                ------------>NULL




    Then you search for the node just before that one using a pointer called previous, in this case it is the node 41.
    Code:
    address
    2            previous
    ---------->address
                      41              current
                     -------------->address
                                          173    
                                           ------------>NULL




    Then assign the address of new node to the pointer in previous and assign the address of current to pointer in new node.
    Code:
    head
    address
    2            previous       
    ---------->address       
                     41                newNode
                     --------------->address               
                                             53              current    
                                             ------------->address
                                                                  173    
                                                                   ------------>NULL
    Now put it into pseudo-code, taking into account special cases of no nodes in list, one node in list, and that value passed in to a general case will be the smallest value in the list:
    Code:
    struct node
    {
        int data
        node * next
    }
    
    node * head = NULL
    
    
    void insertVal(node * head,  int val)
    {
      declare newNode using dynamic memory
      assign val to newNode data
      assign NULL to newNode next
    
      declare two node pointers called current and previous
    
      assign head to both current and previous
    
    //no node in list
    if current is NULL  
    {
        assign newNode to head
    }
    //one node in list
    else if current not NULL but current next is NULL 
    {
      if newNode data less than current data
        assign current to newNode next
        assign newNode to head 
      else
        assign newNode to current next
    }
    //general case
    else if current not NULL and current next not NULL
    {
      while current next not NULL and current data < newNode data
    {
      assign current next to current
    }
    
      while current != head and previous->next->data not equal current data
    {
      assign previous next to previous
    }
    
      if current is same as head
         assign current to newNode next
         assign newNode to head
      else
        assign newNode to previous next
        assign current to newNode next
    }
    }
    Last edited by elad; 12-11-2002 at 11:56 AM.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Homework

    Hey, lightren-up joshdick & mangos! Princess100 did NOT ask you to do (her?) homework. She asked how to get started.

    Princess100: "Linked-lists" are common. This isn't something made-up for this particualr homework assignment. They should be covered in any introductory C++ book so you should be able to find plenty of info. Sorry, I don't have an example at hand.

    Also, try to be specific with your topic as it is more likely that someone who knows about the topic will read it. And as you've found out, be careful about how you ask for help with your homework. Read the FAQ which covers this stuff and "code-tags" which will make your posted code more readable.

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Princess100: Here is a good reference/place to start:

    http://www.brpreiss.com/books/opus4/html/book.html

    You can skip all the stuff at the beginning about computing time and go right to the sections on linked lists; it talks in detail about how to create them, what they're good for, etc.
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

Popular pages Recent additions subscribe to a feed