Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 04-26-2006
bumfluff
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.
• 04-26-2006
Here's a link to the FAQ section of this site that provides all you ask for.

http://www.cprogramming.com/tutorial/lesson15.html
• 04-26-2006
bumfluff
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.
• 04-26-2006
Bench82
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.
• 04-26-2006
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 }```
• 04-26-2006
Kurisu
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
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12