Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Linked list
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Singly linked list=== Singly linked lists contain nodes which have a 'value' field as well as 'next' field, which points to the next node in line of nodes. Operations that can be performed on singly linked lists include insertion, deletion and traversal. [[Image:Singly-linked-list.svg|frame|center|A singly linked list whose nodes contain two fields: an integer value (data) and a link to the next node]] The following C language code demonstrates how to add a new node with the "value" to the end of a singly linked list:<syntaxhighlight lang="c" line> // Each node in a linked list is a structure. The head node is the first node in the list. Node *addNodeToTail(Node *head, int value) { // declare Node pointer and initialize to point to the new Node (i.e., it will have the new Node's memory address) being added to the end of the list. Node *temp = malloc(sizeof *temp); /// 'malloc' in stdlib. temp->value = value; // Add data to the value field of the new Node. temp->next = NULL; // initialize invalid links to nil. if (head == NULL) { head = temp; // If the linked list is empty (i.e., the head node pointer is a null pointer), then have the head node pointer point to the new Node. } else { Node *p = head; // Assign the head node pointer to the Node pointer 'p'. while (p->next != NULL) { p = p->next; // Traverse the list until p is the last Node. The last Node always points to NULL. } p->next = temp; // Make the previously last Node point to the new Node. } return head; // Return the head node pointer. } </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Linked list
(section)
Add topic