Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "lista.h"
- typedef struct Node{
- void* dato;
- struct Node* prev;
- struct Node* next;
- } Node;
- typedef struct List{
- Node* first;
- Node* last;
- Node* current;
- } List;
- List* createList(){
- List* L = (List*) malloc(sizeof(List));
- L->first = NULL;
- L->last = NULL;
- L->current = NULL;
- return L;
- }
- void pushFront(List* L, void* data){
- Node* nodo = (Node*) malloc(sizeof(Node));
- nodo->dato = data;
- if(!L->first){
- L->first = nodo;
- L->last = nodo;
- nodo->next = nodo;
- nodo->prev = nodo;
- }
- else{
- nodo->next = L->first;
- nodo->prev = L->last;
- L->last->next = nodo;
- L->first->prev = nodo;
- L->first = nodo;
- }
- }
- void pushBack(List* L, void* data){
- Node* nodo = (Node*) malloc(sizeof(Node));
- nodo->dato = data;
- if(L->first == NULL){
- L->first = nodo;
- L->last = nodo;
- nodo->next = nodo;
- nodo->prev = nodo;
- }
- else{
- nodo->next = L->first;
- nodo->prev = L->last;
- L->last->next = nodo;
- L->first->prev = nodo;
- L->last = L->first->prev;
- }
- }
- void popFront(List* L){
- if(L->first){
- Node* aux=NULL;
- aux = L->first;
- L->first->next->prev = L->last;
- L->last->next = L->first->next;
- L->first = L->first->next;
- free(aux);
- }
- }
- void popBack(List* L){
- if(L->first){
- Node* aux = (Node*) malloc(sizeof(Node));
- aux = L->last;
- L->last->prev->next = L->first;
- L->first->prev = L->last->prev;
- L->last = L->last->prev;
- free(aux);
- }
- }
- void popCurrent(List* L){
- if(L->first){
- Node* aux = (Node*) malloc(sizeof(Node));
- aux = L->current;
- L->current->next->prev = L->current->prev;
- L->current->prev->next = L->current->next;
- if(L->first == L->current){
- L->first=L->first->next;
- }
- L->current = L->current->next;
- free(aux);
- }
- }
- void* First(List* L){
- if(L->first == NULL){
- return NULL;
- }
- else{
- L->current = L->first;
- return L->first->dato;
- }
- }
- void* Last(List* L){
- if(L->last == NULL){
- return NULL;
- }
- else{
- L->current = L->last;
- return L->last->dato;
- }
- }
- void* Next(List* L){
- if(L->first == NULL){
- return NULL;
- }
- else{
- L->current = L->current->next;
- return L->current->dato;
- }
- }
- void* Current(List* L){
- if(L->current == NULL){
- return NULL;
- }
- else{
- return L->current->dato;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement