Sep 11, 2010

Linked List

Guys I hope this code glimpse will help you to get an idea of what is linked list,and how to operate over it.
I'll revise this code with full fledged functionality like sorting etc.. soon. Download link will be available soon.
An example program illustrating the concept of Linked List also will be published shortly.

/***********************************
Program: Linked List Model
Author : Vanangamudi
***********************************/
#include
#include
#include

#define ESC 27

//single node in a list
struct Node
{
    int data;
    int index ;
    struct Node* link;

    public:
    void show(){ cout<<"Index\t:"<<<"\tData\t:"<<
   
    };

//strcuture for contain the nodes -> list
struct List
{
    Node* head;
    Node* tail;
    int last;
   
    public:
    //Constructors
    List()
    { head = new Node;
      head->data=0;
      head->link=NULL;
       
      tail = head; 
        }
           
    //member functions
    void append(int);
    void push(int);
    void insertAt(int,int);
    void show();
   
    int isEmpty();
    int no_of_nodes();
    void index();
   
    Node* delNode(int);

    Node Search(int);

    };

void List::push(int dat)
{
   struct Node* newnode = new Node;
   newnode->data = dat;
  
   if(head->link==NULL){ head->data=dat; }
   else
   {  newnode->link = head;
      head= newnode;
     }
   this->index();
   }

void List::append(int dat)
{
       struct Node* newnode = new Node;
       struct Node* temp=head;
  
       newnode->data=dat;
       newnode->link=NULL;
     
      if(head->data==0){ head->data=dat;  }
      else
      {    while(temp->link!=NULL)
           { temp=temp->link;  }
           temp->link = newnode;
           }
   
   this->index();
   }

void List::insertAt(int dat, int pos)
{
   struct Node* newnode = new Node;
   newnode->data= dat;
   
   Node* prev = new Node;
   struct Node* temp = head;
   while(temp->link!=NULL)
   {
    if(temp->index == pos ) { break;  }
        prev = temp;
        temp=temp->link;
       }
   
   prev->link= newnode;
   newnode->link = temp;
   last++;
   this->index();
    }//insert()

void List::index()
{
     struct Node* temp=head;
     int ind=0;
     while(temp!=NULL)
     {   temp->index=++ind;
         temp=temp->link;
         }
         }     
      
void List::show()
{
   struct Node* temp = head;
  
   while(temp!=NULL)
   {
    temp->show();
    temp= temp->link;
    }
        }   //show()

int List::no_of_nodes()
{
   int count=1;
   struct Node* temp=head;

   while(temp->link!=NULL)
   {
    count++;
    temp=temp->link;
    }
   return(count);
    }//total no of items;

int List::isEmpty()
{  
   if(!no_of_nodes()) { return(1) ; }
   else { return(0);  }
   }

Node List::Search(int dat)
{
   struct Node* temp = head;
   while( temp->data!=dat)
   {temp=temp->link;       }
   struct Node tempp = *temp;
   tempp.link=NULL;
   return(tempp);
   }//srch()

Node* List::delNode(int dat)
{
   struct Node* temp = head;
   if(head->data==dat)
   {
    head=head->link;
    temp->link=NULL;
    this->index();
    return(temp);
     }
   else
   { while(temp->link->data!=dat)
     {   temp=temp->link;   }
     temp->link=temp->link->link;
     this->index();     
     return(temp);   }
    
     }//delNode()
    

0 comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls