Saturday, July 16, 2011

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 :)

No comments:

Post a Comment