Linked Lists

This is a discussion on Linked Lists within the C++ Programming forums, part of the General Programming Boards category; Hi,
I was wondering how to write a linked list from scratch. It can't be too hard, I have about ...

Linked Lists

Hi,

I was wondering how to write a linked list from scratch. It can't be too hard, I have about 80% of it down(from MUD programming) but I could never werite my own linked list from scratch. any suggestions?

thanks

A linked list is a container that holds instances of a struct/class. The struct/class has the interesting property of having a(t least one) pointer to it's own type, and usually a data member as well (why would anyone just have a group of pointers to other pointers????). Therefore the simplest linked list looks something like this (it will work in both C and C++):

#include <iostream.h>

struct node
{
char data;
node * next;
}

int main()
{
node * head;
return 0;
}

head can be considered to be the first node in a linked list list that has yet to be initialized and therefore useless from a practical standpoint (until something is done to it). Often in C++ programs the list is encapsulated in a class of it's own that includes all the functions necessary to manipulate the list such as adding new nodes to the list, deleting nodes from the list, displaying data stored in the list, sorting the data stored in the list, etc. etc. You have the freedom to design the list however you wish. You can use templated lists if you want them to be more general and there is a templated list class all done up for you in STL; you can have singly linked, doubly linked, and circular linked lists; the list can have a head node, tail node, current node, previous node, etc. etc. As usual, with all this freedom you have the responsibility of setting things up the way you wish.