Shared Flashcard Set

Details

Doubly Linked List
double
11
Electrical Engineering
Undergraduate 3
10/05/2012

Additional Electrical Engineering Flashcards

 


 

Cards

Term
Doubly LInked List
Definition

List element contains pointers to next and previous element in list

 

*more flexible for insertion/deletion

*provides ability to traverse list forward and backwards

Term
List Element Code Struct
Definition

typedef struct DListElmt_{

   ListData *data;

   struct DListElmt_ *next;

   struct DListElmt_ *prev;

} DListElmt;

Term
List Struct Code
Definition

typedef struct DList_{

   DListElmt *head;

   DListElmt *tail;

   int size;

} DList;

Term

Initialization Code

Definition

void dlist_init(DList *list){

   list->head = NULL;

   list->tail = NULL;

   list->size = 0;

Term
Destruction Code
Definition

void dlist_destroy(DList *list){

   while(list->size > 0) {

        dlist_remove(list, list->head);

   }

}

Term
Movement Code
Definition

DListElmt *dlist_head(DList *list) {

return list->head;

}

 

DListElmt *dlist_tail(DList *tail){

return list->tail;

}

 

DListElmt *dlist_next(DListElmt *elment){

return element->next

}

prev ^^^^''

Term
Size Code
Definition

int dlist_size(DList *list){

    return list->size;

}

Term
Insertion (psuedo-code)
Definition

int dlist_insert_next(DList *list, DListElmt *element, ListData *data)

 

if element is null -> error

malloc new element

assign new element data

empty list proof

else make it work

increase size

return success

Term
Insertion Code
Definition

int dlist_ins_next(DList *list, DListElmt *element, ListData *data){

 

if (element == NULL && dlist_size(list) != 0) return -1;

//no null elements

 

if ((new_element = (DListElmt *)malloc(sizeof(DListElmt))) == NULL) return -1;

//allocate storage

 

new_element->data = (void *)data; //insert new element into list

 

if (dlist_size(list) == 0) {   //if list is empty

   list->head = new_element;

   list->head->prev = NULL;

   list->head->next = NULL;

   list->tail = new_element;

}

else{                           //not empty list

   new_element->next = element->next;

   new element->prev = element;

     if(element->next == NULL)

          list->tail = new_element;

     else{

          element->next->prev = new_element;}

    element->next = new_element;

}//end of else

 

list->size++;

return 0;

}

 

Term

Remove psuedo Code

 

*ths dlist_remove operation removes a specified element from a doubly linked list

Definition

int list_remove(DList *list, DListElmt *element);

-if element is null or list empty ->error msg

-if element is head

  -set head to next element

  -if list is empty noe

     -set tail to NULL

  -else

    -set next elements prev pointer to NULL

-else

  -set previous element's next pointer to element's next pointer

  -if element is tail

      -set tail to previous element

  -else

      -set next elements prev pointer to elements prev pointer

-free element's memory (and data)

-decrease size

 

Term

Remove Code

 

Definition

int dlist_remove(DList *list, DListElmt *element, void **data){

 

if(element == NULL || dlist_size(list) == 0)

  return -1;

 

*data = element->data;

 

if (element == list->head) { //removal from head

   list->head = element->next;

 

   if (list->head == NULL)

       list->tail == NULL ;

   else {

      element->prev->next = element->next;

      if(element->next == NULL)

            list->tail = element->prev;

     else

            element->next->prev = element->prev;

   }

 

free(element);

 

list->size--;

 

return 0;

}

 

Supporting users have an ad free experience!