Saturday, July 16, 2011

C++ implementation of the DoubleLinkedList

//DoubleLinkedList.h



#ifndef DOUBLELINKEDLIST_H
#define DOUBLELINKEDLIST_H


struct node{
char *load;
node *next;
node *prev;
};
class DoubleLinkedList
{
    public:
    node *head;
    DoubleLinkedList();
    void addAtHead(char *str);
    void addAtTail(char *str);
    void addAtIndex(char *str,int index);
    void deleteAtHead();
    void deleteAtTail();
    void deleteAtIndex(int index);
    int getLength();
    void printAllValues();
    protected:
    private:
};


#endif // DOUBLELINKEDLIST_H


//DoubleLinkedList.cpp

#include "include/DoubleLinkedList.h"
#include <iostream>
#include "malloc.h"
using namespace std;

DoubleLinkedList::DoubleLinkedList()
{
    head = NULL;
}

void DoubleLinkedList::addAtHead(char *str){
    addAtIndex(str,0);
}

void DoubleLinkedList::addAtTail(char *str){
    addAtIndex(str,getLength());
}

void DoubleLinkedList::addAtIndex(char *str,int index){

   node *ref = head;
   if(index == 0){
       node *newVal = (node *)malloc(sizeof(node));
       newVal->load = str;
       newVal->next = head;
       newVal->prev = NULL;
       head = newVal;
       return;
   }
   int i=0;
   while(ref!= NULL){
    if(i == index - 1){
       node *newVal = (node *)malloc(sizeof(node));
       newVal->load = str;
       newVal->next = ref->next;
       newVal->prev = ref;
       ref->next = newVal;
       if(newVal->next != NULL)
       newVal->next->prev = newVal;
       return;
    }
    i++;
    ref = ref->next;
   }
   int no = getLength();
   cout<<"\nError: Invalid index this linked list has only "<<no<<" element(s)";


}

void DoubleLinkedList::deleteAtHead(){
    deleteAtIndex(0);
}

void DoubleLinkedList::deleteAtTail(){
    deleteAtIndex(getLength()-1);
}

void DoubleLinkedList::deleteAtIndex(int index){

   node *ref = head;
   if(index == 0){
        if(head != NULL){
            head = head ->next;
            head->prev = NULL;
            cout<<"\nThe element at index "<<index<<" with value "<<ref->load<<" is deleted";
            free(ref);
        }
       return;
   }
   int i=0;
   while(ref!= NULL){
    if(i == index - 1){
       if(ref->next != NULL){
       node* r = ref->next;
       ref->next = r->next;
       if(ref->next != NULL)
       ref->next->prev = ref;
       cout<<"\nThe element at index "<<index<<" with value "<<r->load<<" is deleted";
       free(r);
       }
       return;

    }
    i++;
    ref = ref->next;
   }
   int no = getLength();
   cout<<"\nError: Invalid index this linked list has only "<<no<<" element(s)";

}

int DoubleLinkedList :: getLength(){
    node *ref = head;
    int i=0;
    while(ref != NULL){
    i++;
    ref = ref->next;
    }
    return i;

}


void DoubleLinkedList :: printAllValues(){
    node *ref = head;
    int i=0;
    while(ref != NULL){
    cout<<"\nElement "<<i++<<" : "<<ref->load;
    ref = ref->next;
    }
}





int main(){
 DoubleLinkedList list;
 list.addAtHead("test1");
 list.addAtHead("test2");
 list.addAtHead("test3");
 list.addAtTail("test4");
 list.addAtIndex("test5",2);
 list.printAllValues();
 list.deleteAtHead();
 list.printAllValues();
 list.deleteAtTail();
 list.printAllValues();
 list.deleteAtIndex(1);
 list.printAllValues();
}

No comments:

Post a Comment