Revision as of 10:08, 2 December 2003 editKenny Moens (talk | contribs)147 editsm Scheme← Previous edit | Revision as of 17:13, 2 December 2003 edit undoDcoetzee (talk | contribs)37,529 editsm Single pointer, not doubleNext edit → | ||
Line 4: | Line 4: | ||
] | ] | ||
In ], a '''linked list''' is one of the simplest ]s used in ]. It consists of a number of ]s, each containing one element and one (or more) links to other nodes. Linked lists permit insertion and removal of nodes at any point in the list in ](1) time |
In ], a '''linked list''' is one of the simplest ]s used in ]. It consists of a number of ]s, each containing one element and one (or more) links to other nodes. Linked lists permit insertion and removal of nodes at any point in the list in ](1) time. Linked lists do not allow ]. | ||
=== Types of Linked Lists === | |||
The simplest kind of linked list is a ''singly linked list'', which has one link per node. This link points to the next node in the list, or to a ] value if it is the last node. This kind of list allows ] to elements only in one direction (from front to back). | The simplest kind of linked list is a ''singly linked list'', which has one link per node. This link points to the next node in the list, or to a ] value if it is the last node. This kind of list allows ] to elements only in one direction (from front to back). | ||
A more sophisticated kind of linked list is a ''doubly linked list''. Each node has two links, one to the previous node and one to the next. This allows sequential access to the list in both directions |
A more sophisticated kind of linked list is a ''doubly linked list''. Each node has two links, one to the previous node and one to the next. This allows sequential access to the list in both directions. | ||
There are two significant variations to the types mentioned above. First is a ''circularly linked list'', where the first and last nodes are linked together. This can be done for both singly and doubly linked lists. The other variation is the addition of a ''] node'' at the beginning of the list. This node represents ''before the first node'' and/or ''after the last node'' of the list. It also makes it possible to have an empty list. | There are two significant variations to the types mentioned above. First is a ''circularly linked list'', where the first and last nodes are linked together. This can be done for both singly and doubly linked lists. The other variation is the addition of a ''] node'' at the beginning of the list. This node represents ''before the first node'' and/or ''after the last node'' of the list. It also makes it possible to have an empty list. | ||
⚫ | Many ]s, such as ] have linked lists built in. In other languages it is simple to create the data structure using ] or ] to implement links between list nodes. For example, in ]: | ||
A type of linked list with performance similar to a binary tree is the ], which adds extra pointers that move along the list in large increments. These links facilitate faster (O(log <var>n</var>)) random access. Maintaining these links comes at a price, however: insertion and removal are slowed. | |||
=== Programming Language Support === | |||
⚫ | Many ]s, such as ] and ] have linked lists built in. In other languages it is simple to create the data structure using ] or ] to implement links between list nodes. For example, in ]: | ||
typedef struct Person | typedef struct Person | ||
{ | { | ||
struct Person |
struct Person *prev; // <--- previous person in list | ||
struct Person *next; // <--- next person in list | struct Person *next; // <--- next person in list | ||
char name; | char name; | ||
Line 39: | Line 33: | ||
scanf("%s %d", &(p->name), &(p->age)); // Use it | scanf("%s %d", &(p->name), &(p->age)); // Use it | ||
} | } | ||
A curious algorithm for linked lists is ]s. | |||
== Linked Lists On The Web == | == Linked Lists On The Web == |
Revision as of 17:13, 2 December 2003
In computer science, a linked list is one of the simplest data structures used in computer programming. It consists of a number of nodes, each containing one element and one (or more) links to other nodes. Linked lists permit insertion and removal of nodes at any point in the list in O(1) time. Linked lists do not allow random access.
The simplest kind of linked list is a singly linked list, which has one link per node. This link points to the next node in the list, or to a null value if it is the last node. This kind of list allows sequential access to elements only in one direction (from front to back).
A more sophisticated kind of linked list is a doubly linked list. Each node has two links, one to the previous node and one to the next. This allows sequential access to the list in both directions.
There are two significant variations to the types mentioned above. First is a circularly linked list, where the first and last nodes are linked together. This can be done for both singly and doubly linked lists. The other variation is the addition of a sentinel node at the beginning of the list. This node represents before the first node and/or after the last node of the list. It also makes it possible to have an empty list.
Many programming languages, such as LISP have linked lists built in. In other languages it is simple to create the data structure using pointers or references to implement links between list nodes. For example, in C:
typedef struct Person { struct Person *prev; // <--- previous person in list struct Person *next; // <--- next person in list char name; int age; };
An example of code that uses this structure:
Person first; // first item in the linked list Person *p = &first;
while(!feof(stdin)) // there's still input { p->next = (Person *)malloc(sizeof(Person)); // first link p = p->next; printf("Enter name and age:\n"); // Ask for input scanf("%s %d", &(p->name), &(p->age)); // Use it }
A curious algorithm for linked lists is Xor linked lists.
Linked Lists On The Web
Some linked list materials are available from the Stanford Computer Science department: