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;
}
Usually, circular lists do not work much with the concept of absolute positioning, such as first and last
– Jefferson Quesado
What have you tried? What error did you receive? The idea is jump the item to be deleted before giving a
free
. Ifcount == 1
you point the list toNULL
, otherwise,previousElem->next = current->next
andnextElement->previous = current->previous
. Having implementedremoveInit
,removeFinish
is very similar, only you get fromcurrent->previous
.– Anthony Accioly
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– Jefferson Quesado
Result: [ 40 30 20 10 50 60 70 ] Display Init: 40 Display Finish: 70 removeFinish(List* list); [ 40 30 ]
– Thiago Cunha
List* removeFinish(List* list) {

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

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

 free(p);

 return list;

}
– Thiago Cunha
@Thiagocunha , put this in the question, because it is better to read and, also, improves the quality of publication
– Jefferson Quesado
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
– Thiago Cunha
@Thiagocunha already found the solution, you can answer your own question. This is a good way to contribute to the site.
– Raizant
Thanks @Knautiluz! I answered the solution right below!
– Thiago Cunha