Circular List Double chained in C/C++

Asked

Viewed 1,616 times

1

I’m having trouble removing the first and last element of this circular list.

// Created by Thiago Cunha on 25/05/2017.
//

#include "double_linked_list_circular.h"

int count = 0;

List* init() {
    return NULL;
}

List* insertInit(List* list, int data) {

    List* temp = (List *) malloc (sizeof(List));
    temp->data = data;

    if (isEmpty(list)) {
        temp->previous = temp;
        temp->next = temp;
        return temp;
    }

    temp->next = list;
    temp->previous = list->previous;
    list->previous->next = temp;
    list->previous = temp;
    list = temp;

    return list;

}

List* insertFinish(List* list, int data) {

    List* temp = (List *) malloc (sizeof(List));
    temp->data = data;


    if (isEmpty(list)) {
        temp->previous = temp;
        temp->next = temp;
        return temp;
    }

    temp->next = list;
    list->previous->next = temp;
    temp->previous = list->next;
    list->previous = temp;

    return list;

}

void displayInit(List* list) {
    cout << "Display Init: " << list->data << endl;
}

void displayFinish(List* list) {
    cout << "Display finish: " << list->previous->data << endl;

}

void display(List* list) {

    if (isEmpty(list)) {
        cout << "Your list is empty." << endl;
        return;
    }

    List* p = list;
    cout << "[ ";
    do {
        cout << p->data << " ";
        p = p->next;
    } while (p != list);
    cout << " ]" << endl << endl;

}

List* removeInit(List* list) {




}

List* removeFinish(List* list) {



}


bool isEmpty(List* list) {
    return list == NULL;
}

int size() {
    return count;
}

void toIncrease() {
    ++count;
}

void toDesincrease() {
    --count;
}
  • 2

    Usually, circular lists do not work much with the concept of absolute positioning, such as first and last

  • 2

    What have you tried? What error did you receive? The idea is jump the item to be deleted before giving a free. If count == 1 you point the list to NULL, otherwise, previousElem->next = current->next and nextElement->previous = current->previous. Having implemented removeInit, removeFinishis very similar, only you get from current->previous.

  • 1

    I recommend you start this implementation with the function removeElemento(List *elemento_a_ser_removido), to then implement the "start" and "end" removal on that function

  • Result: [ 40 30 20 10 50 60 70 ] Display Init: 40 Display Finish: 70 removeFinish(List* list); [ 40 30 ]

  • List* removeFinish(List* list) {&#xA;&#xA; if (isEmpty(list)) {&#xA; return NULL;&#xA; }&#xA;&#xA; List* p = list->previous;&#xA; list->previous = p->previous;&#xA; list->previous->next = list;&#xA;&#xA; free(p);&#xA;&#xA; return list;&#xA;&#xA;}

  • 1

    @Thiagocunha , put this in the question, because it is better to read and, also, improves the quality of publication

  • I got it here! Thank God! Was having trouble inserting the end... so, as amazing as it may seem, the problem happened at the end remove. Here is the code below: https://github.com/codenome/double-circular-linked-list

  • 1

    @Thiagocunha already found the solution, you can answer your own question. This is a good way to contribute to the site.

  • 1

    Thanks @Knautiluz! I answered the solution right below!

Show 4 more comments

1 answer

1


I was able to solve the removal problem of the last element.

List* removeFinish(List* list) {

if (isEmpty(list)) {
    return NULL;
}

if ( (list->previous == list) && (list->next == list) ) {
    free(list);
    return NULL;
}

List* p = list->previous;
p->previous->next = list;
list->previous = p->previous;

free(p);

cout << "Data was removed with successfully!" << endl;

return list;
}

To access the entire code, come with me on this link and be sure to mark your star on Github!

  • 1

    Laughs a lot of the "come with me on this link"!!!

  • 1

    I found it very strange that you mix C++ (cout) with C (free). Not that this is wrong, but it’s best to always try to use as much as the language gives you. If I’m not mistaken, to free up memory in C++ is delete

  • RSRSR... Me too! I don’t even know if you can make distractions like this when answering. But thank you! :)

  • 1

    give a humorous air to the post is a stylistic issue. I for example did this here, more to try to pass a lesson than for another reason. Having the correct content of the answer, I do not see much offense in giving this stylistic touch in the form of the answer. PS: The touch of humor I used was to try to control myself and not to swear at the AP ;-)

  • @Jeffersonquesado. Thanks for the remark! So, following the term good practice, it’s wrong! But there are injections of C within C++ that can cover this, which is a good performance practice when mixing syntax. I’m learning C++ a little deeper. But the memory release is working anyway! The important thing is that my gambit worked!

  • 1

    It does work because you allocated with C resources (malloc and family, in case you used only the malloc). It’s a question of how much language can do for you. In this sense, C is very raw, does not support you at all, whereas C++ gives you much more support than you are used to. There are some stops with garbage collection with smart pointers that I believe liberates memory for you. It is worth trying =)

  • 1

    See more: https://answall.com/q/248539/64969

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.