Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lab tutorial 10
Pointers, struct and Linked list
In this tutorial, lets make all the doubts about pointers and linked list
clear!
Using linked list to handle student records
Task 1. Pointer variable may also pass by value / pass by reference!
struct Product{
string name;
double price;
Product *next;
};
void initialize_products(Product * & head);
void show_menu(Product * head);
double purchase(Product * head);
void checkout(double);
void tailInsert (Product * & head, string n, double p);
Product * findLast( Product *head );
void removeProduct(Product * & head, string n);
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk
2
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk
Step 1. Standard steps to open a file and create an ifstream object fin.
ifstream fin;
fin.open("product_list.txt");
if ( fin.fail() ){
cout << "Error in opening the file !" << endl;
exit(0); // Force the program to terminate
}else{
// Load data from file, and insert to linked list
}
fin.close();
Step 2. Load the whole line from the file into a string variable line
(Using getline() without the 3rd input parameter can read the whole line
including space) .
string name;
double price;
while (fin >> name){
fin >> price;
Step 3. With name and price, lets insert a new node to the tail of the
linked list with the function tailInsert(). We will implement the
tailInsert() function in the next task.
4
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk
Create a new Product and make that Products next points to NULL.
// Step
Product
newNode
newNode
newNode
5
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk
We need to handle two cases: when the list is empty / not empty.
If the list is not empty, we make a pointer *last, and point it to the last
node in the list. Assume that we will build a findLast() function to return
the address of the last Product in the linked list.
6
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk
last->next = newNode;
head = newNode;
7
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk
8
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk
current = current->next;
}
Step 2. Print out the name and price of each Product structure pointed
to by the pointer current.
Note that current is a pointer to Product structure, so we should use
-> to access the member variable of the Product structure pointed to
by current.
cout << current->name << " $" << current->price << endl;
9
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk
Step 1. Lets traverse the whole list, and remove the node if the
Products name is equal to n.
Since users will call the removeProduct() function by passing in the
head variable, which is the pointer to the first node in the linked list,
and we may update head to point to other nodes (if the first node is
the node to delete), we need to pass by reference.
Step 2. Use a while loop to traverse the list, use previous and current
to point to the previous node and current node when we traverse the list.
10
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk
Step 4. Handle the cases when the node to remove is not the first node.
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk
Study the code in the program and see if you understand the implementation.
12
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk