Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Linked list
(section)
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>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)