Thread: linked lists

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    57

    linked lists

    i need to create a linked list of characters which i'll be retrieving from a text file. my problem is that i'll need to access this data in the same order that i retrieve it in. i am only used to writing linked lists so that it actually reverses the data using a stack. any ideas on this?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "i am only used to writing linked lists so that it actually reverses the data using a stack. any ideas on this?"

    Yep. Learn to write 'em so you can access the items in the list in any order. How about having data members of the list like:

    Node* pbeginning
    Node* pcurrent
    Node* pend
    Last edited by 7stud; 04-03-2003 at 01:08 AM.

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Create a double linked list (DLL) and traverse it backwards.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    32
    "i am only used to writing linked lists so that it actually reverses the data using a stack. any ideas on this?"

    First lets straighten out your terminology. A stack and a linked list are two seperate data structures. I'll assume you are refering to a linear linked list. What you should use, is a circular linked list.

    If you need more information just post, I'm a bit tight on time.

  5. #5
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    You could learn how to add a new node at the end of a list instead of the beginning and save yourself a lot of trouble.
    Code:
    // Assuming a node like this
    struct node {
        int content;
        node *next;
    
        node(int c, node *n)
        {
            content = c;
            next = n;
        }
    };
    
    // Add at the front
    current = new node(data, front);
    front = current;
    
    // Add at the end
    if (front != 0)
    {
        for (current = front; current != 0; current = current->next)
        {
            if (current->next == 0)
                break; // Found the end
        }
    
        current->next = new node(data, 0);
    }
    else
        front = new node(data, 0); // Empty list

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM