Build a function that removes the last element of the list.

Hello everybody. I have build a code which creates and displays list of cylinders which contains 10 elements. Now my task is to build an additional functions that removes the last element of the list. Could somebody please show me how it could be done with the most basic idea?

Thank You.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <cstdlib>

using namespace std;

typedef struct Cylinder
{
    double height;
    double radius;
    Cylinder *next;
}cylinder;

void create_cylinder(cylinder *any_cylinder)
{
    any_cylinder->height=rand()%101+1;
    any_cylinder->radius=rand()%101+1;
}

void display_list(cylinder *start_list)
{
    cylinder *p1 = start_list;
    while(1)
    {
        cout<<" height= "<<p1->height<<" radius= "<<p1->radius<<endl;
        if(p1->next==0) break;
        p1=p1->next;
    }
}

void add_to_list(cylinder *start_list)
{
    cylinder *p1 = start_list;
    while(1)
    {
        if(p1->next==0) break;
        p1=p1->next;
    }
    cylinder *new_element= new cylinder;
    create_cylinder(new_element);
    new_element->next=0;
    p1->next=new_element;
}

void remove_last_element(cylinder *start_list)
{

}

int main()
{
    cout << "13A Lists" << endl;

    cylinder *cylinder1;
    cylinder1= new cylinder;
    create_cylinder(cylinder1);


    cylinder *start_list;
    start_list= cylinder1;
    start_list->next=0;

     for(int i=1; i<=10; i++)
    {
       add_to_list(start_list);
    }

    display_list(start_list);

    return 0;
}
Since you have a singly linked list, you have to walk the list until you find the last entry (next == 0). Save the pointer to the previous entry as you go. delete prev->next, then set prev->next = 0.
1
2
3
4
5
6
7
8
cylinder **pp, *p;
for (pp = &start_list; p=*pp; pp = &p->next) {
    if (p->next == nullptr) {
        break;
    }
}
delete p;
*pp = nullptr;


Other comments:
- add_to_list only works when the list isn't empty.
- It's inefficient to add to the back of a linked list. You should add to the front instead.
- No need to create the typedef for cylinder. If you want to use cylinder instead of Cylinder then just say struct cylinder { ... };
- create_cylinder() should be cylinder's constructor. It should set the next pointer too.
- Use a for loop for display_list:
1
2
3
    for (cylinder *p1 = start_list; p1; p1 = p1->next) {
        cout<<" height= "<<p1->height<<" radius= "<<p1->radius<<endl;
    }

dhayden, it tells me that nullptr is not declared in this scope. Also I'm not familiar with this operate type. Can you tell me what could the problem be?
In your original code you used 0 as a null pointer constant, which works, but could be confused for an integer. C++11 added nullptr but if you compiler is too old to support it, and you don't feel like upgrading, you could just replace it with 0 (or NULL).
if (p->next == nullptr) {
break;
}

nullptr is checking if next is not set, previously you would compare to NULL, this was introduced in c++11.

You will need to check if you got support for c++11, if not enable or revert back to NULL.


Grunalin wrote:
nullptr is checking if next is not set

You cannot safely compare an uninitialized pointer to see if it's null. It has to be set to null.

With that in mind, it's probably a good idea to also initialize the next pointer in the create_cylinder function (or wherever it belongs).
Topic archived. No new replies allowed.