03 Lists (Using Linked Lists)
03 Lists (Using Linked Lists)
N1, N2, . . ., Nn
address field
address field
struct NODE
{
char name[50];
char address[100];
int age;
float weight;
float height;
*
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;
};
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).
}
• For example:
int x;
int *p;
p = (int*) malloc(sizeof(int));
#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:
ptr1 = (NODE*)
NODE malloc(sizeof(NODE));
NODE
555
• The next step is to put values in the fields of the new node.
To put data in the value field, execute:
555
This is done by making the next field of the last node in the
list (the one pointed to be ptr2) point to the newly
created node (the one pointed to by ptr1).
The last step is to make ptr2 point to the last node. The reason for
this is just in case another node will be added.
ptr2 = ptr2->next;
• 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;
}
}
if (start == NULL)
cout << "The list is empty!";
else
{
ptr1 = start;
i = 1;
struct NODE
{
int value;
NODE *next;
};
NODE *start;
list_empty (start);
NODE *p
int list_empty (NODE *p) p
• It should be noted that list_empty() does not have to modify the value of
start.
void
void print_items (NODE
NODE *p)
{
int ctr;
p
if (list_empty(p) == 1)
cout << "\nThe List is Empty!";
else To invoke this function at main():
{
ctr = 1; print_items (start);
while (p != NULL)
{
cout << "\nThe value of node " << ctr << " is " << p -> value <<".";
p = p -> next;
++ctr;
}
}
}
• Function to Locate an Item in a List and Print its Position (assuming items are unique)
if (list_empty(p) == 1)
cout << "\nThe List is Empty!";
else p
{
ctr = 1;
while (p p != NULL && p -> value != search_data
search_data)
{
p = p -> next;
++ctr; To invoke this function at main():
}
if (p NULL)
p != NULL
locate_item (start, key);
cout << "Node requested is Node " << ctr << ".";
else
cout << "Node Does Not Exist.";
}
}
• 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 new_data
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.
Assume pos = 3
• To move the pointers before and here to their respective positions (the pointer
before at pos -1 and the pointer here at pos):
2 3 4 5 6 7 8
before = start;
here = start -> next;
Once before and here
are in their respective ctr = 1;
starting positions, they while (ctr <= pos-2)
would have to be
moved pos – 2 times. {
before = before -> next;
here = here -> next;
++ctr;
}
• So to insert a new node within the list, the following program segment must
be executed:
else
{
before = start;
ptr = (NODE*) malloc(sizeof(NODE)); here = start -> next;
ptr -> value = new_data; ctr = 1;
while (ctr <= pos-2)
if (pos == 1) {
{ before = before -> next;
ptr -> next = start; here = here -> next;
start = ptr; ++ctr;
} }
before -> next = ptr;
ptr -> next = here;
}
• Take note that if pos = 1 (or if the list is empty), the insert
function should make the pointer start point to the newly
created node.
if (pos == 1)
{
ptr -> next = start;
start = ptr;
}
• This would mean that the function to insert a node would have
to modify the value of start which is a local variable to main() or
to some other function.
{
ptr -> next = start;
*ptr_start; 999
start
*ptr_start
= ptr;= ptr; ptr_start
ptr
}
if (pos == 1)
{
ptr -> next = *ptr_start;
*ptr_start = ptr;
}
else
{
*ptr_start;
before = start;
(*ptr_start)
here = start -> next;-> next;
ctr = 1;
while (ctr <= pos-2) ptr_start
{
before = before -> next;
here = here -> next;
++ctr;
}
before -> next = ptr;
ptr -> next = here;
}
cout << "\nItem Successfully Added!";
}
To make discard point to the node to be deleted (and before point to the
preceding node):
discard = start;
while (discard
discard != NULL && discard -> value != delete_value
delete_value)
{
before = discard;
discard = discard->next;
}
discard = start;
discard = start;
if (discard == NULL)
cout << "\nNode To Be Deleted Does Not Exist!";
else
before -> next=discard -> next;
• If the node to be deleted happens to be the first node in the list, start
should now point to the second node and delete the designated node.
discard = start;
discard
else
{
while (discard != NULL && discard -> value != delete_value)
{
before = discard;
discard = discard->next;
}
if (discard == NULL)
cout << "\nNode To Be Deleted Does Not Exist!";
else
before -> next = discard -> next;
}
discard = start;
else
{
while (discard != NULL && discard -> value != delete_value)
{
before = discard;
discard = discard->next;
}
if (discard == NULL)
cout << "\nNode To Be Deleted Does Not Exist!";
else
{
before -> next = discard -> next;
free(discard);
}
}
if (list_empty(*ptr_start) == 1)
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;
}
if (discard == NULL)
cout << "\nNode To Be Deleted Does Not Exist!";
else
{
before -> next = discard -> next;
free(discard);
cout << "\nItem Successfully Deleted!";
}
}
}
}
• Sample declaration:
struct NODE
{
int data;
NODE *prev;
NODE *next;
};
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 = *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.
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).
The pointer ptr should point to the first node (ptr should point to
the same thing start is pointing at). This is done by executing:
ptr = start;
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;
The pointer ptr should point to the first node (ptr should point to
the same thing start is pointing at). This is done by executing:
ptr = start;
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;
cout << "The value of the node is " << (*ptr).value << ".";
cout << "The value of the node is " << ptr -> value << ".";
Then let the pointer ptr point to the second node in the list. This
is done by executing:
cout << "The value of the node is " << ptr -> value << ".";
Then let the pointer ptr point to the third node in the list. This is
done by executing:
cout << "The value of the node is " << ptr -> value << ".";
Then let the pointer ptr point to the fourth node in the list. This is
done by executing:
cout << "The value of the node is " << ptr -> value << ".";
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;
• If pos = 1 (the new node will become the first node in the list):
999
ptr
• 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 new_data contains the value of this node):
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.
Assume pos = 3
• 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.
Assume pos = 3
• 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 new_data contains the value of this node):
• Next is to have a pointer (ptr2) point to the last element in the list.
ptr2 = start;
while (ptr2 -> next != NULL)
ptr2 = ptr2 -> next;
ptr2 = start;
while (ptr2 -> next != NULL)
ptr2 = ptr2 -> next;
ptr2 -> next = ptr1;
• If the list happens to be empty (start = NULL), then the program should
ensure that the pointer start should point to the newly created node. So
the program segment to add the new node will now be:
• This would mean that the function to add a node would have to
modify the value of start which is a local variable to main() or to
some other function.
before here
999
ptr