Linked Lists Lecture Notes
Linked Lists Lecture Notes
LINKED LISTS
What is a linked list?
- is a data structure in which each data item points to the
next data item. This “linking” is accomplished by
keeping an address variable (a pointer) together with each
data item.
- This pointer is used to store the address of the next data
item in the list.
- The structure that is used to store one element of a linked
list is called a node.
- A node has two parts: data and next address. The data
part contains the necessary information about the items of
the list, the next address part contains the address of the
next node.
Accessing and Creating Linked Lists:
A special pointer will be used to point to the first node of a
linked list.
- pointer head.
o At the beginning, when there are no nodes, head will
be NULL (pointer pointing to nowhere).
o As soon as there is a node in the list, head will
contain the address of this first node.
- A list with no nodes is called an empty list or a null list.
o A NULL Pointer is used to signal the end of a list.
The last node will have NULL in its next address
part.
} node_t;
} node_t;
// Pointer to a node:
typedef node_t *nodeptr;
//Declaring a node:
node_t course_node;
//Declaring a pointer to a node:
nodeptr ptr;
node_t *ptr; /* This has exactly the same meaning */
nodeptr getnode(void)
{
nodeptr ptr;
ptr = (node_t *)malloc(sizeof(node_t));
return(ptr);
}
Actions Related With Linked Lists:
(These will be explained in the lecture.)
Adding a node to a Linked List:
(a) To the beginning
(b) To the end
(c) Between two nodes.
(d)Traversing a Linked List.
(e)Deleting a node from a Linked List: