# C + + linked list entry exercise

1. Find program 2 in the comments (insert several nodes in the FRONT of the list). Modify
The program inserts only three (3) nodes in front of the list. Test procedure.

Q1 solution:

Classes and functions remain unchanged. This is one of the main reasons for using functions,
That is, we can change one function (or main function) while leaving everything else unchanged. Change the main function to:

```int main() { // insert at the FRONT of the list
Node *temp;
int number, i;
listpointer = NULL;
for (i = 0; i < 3; i++) {
cout << "Enter an integer value ";
cin >> number;
temp = new Node;
listpointer = temp;
}
displaylist();
}
```
1. Find program 3 in the comment (insert multiple nodes at the BACK of the list). Modify
The program inserts only three (3) nodes after the list. Test procedure.

Q2 solution:

Classes and functions remain unchanged. Change the main to:

```int main() { // insert at the BACK of the list
Node *temp, *lastnode;
int number, i;
listpointer = NULL;
// set up the first node
cout << "Enter the first integer value ";
cin >> number;
listpointer = new Node;
lastnode = listpointer;
// insert the other nodes
for (i = 1; i < 3; i++) { // start from 1
cout << "Enter an integer value ";
cin >> number;
temp = new Node;
lastnode->setnext(temp);
lastnode = temp;
}
displaylist();
}
```
1. Find program 4 in the comment (remove a node from the list). Modify the program to (a) insert
Four (4) nodes in the list, and then (b) delete the third node. Test procedure.

Q3 solution:

Classes and functions remain unchanged. Change the main to:

```int main() {
Node *ptr1, *ptr2, * ptr3, *ptr4, *temp;
int i;
listpointer = NULL;
// insert four nodes in the list
for (i = 0; i < 4; i++) {
temp = new Node;
listpointer = temp;
}
displaylist();
cout << endl;
// now remove the third node
ptr1 = listpointer; // ptr1 points to the first node
ptr2 = ptr1->getnext(); // the second node
ptr3 = ptr2->getnext(); // the third node
ptr4 = ptr3->getnext(); // the fourth node
// change node2 to point to node4
ptr2->setnext(ptr4);
delete ptr3; // delete node3 to free memory
displaylist();
cout << endl;
}
```

4**. Expand program 4 in the note to do the following:
a) Enter five (5) values in the list
b) Enter a value that must be deleted
c) Locate the value and remove it from the list
d) Test program (remember to delete the first or last value)

Q4 solution:

Classes and functions remain unchanged. Change the main to:

```int main() {
Node *temp, *nextnode, *prevnode, *current, *previous;
int searchvalue, currentvalue, i;
bool found;
listpointer = NULL;
// insert five nodes in the list – this could be a function
for (i = 0; i < 5; i++) {
temp = new Node;
listpointer = temp;
}
displaylist();
cout << endl;
// enter the value that must be deleted
cout << "Enter the value to be deleted ";
cin >> searchvalue;
// find the node with the search value
found = false;
previous = NULL;
current = listpointer;
while (current != NULL) {
currentvalue = current->getvalue();
if (currentvalue == searchvalue) {
found = true;
temp = current;
prevnode = previous;
}
previous = current;
current = current->getnext();
}
if (found == false) {
cout << searchvalue << " is not in the list.\n";
exit(2);
}
previous = prevnode;
nextnode = temp->getnext();
if (previous == NULL) { // this is the first node
listpointer = nextnode;
} else {
previous->setnext(nextnode);
}
delete temp; // delete the node to free memory
displaylist();
cout << endl;
}
```
1. Create a class to save the data of domestic flights. This class must store the following information:
Flight number, such as NZ161 (string)
Origin, e.g. Auckland (string)
Destination, e.g. Wellington (string)
Travel time in minutes, e.g. 85 (positive integer)
Create some useful methods, such as loaddata and display.
Using the library, create a list where each node in the list will be a flying object
The class described above.

Q5 solution:

Note – you do not need to provide a complete program.

```class flight_class {
private:
string fltnum, departure, arrival;
int minutes;
public:
void display();
};
list<flight_class> mylist;
//-------------- methods for the flight_class ------------
cout << "Enter the flight number, e.g. NZ161 ";
getline(cin, fltnum);
cout << "Enter the departure place ";
getline(cin, departure);
cout << "Enter the arrival place ";
getline(cin, arrival);
cout << "Enter the travel time in minutes ";
cin >> minutes;
getchar(); // flush the input buffer
}
void flight_class::display() {
cout << "Information about flight " << fltnum << endl;
cout << "Departs from " << departure << endl;
cout << "Arrives at " << arrival << endl;
cout << "Travel time is " << minutes << " minutes.\n";
}
```

6. Use program 5 (using linked list Library) in the notes and the information in question 5
More than. Write a program to read the data of three (3) flights and display the flight details
All three flights. Flight data must be stored in the list
Q6 solution:

```#include <iostream>
#include <cstdio> // for getchar
#include <list>
using namespace std;
class flight_class {
private:
string fltnum, departure, arrival;
int minutes;
public:
void display();
};
void displaylist(list<flight_class> l);
list<flight_class> mylist;
int main() {
int i;
flight_class temp;
for (i = 0; i < 3; i++) {
mylist.push_back(temp);
}
displaylist(mylist);
}
void displaylist(list<flight_class> l) {
list<flight_class>::iterator ptr;
for (ptr = l.begin(); ptr != l.end(); ptr++) {
ptr->display();
}
cout << endl;
}
//-------------- methods for the flight_class ------------
cout << "Enter the flight number, e.g. NZ161 ";
getline(cin, fltnum);
cout << "Enter the departure place ";
getline(cin, departure);
cout << "Enter the arrival place ";
getline(cin, arrival);
cout << "Enter the travel time in minutes ";
cin >> minutes;
getchar(); // flush the input buffer
}
void flight_class::display() {
cout << "Information about flight " << fltnum << endl;
cout << "Departs from " << departure << endl;
cout << "Arrives at " << arrival << endl;
cout << "Travel time is " << minutes << " minutes.\n";
}
```

Keywords: C++ data structure linked list

Added by stubarny on Thu, 23 Dec 2021 09:27:01 +0200