Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * DoubleLinkedList.c
- *
- * Created on: Jun 26, 2014
- * Author: Arun
- */
- /*
- * DoubleLinkedList.c
- *
- * Created on: Jun 18, 2014
- * Author: Arun
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include "DoubleLinkedList.h"
- typedef struct DNode mainTemp;
- typedef struct DoubleLinkedList mainList;
- //1 DONE
- DoubleLinkedList* allocDList(uint elementSize){
- struct DoubleLinkedList* list = &mainList;
- list->head = NULL;
- list->tail = NULL;
- list->length = 0;
- list->elementSize = elementSize;
- return list;
- }
- //2 DONE
- void releaseDList(DoubleLinkedList* list){
- struct DNode* node = list->head;
- struct DNode* next = NULL;
- while(node){
- struct DNode* next = node->next;
- free(node);
- node = next;
- }
- }
- //3 DONE
- void insertDListElementAt(DoubleLinkedList* list, Object newElement, uint position){
- struct DNode* newNode = calloc(list, sizeOf(newElement));
- newNode->data = newElement;
- int counter = 0;
- struct _DNode* current = list->head;
- while(counter < list->length){
- if(counter == position){
- current->next = newNode;
- newNode->prev = current;
- newNode->next = current->next;
- current->next->prev = newNode;
- }
- counter++;
- current = current->next;
- }
- }
- //4 DONE
- void appendDList(DoubleLinkedList* list, Object newElement){
- struct DNode* newNode = calloc(list, sizeOf(newElement));
- newNode->data = newElement;
- newNode = list->tail->next; // setting newNode as current tail's next
- newNode->prev = list->tail; // setting up current tail as newNodes's prev
- newNode = list->tail; // newNode as the new tail
- }
- //5 DONE
- void insertDList(DoubleLinkedList* list, Object newElement){
- struct DNode* newNode = (DNode*)calloc(list, sizeOf(newElement));
- newNode->data = newElement;
- newNode->next = list->head;
- newNode = list->head->prev;
- newNode->prev = NULL;
- list->head = newNode;
- }
- //6 DONE
- DoubleLinkedList* reverseDList(DoubleLinkedList* list){
- struct DoubleLinkedList* newList = NULL;
- newList = (struct DoubleLinkedList*) malloc(sizeOf(DoubleLinkedList));
- struct DNode* temp = NULL;
- temp = (DNode*)malloc(sizeOf(DNode));
- temp = list->tail;
- while(temp->prev != NULL){
- appendDList(newList, temp->data);
- temp = temp->prev;
- }
- return newList;
- }
- //7 DONE
- DoubleLinkedList* halfList(DoubleLinkedList* list){
- struct DNode* slow = list->head;
- struct DNode* fast = list->head;
- struct DoubleLinkedList* newList = malloc(uint);
- if(list->head != NULL){
- while(fast != NULL && fast->next != NULL){
- fast = fast->next->next;
- slow = slow->next;
- } // slow now ='s midpoint
- while(slow != NULL){
- appendDList(newList, slow->data);
- slow = slow->next;
- }
- return newList;
- }
- return newList;
- }
- //8 DONE
- Object removeDList(DoubleLinkedList* list, int position){
- int counter = 0;
- struct _DNode* temp = list->head;
- while(counter < list->length){
- if(counter == position){
- temp->prev->next = temp->next;
- temp->next->prev = temp->prev;
- return temp->data;
- }
- counter++;
- temp = temp->next;
- }
- return NULL;
- }
- //9 DONE
- void printDList(DoubleLinkedList* list){
- struct _DNode* temp = list->head;
- while(temp->next != NULL){
- printf("%d", temp->data);
- temp = temp->next;
- }
- }
- void debugDList(DoubleLinkedList* list){
- DNode* iter = list->head;
- while(iter){
- printf("%p\t", iter);
- iter = iter->next;
- }
- printf("\n");
- iter = list->tail;
- while(iter){
- printf("%p\t", iter);
- iter = iter->prev;
- }
- printf("\n\n");
- }
- // end of DoubleLinkedList.c
- // DoubleLinkedList.h
- /*
- * DoubleLinkedList.h
- *
- * Created on: Jun 18, 2014
- * Author: Arun
- */
- #ifndef DOUBLELINKEDLIST_H_
- #define DOUBLELINKEDLIST_H_
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- typedef unsigned int uint;
- typedef unsigned long ulong;
- typedef void* Object;
- typedef struct _DNode{
- Object data;
- struct _DNode* prev;
- struct _DNode* next;
- } DNode;
- typedef struct _DoubleLinkedList{
- DNode* head;
- DNode* tail;
- uint length;
- uint elementSize;
- } DoubleLinkedList;
- //1 DONE
- DoubleLinkedList* allocDList(uint elementSize);
- //2 DONE
- void releaseDList(DoubleLinkedList* list);
- //3 DONE
- void insertDListElementAt(DoubleLinkedList* list, Object newElement, uint position);
- //4 DONE
- void appendDList(DoubleLinkedList* list, Object newElement);
- //5 DONE
- void insertDList(DoubleLinkedList* list, Object newElement);
- //6 DONE
- DoubleLinkedList* reverseDList(DoubleLinkedList* list);
- //7 DONE
- DoubleLinkedList* halfList(DoubleLinkedList* list);
- //8 DONE
- Object removeDList(DoubleLinkedList* list, int position);
- //9 DONE
- void printDList(DoubleLinkedList* list);
- void debugDList(DoubleLinkedList* list);
- #endif /* DOUBLELINKEDLIST_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement