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.
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.
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.
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.
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: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.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 }
Last edited by Bench82; 04-26-2006 at 11:17 AM. Reason: removed unnecessary #include
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.
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)
Of course you can modify this for inserting items anywhere in the list; searching list; etc.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]
Graphical Representation of above Linked List
Last edited by Kurisu; 04-26-2006 at 02:36 PM.