scosant
12/17/2012 - 12:38 PM

doublylinkedlist_insert_and_delete

doublylinkedlist_insert_and_delete

// Copyright 2009, Scott Santarromana
// @file doublylinkedlist_insert_and_delete.c
// @created Aug 26, 2009
// @author Scott Santarromana
 
#include <stdio.h>
#include <stdlib.h>
 
/*************************
 * CONSTANTS AND STRUCTS *
 *************************/
 
const int true = 1;
const int false = 0;
 
typedef struct node {
  struct node* previous;
  struct node* next;
  int data;
} node;
 
/**************
 * PROTOTYPES *
 **************/
 
// Creates and initializes a doubly-linked linked list.
int createDoublyLinkedList (node** head);
 
// Destroys and deallocates the doubly-linked linked list.
int destroyDoublyLinkedList (node** head);
 
// Appends a new node containing "data" at the end of the list.  Returns
// "1" if successful and "0" otherwise.
int Insert (node** head, int data);
 
// Deletes the first encountered occurrence of "deleteMe".  Returns
// "1" if successful and "0" otherwise.
int Delete (node** head, int deleteMe);
 
// Prints the list contents.
void PrintList (node** head);
 
/*******************
 * IMPLEMENTATIONS *
 *******************/
 
int createDoublyLinkedList (node** head) {
  *head = NULL;
  return true;
}
 
int destroyDoublyLinkedList (node** head) {
  node* next;
  while (*head) {
    next = (*head)->next;
    free(*head);
    *head = next;
  }
  return true;
}
 
int Insert (node** head, int data) {
  node* newhead = (node*) malloc(sizeof(node));
  if (!newhead) { // memory allocation for new element failed.
    return false;
  }
  newhead->data = data;
  newhead->previous = NULL;
  newhead->next = *head;
  if (newhead->next) { // list contains at least one additional element
    (newhead->next)->previous = newhead;
  }
  *head = newhead;
  return true;
}
 
int Delete (node** head, int deleteMe) {
  node* currentnode = *head;
  while (currentnode) {
    if (currentnode->data == deleteMe) {
      if (currentnode->previous == NULL) { // first node
        *head = currentnode->next;
        (*head)->previous = NULL;
      } else if (currentnode->next == NULL) { // last node
        (currentnode->previous)->next = NULL;
      } else { // middle node
        (currentnode->previous)->next = currentnode->next;
        (currentnode->next)->previous = currentnode->previous;
      }
      free(currentnode);
      return true;
    }
    currentnode = currentnode->next;
  }
  return false;
}
 
void PrintList (node** head) {
  printf("List:  ");
  node* currentnode = *head;
  printf("%i", currentnode->data);
  currentnode = currentnode->next;
  while (currentnode) {
    printf(", %i", currentnode->data);
    currentnode = currentnode->next;
  }
 
  printf("\n\n");
}
 
/********
 * MAIN *
 ********/
 
int main (int argc, char** argv) {
  node** head;
  createDoublyLinkedList((node**) &head);
 
  printf("Insert elements 1 through 5\n");
  Insert((node**) &head, 1);
  Insert((node**) &head, 2);
  Insert((node**) &head, 3);
  Insert((node**) &head, 4);
  Insert((node**) &head, 5);
  PrintList((node**) &head);
 
  printf("Delete occurrence of 3 (middle node)\n");
  Delete((node**) &head, 3);
  PrintList((node**) &head);
 
  printf("Add two additional occurrences of 9\n");
  Insert((node**) &head, 9);
  Insert((node**) &head, 9);
  PrintList((node**) &head);
   
  printf("Delete occurrence of 9 (first node)\n");
  Delete((node**) &head, 9);
  PrintList((node**) &head);
 
  printf("Delete occurrence of 9 (first node)\n");
  Delete((node**) &head, 9);
  PrintList((node**) &head);
 
  printf("Delete occurrence of 1 (last node)\n");
  Delete((node**) &head, 1);
  PrintList((node**) &head);
   
  printf("Delete occurrence of 20 (nonexistent node)\n");
  Delete((node**) &head, 20);
  PrintList((node**) &head);
 
  destroyDoublyLinkedList((node**) &head);
  return 0;
}