Singly Link Lists
*NOTE: The primary use of dynamic variables is
to form flexible structures whose size can
“expand or shrink” to accommodate
varying data sizes.
Linked Lists
- consists of several dynamic variables linked together to
form a chain-like structure
- dynamic variables can be created and destroyed within
the program, the size of the linked list can be adjusted
appropriately
- Each variable is called a NODE of the linked list
- Each node stores two items of information:
o An element of the list
o a link or pointer that indicates explicitly
the location of the node containing the
successor of this list element
- Access to the node storing the first list element must be
maintained so there must a pointer pointing to the first
node. This is needed because the dynamic variables
do not have names (the
- only way to refer to them is through an external pointer
like head)
A Singly Link that has two nodes
Referring to the nodes in the list
● The only way to refer to them is through an external
pointer
typedef struct node *nd;
struct node {
<data-type 1> <field-name 1>;
<data-type 2> <field-name 2>;
…
<data-type n> <field-name n>;
nd next; //next is the internal pointer that can point to
either NULL or another node with the same structure as
NODE
} NODE; //NODE is the new name of the node structure that
will be used in the program body; this can be any name
● If there are other external pointers (e.g., ptr and temp),
you can use these pointers to refer to the nodes in the
list.
Declaration in C
Creating a node to call to malloc()
● The malloc() function stands for “memory allocation”
● This function is found in the header file stdlib.h
● It is used to dynamically create space for arrays,
structures and unions
Example:
1. Head = malloc(sizeof(NODE));
//let head create a new node with the size of the
structure NODE
2. ptr = head;
//let ptr point to whatever head is pointing to
3. ptr -> next = malloc(sizeof(NODE));
//let ptr->next create a new node
● external pointers (like head and ptr) and internal
pointers (like next) can create new nodes with a call to
malloc()
● an internal pointer (like next) can only be accessed
with the help of an external pointer (like head)
● Therefore, the first internal pointer next can be
accessed/referred to as:
head -> next or ptr -> next
● while the second next can be accessed as
head -> next -> next
or
ptr -> next -> next
and can create new nodes with malloc()
ptr -> next -> next = malloc(sizeof(NODE));
sizeof() function
● This function automatically calculates the memory to
be allocated
Sample: head = malloc(sizeof(NODE));
Storing data
● Integers, characters and floats
- Use the assignment operator
- Example:
head -> studentID = 12345;
scanf(“%d”, &sID);
head - > studentID = sID ;
● Strings
- Use the strcpy() function
- Example:
gets(lName); //assuming “Rivera”
strcpy(ptr ->lastName, lName);
Creating the list:
ptr = head;
ptr = ptr -> next;
head = malloc(sizeof(NODE));
strcpy(ptr -> name, “Maria”);
head ptr -> next = NULL;
strcpy(head -> name, “Velvette”);
head -> next = malloc(sizeof(NODE));
Inserting a node at the beginning of the list
head = temp;
temp = NULL;
temp = malloc(sizeof(NODE));
temp -> num = 1;
free(temp);
temp -> next = head;
Inserting a node at the end of the list
ptr -> next = temp;
temp -> next = NULL;
temp = NULL;
temp = malloc(sizeof(NODE));
temp -> num = 6;
ptr = head;
while (ptr -> next != NULL && ptr -> num < temp -> num)
ptr = ptr->next;
free(temp);
free(ptr);
Inserting a node in the middle of the list
while (p -> next != NULL && (p -> num < temp -> num && q ->
num < temp -> num)){
q = p; p = p->next;
}
temp = malloc(sizeof(NODE));
temp -> num = 4;
p = head;
q = p;
temp -> next = p;
q -> next = temp;
Displaying the list (traversing the list)
ptr = head;
Deleting a node from the beginning of the list
while (ptr != NULL){
printf("%d\t", pt r-> num);
ptr = ptr -> next;
}
ptr = head;
head = ptr -> next;
ptr -> next = NULL;
Deleting a node from the middle of the list
Deleting a node at the end of the list
ptr = head;
while (ptr -> next != NULL && n != ptr -> num){
q = ptr;
ptr = ptr -> next;
}
ptr = head;
while (ptr -> next != NULL && n != ptr -> num){
q = ptr;
ptr = ptr -> next;
}
q -> next = ptr -> next;
ptr -> next = NULL;
q -> next = ptr -> next;
ptr -> next = NULL;