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();
}

C++ implementation of the SinglyLinkedList

I am providing a implementation of SinglyLinkedList in C++. It's done in code blocks.I will posting the double linked list as next post.


//SinglyLinkedList.h




#ifndef SINGLYLINKEDLIST_H
#define SINGLYLINKEDLIST_H


struct node{
char *load;
node *next;
};


class SinglyLinkedList
{
    public:
    node *head;
    SinglyLinkedList();
    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 // SINGLYLINKEDLIST_H



//SinglyLinkedList.cpp



#include <iostream>
#include "include/SinglyLinkedList.h"
#include "malloc.h"
using namespace std;
SinglyLinkedList::SinglyLinkedList()
{
    //ctor
    head = NULL;
}


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


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


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


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




}


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


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


void SinglyLinkedList::deleteAtIndex(int index){


   node *ref = head;
   if(index == 0){
        if(head != NULL){
            head = head ->next;
            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;
       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 SinglyLinkedList :: getLength(){
    node *ref = head;
    int i=0;
    while(ref != NULL){
    i++;
    ref = ref->next;
    }
    return i;


}




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










//int main(){
// SinglyLinkedList 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();
//}


Please give your comments :)

Monday, July 11, 2011

Basics of LinkedList



Hi, This blog is about linked list. Mostly i won't talk, i will try to get the wordings that sounds good to understand. I will give the references at the end of the blog so that you can take a look.




A linked list is a data structure that can hold an arbitrary number of data items, and can easily change size to add or remove items. A linked list, at its simplest, is a pointer to a data node. Each data node is then composed of data (possibly a record with several data values), and a pointer to the next node. At the end of the list, the pointer is set to null.
By nature of its design, a linked list is great for storing data when the number of items is either unknown, or subject to change. However, it provides no way to access an arbitrary item from the list, short of starting at the beginning and traversing through every node until you reach the one you want. The same is true if you want to insert a new node at a specific location. It is not difficult to see the problem of inefficiency.


They can be used to implement several other common abstract data structures, including stacks, queues, associative arrays, and symbolic expressions, though it is not uncommon to implement the other data structures directly without using a list as the basis of implementation.


Linear and circular lists


In the last node of a list, the link field often contains a null reference, a special value that is interpreted by programs as meaning "there is no such node". A less common convention is to make it point to the first node of the list; in that case the list is said to be circular or circularly linked; otherwise it is said to be open orlinear.

A circular linked list

Singly, doubly, and multiply linked lists

Singly linked lists contain nodes which have a data field as well as a next field, which points to the next node in the linked list.

A singly linked list whose nodes contain two fields: an integer value and a link to the next node

In a doubly linked list, each node contains, besides the next-node link, a second link field pointing to the previous node in the sequence. The two links may be called forward(s) and backwards, ornext and prev(ious).

A doubly linked list whose nodes contain three fields: an integer value, the link forward to the next node, and the link backward to the previous node

The technique known as XOR-linking allows a doubly linked list to be implemented using a single link field in each node. However, this technique requires the ability to do bit operations on addresses, and therefore may not be available in some high-level languages.

In a multiply linked list, each node contains two or more link fields, each field being used to connect the same set of data records in a different order (e.g., by name, by department, by date of birth, etc.). (While doubly linked lists can be seen as special cases of multiply linked list, the fact that the two orders are opposite to each other leads to simpler and more efficient algorithms, so they are usually treated as a separate case.)

In the case of a doubly circular linked list, the only change that occurs is the end, or "tail" of the said list is linked back to the front, "head", of the list and vice versa.