Sei sulla pagina 1di 12

ENGG 1111B Computer Programming and Applications

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);

Question: What is Product * & head ?


Product * head means that head is a pointer to Product structure.
Product * & head means that head is pass by reference (i.e., when
we change the value of head in the function, like making head points
to other Product structure, the variable that we pass in when calling
the function is also changed.)
1

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

Task 2. initialize_products() File I/O + Linked List - Lets try to load


data from a file into a linked list.

Find the initialize_products() function, it is currently empty.

void initialize_products(Product * & head){

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.

// Call tailInsert() to insert a new product to


the linked list pointed by the pointer head.
tailInsert(head, name, price);
3
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk

4
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk

Task 3. Implement the function tailInsert() to insert a Product node


at the tail of the linked list.

Find the tailInsert() function, it is currently empty.

void tailInsert (Product * & head, string n, double p){

Create a new Product and make that Products next points to NULL.

// Step
Product
newNode
newNode
newNode

1. Create a new node


*newNode = new Product;
-> name = n;
-> price = p;
-> next = ???; // The newNodes next should points to NULL

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.

// Step 2. Add the newNode to the tail of the linked list


if (head != NULL) { // If the list is not empty

}else{ // If the list is 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.

Product *last = findLast(head);

6
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk

Then we update the lasts next to point to the new Node.

last->next = newNode;

If the list is empty, head point to the new Node.

head = newNode;
7
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk

The findLast function is simply a search until we reach the node


with next pointer pointing to NULL.

Product * findLast( Product *head ){


Product *seeker = head;
while (seeker->next != NULL){ // are you the last?
seeker = seeker->next;
}
return seeker;
}

8
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk

Task 4. Show menu

Step 1. Use current as the pointer to traverse the linked list.

void show_menu(Product *head){


Product * current = head;

while ( current != NULL ){

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

Task 5. Remove product(s) by name

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.

void removeProduct(Product * & head, string n){

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.

Product *previous = NULL;


Product *current = head;
while (current != NULL){
if (current->name == n){ // The current node is the node to remove

}else{ // The current node is NOT the node to remove


previous = current;
current = current->next;
}
}

10
Materials designed by Dr. Chui Chun Kit (Kit) for students in ENGG1111. For other uses, please email :
ckchui@cs.hku.hk

Step 3. If the first node is the node to delete, it is a special case as


previous is still pointing to NULL.

if (previous == NULL){ // The node to remove is the first node


head = head->next;
delete current;
current = head; // Move current to point to the 2nd node
}

Step 4. Handle the cases when the node to remove is not the first node.

else{ // The node to remove is NOT the first node


previous->next = current->next;
delete current;
current = previous->next; // Current point to the next node
}

Take home exercise

The purchase() function is updated so that we are asking users to give


the name of the product but not the product ID.
The purchase function will use the double searchProduct(Product
*head, string n) function to return the price of the product with name
equal to n.
11

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

Potrebbero piacerti anche