LinkedListery Goodness

This is a discussion on LinkedListery Goodness within the C++ Programming forums, part of the General Programming Boards category; This thread seems to have moved quite off-topic. Seeing as you say there are many ways if making linked lists...could ...

  1. #16
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    This thread seems to have moved quite off-topic.

    Seeing as you say there are many ways if making linked lists...could someone please just enter a simple program that will compile and show me how to use linked lists and output them.

  2. #17
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Here's a link to the FAQ section of this site that provides all you ask for.

    http://www.cprogramming.com/tutorial/lesson15.html
    You're only born perfect.

  3. #18
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    It seems easier to do it with classes....Kurisu's way looks like something that I understand...could someone just complete the code for me please.

  4. #19
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    The first example you were given by Kurisu is a pretty simple, but fully functional linked list. To create the list, the easiest way (IMHO) is to build it in 'reverse' order. By this, I mean, create the last/end node first.. then work your way to the front

    Slightly modified version of Kurisu's first example:
    Code:
    struct node
    {
       int value;
       node* next;
       node(node* n, int i) : next(n), value(i) {}
    };
    
    int main()
    {
      node* current = NULL;
    
      for (int i=0; i!=10; ++i)
          current = new node(current, i);
          // Build a list in reverse
    }
    Since the above code has built the list backwards, by the end of the for loop, current will be a pointer to the node object at the front of the list.
    Last edited by Bench82; 04-26-2006 at 12:17 PM. Reason: removed unnecessary #include

  5. #20
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    declare a public member function of the linkedList class to display the data stored within the linked list

    within the body of the function definition do something like this:
    Code:
    node * current;   //use a temporary node pointer to traverse the list
    current = head;   //start at the front of the list
    while(current != NULL)  //assuming there is a value associated with current
    {
       cout << current->value;  //display it
       current = current->next; //now go to the next node in the the list
    }
    You're only born perfect.

  6. #21
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62

    Example

    If your still looking for a simple example here is something that should compile.

    1) Inserts 3 items into a linked list.
    2) Displays all items to standard output.
    3) Destroys linked list (frees allocated memory)

    Code:
     #include <iostream>
    
     // THIS SECTION [1] WOULD NORMALLY GO INTO "linkedList.h"
     // #ifndef linkedList_h
     // #define linkedList_h
     
    struct node
    {
        int ID; 
        int phoneNum; 
        node *next; 
    
        // node *prev;  Used for a doubly linked list.  i.e. traverse both ways.
    };
    
    class linkedList
    {
      public:
    	linkedList(); 
    	~linkedList(); 
    	void insertItem(int ID, int phoneNum);
    	void outputList(); 
    	
      private:
    	node *head; 
    
        // Can add more stuff if desired i.e.	   int numEntries;  node *lastEntry;
    };
    
     // #endif
     // END OF SECTION [1]
    
     // THIS SECTION [2] WOULD NORMALLY GO INTO "linkedList.cpp"
     // #include "linkedList.h" 
    
    linkedList::linkedList()
    {
    	head = NULL;
    }
    
    linkedList::~linkedList()
    {
    	node *iterator;
    
    	while(head != NULL)
    	{
    		iterator = head;
    		head = head->next;
    
    		delete iterator;
    	}
    }
    
     void linkedList::insertItem(int ID, int phoneNum)
    {
    	
    
    	if(head == NULL)
    	{
    		head = new node;
    		head->ID = ID;
    		head->phoneNum = phoneNum;
    		head->next = NULL;
    	}
    	else
    	{
    		node *iterator = head;
    
    		while(iterator->next != NULL)
    		{
    			iterator = iterator->next;
    		}
    
    		iterator->next = new node;
    		iterator = iterator->next;
    		iterator->ID = ID;
    		iterator->phoneNum = phoneNum;
    		iterator->next = NULL;
    	}	
    }
    
     void linkedList::outputList()
    {
    	node *iterator = head;
    
    	while(iterator != NULL)
    	{
    		std::cout << "ID #: " << iterator->ID << "\nPhone Number: " << iterator->phoneNum << "\n\n";
    		iterator = iterator->next;
    	}
    }
    
     // END OF SECTION [2]
    
     // THIS SECTION [3] WOULD NORMALLY GO INTO "main.cpp"
     // #include "linkedList.h" 
    
     int main()
    {
        linkedList students;
    
       students.insertItem(1, 2983223);
       students.insertItem(2, 1456784);
       students.insertItem(3, 4868336);
    
       students.outputList();
    
       return 0;
    } 
     // END OF SECTION [3]
    Of course you can modify this for inserting items anywhere in the list; searching list; etc.

    Graphical Representation of above Linked List
    Last edited by Kurisu; 04-26-2006 at 03:36 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Party goodness
    By master5001 in forum Party Board
    Replies: 0
    Last Post: 09-26-2008, 02:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21