'TMF1434 Data Structure and Algorithms Semester 2, 2022/2023 LAB04: Linked List
'TMF1434 Data Structure and Algorithms Semester 2, 2022/2023 LAB04: Linked List
LAB 04
1. Defining the data structure for a linked list. Declare a data structure of a node named
node.
struct node
{
char name[20]; // Name of up to 20 letters
int age; // Age in integer
float height; // In meters
node *next; // Pointer to next node
};
node *start_ptr = NULL; // Start Pointer (root)
2. Adding the first node to the list (by adding the node at the end of the list).
Step 1: Declare the space for a pointer item and assign a temporary pointer to it.
node *temp;
temp = new node;
Step 2: Write statements to ask the user to fill in the details of the person, i.e: the name,
age and height.
cout << "Please enter the name of the person: ";
cin >> temp->name;
cout << "Please enter the age of the person : ";
cin >> temp->age;
cout << "Please enter the height of the person : ";
cin >> temp->height;
Step 3: If the list is empty to start with, set the start_ptr pointer to point to the new
node (i.e: set it to the same value as temp).
if (start_ptr == NULL)
start_ptr = temp;
Step 4: If there are already nodes in the list, declare a second pointer, temp2, to step
through the list until it finds the last node.
node *temp2;
temp2 = start_ptr; // We know temp2 is not NULL - list not empty!
while (temp2->next != NULL) // The loop will terminate when temp2
// points to the last node in the list
temp2 = temp2->next; // Move to next link in chain
temp2->next = temp; // Sets the pointer from that last node to point
// to the node that has just declared
TMF1434 Data Structure and Algorithms 2
LAB 04
After adding one or more nodes, we need to display the list of nodes on the screen. In
order to display the nodes, you need to traverse the whole list. These are the steps:
Step 1: Set a temporary pointer to point to the same thing as the start pointer.
Step 2: If the pointer points to NULL, display the message "End of list" and stop.
Step 3: Otherwise, display the details of the node pointed to by the start pointer.
Step 4: Make the temporary pointer point to the same thing as the next pointer of the
node it is currently indicating.
Step 5: Jump back to step 2.
The temporary pointer moves along the list, displaying the details of the nodes it comes
across. At each stage, it can get hold of the next node in the list by using the next pointer
of the node it is currently pointing to.
Hint:
// Step 1
Node *temp;
temp = start_ptr;
.
.
.
.
// Display details for what temp points to
cout << "Name : " << temp->name << endl;
cout << "Age : " << temp->age << endl;
cout << "Height : " << temp->height << endl;
cout << endl;
Step 1: Set a temporary pointer to point to the same thing as the start pointer.
Node *temp;
temp = start_ptr;
Step 2: Now that the first node has been safely tagged (so that we can refer to it even
when the start pointer has been reassigned), we can move the start pointer to the
next node in the chain.
start_ptr = start_ptr->next; // Second node in chain.
TMF1434 Data Structure and Algorithms 3
LAB 04
void delete_start_node()
{
Node *temp;
temp = start_ptr;
start_ptr = start_ptr->next;
delete temp;
}
Step 1: Look at the start pointer. If it is NULL, then the list is empty, so print out a "No
nodes to delete" message.
Step 2: Make temp1 point to whatever the start pointer is pointing to.
Step 3: If the next pointer of what temp1 indicates is NULL, then we've found the last
node of the list, so jump to step 7.
Step 4: Make another pointer, temp2, point to the current node in the list (where
temp1 is pointing to).
Step 5: Make temp1 point to the next item in the list.
Step 6: Go to step 3.
Step 7: If you get this far, then the temporary pointer, temp1, should point to the last
item in the list and the other temporary pointer, temp2, should point to the last-
but-one item.
Step 8: Delete the node pointed to by temp1.
Step 9: Mark the next pointer of the node pointed to by temp2 as NULL - it is the new
last node.
void delete_end_node()
{
Node *temp1, *temp2;
if (start_ptr == NULL)
cout << "The list is empty!" << endl;
else
{
temp1 = start_ptr;
while (temp1->next != NULL)
{
temp2 = temp1;
temp1 = temp1->next;
}
delete temp1;
temp2->next = NULL;
}
}
TMF1434 Data Structure and Algorithms 4
LAB 04
This is certainly necessary when you want to insert or delete a node from somewhere
inside the list, as you will need to specify the position.
Step 1: Declare a mobile pointer current and set the same value as start_ptr
pointer.
node *current;
current = start_ptr;
Step 2: If you want to get the current pointer to point to the next node in the list, assign
it to the next field of the node that it is pointing at the moment.
current = current->next;
Step 3: Check whether the current pointer is pointing to the last item in the list. If it is,
then there is no next node to move to.
// Fix the if condition below
if (……………………………………………………………………)
cout << "You are at the end of the list." << endl;
else
current = current->next;
Step 1: Check to see if the current node is also the first node. If it is, then there is no
previous node to point to. If not, check through all the nodes in turn until we
detect that we are just behind the current one.
if (current == start_ptr)
cout << "You are at the start of the list" << endl;
else
{
node *previous; // Declare a temporary pointer
previous = start_ptr;
while (previous->next != current)
previous = previous->next;
current = previous;
}
TMF1434 Data Structure and Algorithms 5
LAB 04
#include <iostream>
#include <string>
using namespace std;
class linkedList
{
private:
struct node
{
char name[20]; // Name of up to 20 letters
int age; // Age in integer
float height; // In meters
node *next; // Pointer to next node
};
node *start_ptr; // Start Pointer (root)
public:
linkedList(){start_ptr = NULL;} // Add constructor and initialize
//start pointer to NULL
int menu();
void add_node_at_end();
void displayList();
void delete_start_node();
void delete_end_node();
};
int linkedList::menu()
{
int menuOption = 0;
switch (menuOption)
{
case 1:
add_node_at_end();
break;
case 2:
displayList();
break;
case 3:
delete_start_node();
break;
case 4:
delete_end_node();
break;
case 5:
TMF1434 Data Structure and Algorithms 6
LAB 04
return 0;
break;
}
return 0;
}
void linkedList::add_node_at_end()
{
node *temp, *temp2; // Temporary pointers
menu();
}
void linkedList::displayList()
{
node *temp;
temp = start_ptr;
while(temp)
{
cout << "Name : " << temp->name << endl;
cout << "Age : " << temp->age << endl;
cout << "Height : " << temp->height << endl;
cout << endl;
temp = temp->next;
}
menu();
void linkedList::delete_start_node()
{
TMF1434 Data Structure and Algorithms 7
LAB 04
node *temp;
temp = start_ptr;
start_ptr = start_ptr->next;
delete temp;
menu();
}
void linkedList::delete_end_node()
{
node *temp1, *temp2;
if (start_ptr == NULL)
cout << "The list is empty!" << endl;
else
{
temp1 = start_ptr;
if (temp1->next == NULL) // This part is new!
{
// Here comes your code
// Fix me!
delete temp1;
start_ptr = NULL;
}
else
{
while (temp1->next != NULL)
{
temp2 = temp1;
temp1 = temp1->next;
}
delete temp1;
temp2->next = NULL;
}
}
menu();
}
int main()
{
linkedList listStudent;
listStudent.menu();
return 0;
}