12
ENGG 1111B Computer Programming and Applications Lab tutorial 10 Pointers, struct and Linked list In this tutorial, let’s make all the doubts about pointers and linked list clear! Using linked list to handle student records Task 1. Pointer aria!le ma" also pass !" alue # pass !" re$erence% struct Product{  string name;  double price; Product *next; }; void initialize_products(P roduct * & head); void show_menu(Product * head); double purchase(Product * head); void checout(double); void tail!nsert (Product * & head" string n" double p); Product * #ind$ast( Product *head ); void removeProduct(Prod uct * & head" string n); &uestion' What is Product * & head % Product * head  means that head is a pointer to Product structure. Product * & head  means that head is pass by refer ence (i.e., when we chane the alue of head in the function, like makin head points to other Product structure, the ariable that we pass in when callin the function is also chaned." 1 #aterials desined by $r. %hui %hun &it ( (it" for students in '))1111. *or other uses, please email + ckchuics.hku.hk  

Lab 10 Question Paper

Embed Size (px)

Citation preview

7/24/2019 Lab 10 Question Paper

http://slidepdf.com/reader/full/lab-10-question-paper 1/12

ENGG 1111B Computer Programming and Applications

Lab tutorial 10

Pointers, struct and Linked list

In this tutorial, let’s make all the doubts about pointers and linked listclear!

Using linked list to handle student records

Task 1. Pointer aria!le ma" also pass !" alue # pass !" re$erence%

struct Product{

  string name;

  double price;

Product *next;

};

void initialize_products(Product * & head);void show_menu(Product * head);

double purchase(Product * head);

void checout(double);

void tail!nsert (Product * & head" string n" double p);

Product * #ind$ast( Product *head );

void removeProduct(Product * & head" string n);

&uestion' 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 chane the alue of head in the function, like makin head points

to other Product structure, the ariable that we pass in when callin

the function is also chaned."

1

#aterials desined by $r. %hui %hun &it ((it" for students in '))1111. *or other uses, please email +

ckchuics.hku.hk 

7/24/2019 Lab 10 Question Paper

http://slidepdf.com/reader/full/lab-10-question-paper 2/12

-

#aterials desined by $r. %hui %hun &it ((it" for students in '))1111. *or other uses, please email +

ckchuics.hku.hk 

7/24/2019 Lab 10 Question Paper

http://slidepdf.com/reader/full/lab-10-question-paper 3/12

Task ). initialize_products()  *ile +# - Linked List Let/s tr" to load

data $rom a 0le into a linked list.

• *ind the initialize_products() function, it is currently empty.

void initialize_products(Product * & head){

}

• tep 1. tandard steps to open a /le and create an i#stream obect #in.

  i#stream #in;

  #inopen('product_listtxt');

  i# ( #in#ail() ){

  cout 'rror in opening the #ile ' endl;

  exit(+); // Force the program to terminate 

}else{

  // Load data from file, and insert to linked list

  }

  #inclose();

• tep ). Load the whole line from the /le into a strin ariable line 

(sin getline() without the 2rd input parameter can read the whole lineincludin space" .

string name;

  double price;

  while (#in ,, name){

  #in ,, price;

  }

  tep 2. With name and price, let’s insert a new node to the tail of the

linked list with the function tail!nsert(). We will implement the

tail!nsert() function in the ne3t task. 

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

the linked list pointed by the pointer head.

  tail!nsert(head" name" price);

2

#aterials desined by $r. %hui %hun &it ((it" for students in '))1111. *or other uses, please email +

ckchuics.hku.hk 

7/24/2019 Lab 10 Question Paper

http://slidepdf.com/reader/full/lab-10-question-paper 4/12

4

#aterials desined by $r. %hui %hun &it ((it" for students in '))1111. *or other uses, please email +

ckchuics.hku.hk 

7/24/2019 Lab 10 Question Paper

http://slidepdf.com/reader/full/lab-10-question-paper 5/12

Task 2. +mplement the $unction tailInsert() to insert a Product node

at the tail o$ the linked list.

• *ind the tail!nsert() function, it is currently empty.

void tail!nsert (Product * & head" string n" double p){

}

• %reate a new Product and make that Product’s next points to -.$$.

  // tep !. Create a new node

  Product *new-ode / new Product;

  new-ode 0, name / n;

new-ode 0, price / p;

  new-ode 0, next / %%%; // "he new#ode$s ne%t should points to #&LL 

5

#aterials desined by $r. %hui %hun &it ((it" for students in '))1111. *or other uses, please email +

ckchuics.hku.hk 

7/24/2019 Lab 10 Question Paper

http://slidepdf.com/reader/full/lab-10-question-paper 6/12

 

3e need to handle t4o cases' 4hen the list is empt" # not empt".

  // tep '. dd the new#ode to the tail of the linked list

i# (head / -.$$) { // If the list is not empty 

  }else{ // If the list is empty 

  }

• If the list is not empty, we make a pointer 6last, and point it to the last

node in the list. 7ssume that we will build a #ind$ast() function to return

the address of the last 8roduct in the linked list.

  Product *last / #ind$ast(head);

9

#aterials desined by $r. %hui %hun &it ((it" for students in '))1111. *or other uses, please email +

ckchuics.hku.hk 

7/24/2019 Lab 10 Question Paper

http://slidepdf.com/reader/full/lab-10-question-paper 7/12

•  :hen we update the last’s next to point to the new ode.

  last0,next / new-ode;

 

+$ the list is empt", head point to the ne4 Node.

  head / new-ode;

;

#aterials desined by $r. %hui %hun &it ((it" for students in '))1111. *or other uses, please email +

ckchuics.hku.hk 

7/24/2019 Lab 10 Question Paper

http://slidepdf.com/reader/full/lab-10-question-paper 8/12

 

The 0ndLast $unction is simpl" a search until 4e reach the node

4ith ne5t pointer pointing to NULL.

Product * #ind$ast( Product *head ){

  Product *seeer / head;

  while (seeer0,next / -.$$){ 11 are 2ou the last%

  seeer / seeer0,next;

  }

  return seeer;

}

<

#aterials desined by $r. %hui %hun &it ((it" for students in '))1111. *or other uses, please email +

ckchuics.hku.hk 

7/24/2019 Lab 10 Question Paper

http://slidepdf.com/reader/full/lab-10-question-paper 9/12

Task 6. ho4 menu

• tep 1. se current as the pointer to traerse the linked list.

void show_menu(Product *head){

  Product * current / head;

  while ( current / -.$$ ){

  current / current0,next;

  }

 }

• tep ). 8rint out the name and price of each Product structure pointed

to by the pointer current.

ote that current is a pointer to Product structure, so we should use

0, to access the member ariable of the Product structure pointed to

by current.

  cout current0,name ' 3' current0,price endl;

=

#aterials desined by $r. %hui %hun &it ((it" for students in '))1111. *or other uses, please email +

ckchuics.hku.hk 

7/24/2019 Lab 10 Question Paper

http://slidepdf.com/reader/full/lab-10-question-paper 10/12

Task 7. 8emoe product9s: !" name

• tep 1. Let’s traerse the whole list, and remoe the node if the

Product4s name is e>ual to n.

ince users will call the removeProduct() function by passin in the

head ariable, which is the pointer to the /rst node in the linked list,and we may update head to point to other nodes (if the /rst node is

the node to delete", we need to pass by reference.

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

}

• tep ). se a while loop to traerse the list, use previous and current 

to point to the preious node and current node when we traerse the list.

  Product *previous / -.$$;

  Product *current / head; 

while (current / -.$$){

  i# (current0,name // n){ // "he current node is the node to remoe

  }else{ // "he current node is #*" the node to remoe

  previous / current;

  current / current0,next;

  }

  }

10

#aterials desined by $r. %hui %hun &it ((it" for students in '))1111. *or other uses, please email +

ckchuics.hku.hk 

7/24/2019 Lab 10 Question Paper

http://slidepdf.com/reader/full/lab-10-question-paper 11/12

• tep 2. If the /rst node is the node to delete, it is a special case as

preious is still pointin to LL.

i# (previous // -.$$){ // "he node to remoe is the first node

  head / head0,next;  delete current;

  current / head; // +oe current to point to the 'nd node

  }

• tep 6. ?andle the cases when the node to remoe is not the /rst node.

else{ // "he node to remoe is #*" the first node

  previous0,next / current0,next;

  delete current;

  current / previous0,next; // Current point to the ne%t node

  }

Take home e5ercise

•  :he purchase() function is updated so that we are askin users to ie

the name of the product but not the product I$.

•  :he purchase function will use the double searchProduct(Product

*head" string n) function to return the price of the product with name

e>ual to n.

11

#aterials desined by $r. %hui %hun &it ((it" for students in '))1111. *or other uses, please email +

ckchuics.hku.hk 

7/24/2019 Lab 10 Question Paper

http://slidepdf.com/reader/full/lab-10-question-paper 12/12

tudy the code in the proram and see if you understand the implementation.

1-

#aterials desined by $r. %hui %hun &it ((it" for students in '))1111. *or other uses, please email +

ckchuics.hku.hk