Thread: C++ linked list

  1. #1
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59

    C++ linked list

    Please I Need help in retrieving the value of the node of its given position

    here's my code, but there's is a problem in returning the value . .

    Code:
    int n=1;
          Node *newNode=head;
          while(newNode->next!=NULL)
           {
                if(newNode->data==location) {
                   newNode=newNode->next;
                    n++;
                } else {
                    break;
               }
           }
          return newNode->data;

  2. #2
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    if i have 7 2 3

    I enter position 1 and it must return 7....

    but i have a new element at first so 2 7 2 3

    I enter position 1 why is it returning the same value "7". .. instead of 2 . . that's my problem

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It would be better to start completely over.

    You're ok until we get to here:
    if(newNode->data==location)
    Will compare the data in the node to the location you want to find. These two things don't really coincide. Plus, you break the loop at the point the comparison is false, which will be most of the time.

    You should be counting the number of nodes you've passed over, and break when you reach the location. Data has nothing to do with it.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    26
    I'm new also but i think this is about what you are trying to do

    Code:
    int location;   // which list do you want to access?
    int ctr;         // needs a counter to keep track of the list we are currently in
    node* conductor = head;   // start at the beginning
    for(ctr = 1; ctr != location; ctr++) //run this loop until you are at the nth list
    {
        if(conductor->next == (node*)NULL) // seeing if there is a next list
        {
            // location does not exist, insert some error handling stuff
            break;
        }
        conductor = conductor->next; // moving thru the next link
    }
    if((ctr == location) && (conductor != (node*)NULL))  // pointer is good, and we should be in the right list
    {
    return conductor->data;   // return some data
    }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The term "list" is used inappropriately here. The term you seek is "node."
    There is only one list, and it links several nodes together.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by seanJ View Post
    I'm new also but i think this is about what you are trying to do
    you're new, so the forum rules should be fresh in your memory, having read them very recently, right? so why did you post a complete solution, which is against the rules?

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    26
    Sorry, I didn't know that

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    26
    So the *head is the list, *conductor is a node, and conductor->next is a link? Is there a special name for a list that loops, where the last link points to the first node? I will read the forum rules when I get home, wasn't trying to agitate anyone. List's are kind of a weird concept, at least they seemed that way when I first read about them because of how they are accessed. It didn't make any sense to me until I really focused on a few functions In my book about them.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by seanJ
    Is there a special name for a list that loops, where the last link points to the first node?
    Circular linked list.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Linked List inside Linked List
    By de4th in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2006, 11:17 AM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM