pop_front() :
void pop_front(List *list) {
Node *temp;
if (!size(list)) {
printf("\n List is empty.\n");
exit(0);
}
temp = list->head;
list->head = list->head->next;
list->head->prev = NULL;
free(temp);
list->size--;
}
pop_back() :
void pop_back(List *list) {
Node *temp;
if (!size(list)) {
printf("\n List is empty.\n");
exit(0);
}
temp = list->tail;
list->tail = list->tail->prev;
list->tail->next = NULL;
free(temp);
list->size--;
}
The structures are :
typedef long long int data_type;
typedef struct NodeStruct {
struct NodeStruct *prev;
data_type data;
struct NodeStruct *next;
} Node;
typedef struct ListStruct{
Node *head;
Node *tail;
size_t size;
} List;
And main() function is :
#include <stdio.h>
#include "list.h"
int main() {
List list;
data_type i;
system("clear");
set_list(&list);
for (i = 1; i <= 10; i++) {
push_back(&list, i);
}
display_forward(&list);
printf("\n Size : %u\n", size(&list));
display_backward(&list);
printf("\n");
for (i = 1; i <= 10; i++) {
pop_front(&list);
}
return 0;
}
If needed I will provide the other functions used in main().
Output :
1 2 3 4 5 6 7 8 9 10 Size : 10 10 9 8 7 6 5 4 3 2 1 Segmentation fault

New Topic/Question
Reply




MultiQuote






|