03 Lists (Using Linked Lists)
03 Lists (Using Linked Lists)
N1, N2, . . ., Nn
Each linked list has a pointer that points to the first node in
the list (node N1).
N1 N2 N3 Nn
start
N1 N2 N3 Nn
start
N1 N2 N3 Nn
start
The address field of the last node, Nn, contains a special value
called NULL which means that the address field of the last node
does not point to anything.
A pointer (in this example, start) will point to the first element or
item in the list. This allows the program to easily locate the first
node.
{
Data
char name[50];
char address[100];
int age;
float weight;
float height;
Node NODE *next;
};
struct NODE
{
int value;
NODE *next;
};
3. Each node has two fields. The first field contains the value of
the integer.
5. The address field of the last node has the value NULL to
indicate that it is not pointing to any other node and that it is
the last node of the list.
7. An empty list is a linked list that does not contain any node.
Since there is no first node, start is equal to NULL (start is not
pointing to anything).
#include <iostream>
using namespace std;
struct NODE
{
int value;
NODE *next;
};
main()
{
NODE node1, node2, node3, node4, *start;
node1.value = 111;
node2.value = 222; This program is not practical
node3.value = 333; since it is static (additional
node4.value = 444; nodes cannot be created while
the program is running).
start = &node1;
One of the advantages of
node1.next =&node2; linked lists is that programs can
node2.next =&node3; create additional nodes during
node3.next =&node4; run-time (to be discussed
node4.next = NULL; later).
}
Traversing a linked list means visiting each and every node in the
list and perhaps performing a necessary operation (such as
printing or displaying the contents of the node).
Always remember that the pointer start should not be moved (it
should always point to the first item in the list).
First, let the pointer ptr point to the first node (ptr should point to
the same thing start is pointing at). This is done by executing:
ptr = start;
ptr
ptr
cout << "The value of the node is " << (*ptr).value << ".";
cout << "The value of the node is " << ptr -> value << ".";
ptr
Then let the pointer ptr point to the second node in the list. This
is done by executing:
ptr
ptr
cout << "The value of the node is " << ptr -> value << ".";
ptr
Then let the pointer ptr point to the third node in the list. This is
done by executing:
ptr
ptr
cout << "The value of the node is " << ptr -> value << ".";
ptr
Then let the pointer ptr point to the fourth node in the list. This is
done by executing:
ptr
ptr
cout << "The value of the node is " << ptr -> value << ".";
ptr
Take note that if the pointer ptr is pointing to the last node
and the instruction ptr = ptr -> next; is executed, ptr will
become equal to NULL. This means that there are no more
nodes to be traversed so the loop should be terminated.
ptr = start;
#include <iostream>
using namespace std;
struct NODE
{
int value;
NODE *next;
};
main()
{
node1.value = 111;
node2.value = 222;
node3.value = 333;
node4.value = 444;
start = &node1;
node1.next =&node2;
node2.next =&node3;
node3.next =&node4;
node4.next = NULL;
ptr = start;
ctr = 1;
• For example:
int x;
int *p;
#include <iostream>
#include <cstdlib>
using namespace std;
main()
{
int *p1;
char *p2;
p1 = (int*) malloc(sizeof(int));
p2 = (char*) malloc(sizeof(char));
cout << "\n\nThe integer you entered is " << *p1 << ".";
cout << "\n\nThe character you entered is " << *p2 << ".";
}
• To add a new node at the rear of the list, there must be a pointer
pointing to the last node of the list
ptr2
ptr1
value next
• The next step is to put values in the fields of the new node. To put
data in the value field, execute
ptr1 -> value = 555; (or ask the user to input the data)
The next field should be made equal to NULL since the new node
will become the last node in the list. This is done by executing:
ptr1 555
value next
ptr2
ptr1 555
• Take note that if the newly created node happens to be the first
node to be created (the list is empty or start = NULL) , then make
sure to make start and ptr2 point to that node:
if (start == NULL)
{
start = ptr1;
ptr2 = ptr1;
}
• Sample Program for Creating a Linked List (and then printing the nodes)
#include <iostream>
using namespace std;
struct NODE
{
int value;
NODE *next;
};
main()
{
NODE *start, *ptr1, *ptr2;
int i, number_of_nodes;
start = NULL;
cout << "Enter the number of nodes: ";
cin >> number_of_nodes;
if (start == NULL)
{
start = ptr1;
ptr2 = ptr1;
}
else
{
ptr2 -> next = ptr1;
ptr2 = ptr2 -> next;
}
}
ptr1 = start;
if (ptr1 == NULL)
cout << "The list is empty!";
else
{
i = 1;
while (ptr1 != NULL)
{
cout << "\nThe value of node " << i << " is " << ptr1 -> value <<".";
++i;
ptr1 = ptr1->next;
}
}
}
struct NODE {
int value;
NODE *next;
};
NODE *start;
• Since some functions for the operations on linked lists need to access the
list itself, it should be able to access the pointer variable start. Therefore, it
must be passed on to these functions as an argument.
list_empty (start);
It should be noted that list_empty() does not have to modify the value of
start.
ctr = 0;
while (p != NULL)
{
++ctr;
p = p -> next;
}
return (ctr);
}
• Function to Locate a Node in a Linked List and Print its Position (assuming nodes are unique)
• The first step is to create a new node and enter the necessary
values (assume the pointer ptr1 will point to the new node and
the variable data contains the value of this node):
• Next is to have a pointer (ptr2) point to the last element in the list
(node n).
ptr2
ptr2 = start;
while (ptr2 -> next != NULL)
ptr2 = ptr2 -> next;
ptr2
ptr1 555
• If the list is empty (start = NULL), then the pointer start should point to the
new node.
• This would mean that the function to insert/add a node would have to
modify the value of start which is most likely a local variable to main() or to
some other function.
• For the add_item() function to do this, the address of start must be passed
on to the function. Consequently, the function must have a pointer to start
to store this address (a pointer to another pointer):
• Assume the node will be inserted at position pos. The first step is to create a
new node and enter the necessary values (assume the pointer ptr will point to
the new node and the variable add_value contains the value of this node):
999
ptr
• If pos = 1 (the new node will become the first node in the list):
999
ptr
• If pos > 1, then the next step is to have two pointers (before and here) to point
to the nodes before and at the position where the node is to be inserted. In
other words, before should point to the node at position pos – 1 and here should
point to the node at position pos.
before here
999
ptr
else
{
before = *ptr_start;
here = (*ptr_start) -> next;
ctr = 1;
while (ctr <= pos-2)
{
before = before -> next;
here = here -> next;
++ctr;
}
before -> next = ptr;
ptr -> next = here;
}
cout << "\nItem Successfully Added!";
}
before discard
before discard
discard = start;
while (discard != NULL && discard->value != delete_value)
{
before = discard;
discard = discard->next;
}
before discard
333
before
discard
333
before
discard
if (list_empty(*ptr_start) == 1)
delete_node (&start, delete_data);
cout << "\nThe List is Empty!";
else
{
if ((*ptr_start) -> value == delete_value)
{
*ptr_start = (*ptr_start) -> next;
free(discard);
cout << "\nItem Successfully Deleted!";
}
else
{
while (discard != NULL && discard -> value != delete_value)
{
before = discard;
discard = discard -> next; To invoke this function at main():
}
delete_node (&start, delete_data);
if (discard == NULL)
cout << "\nNode To Be Deleted Does Not Exist!";
else
{
before -> next=discard -> next;
free(discard);
cout << "\nItem Successfully Deleted!";
}
}
}
}
• Sample declaration:
The prev field points to the previous node in the list while
the next field points to the next node in the list.
• Deleting a Node
discard
discard = *ptr_start;
else
{
while (discard != NULL && discard -> value != delete_value)
discard = discard -> next;
if (discard == NULL)
cout << "\nNode to be deleted does not exist!";
else
{
discard -> next -> prev = discard -> prev;
discard -> prev -> next = discard -> next;
free(discard);
}
}
}
• Inserting a Node
Assume that a pointer before points to the node that precedes the node to be inserted and
that pointer new points to the node to be inserted.
ptr 999
before