Wednesday, February 27, 2008

LinkList or linked list --- An easy way to implement it

Linked list or linklist has always been the favorite Data structure of all the interviewers. So here is the easiest way to implement it( I think so..) :o)

Njoy Coding!!!


Linked list or linklist has always been the favorite Data structure of all the interviewers. So here is the easiest way to implement it( I think so..) :o)

Njoy Coding!!!


///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
// //
// Linklist program by vaibhav chugh //
// Platform : windows vista business ed //
// Language : VC++ / std cpp //
// Date : 2/25/2008 //
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

#include
using namespace std;

class linklist
{
private:
struct node
{
int data;
node *next;
}*p;
public:
linklist(); //--- 1
int count_nodes(); //--- 2 count the nodes in the link list
void addLinkbegin(int d); //--- 3
void add_at_end(int d); //--- 4
void add_at_pos(int d,int pos); //--- 5
void del_node_num(int pos); //--- 6
void del_node_val(int d); //--- 7
void showLinks(); //--- 8
void sortLinklist(); //--- 9
void addvalues(); //---10 // Sum all the values in the linklist

};

linklist::linklist() //--------------1
{
cout<<"\n Link said Hi";
p=NULL;
}
/////////////////////////////
// 2 Counting nodes
////////////////////////////
int linklist::count_nodes()
{

node *t;
int cnt=0;
t=p;
if(p==NULL)
{
cnt=0;
}
else
{
while(t != NULL)
{
cnt++;
t=t->next;
}

}
return cnt;
}

//////////////////////////////////
// 3 add link begin
////////////////////////////////
void linklist::addLinkbegin(int d)
{
node *q = new node;

q->data=d;
q->next = p;
p=q;
cout<<"\n I guess I added link at beginning=="<data;
}

///////////////////////////////////
// 4 Add at end
//////////////////////////////////
void linklist::add_at_end(int d)
{
node *q,*t;

if(p==NULL)
{
p = new node;
p->data = d;
p->next=NULL;
}
else
{
t=p;

while(t->next !=NULL)
t=t->next;
q= new node;
q->data = d;
q->next=NULL;
t->next=q;
}
}

/////////////////////////////////////////
// 5 add at position
////////////////////////////////////////
void linklist::add_at_pos(int d, int pos)
{
node *t,*q;
int cnt=1;
int check=0;
if(pos ==1)
{
cout<<"\n calling add begin";
this->addLinkbegin(d);

}
else
{
cout<<"\n Search pos to add";
t=p;
while(cnt < pos-1)
{
t=t->next;
if(t==NULL)
{
cout<<"\nNot many nodes in the linklist";
check=1;
break;
}
cnt++;
}
if(check == 0)
{
q= new node;
q->data=d;
q->next=t->next;
t->next=q;
}
}
}

////////////////////////////////////////
// 6 del node number
///////////////////////////////////////
void linklist::del_node_num(int pos)
{
node *t;
int cnt=1;
t=p;
if(p==NULL)
cout<<"\n no node here";
else
{
while(cnt < pos)
{
t=t->next;
if(t==NULL)
{
cout<<"\n Not many nodes to delete the one u want";
break;
}
cnt++;
}
t->next=t->next->next;
}
}

///////////////////////////////////////
// 7. delete node with value
///////////////////////////////////////
void linklist::del_node_val(int d)
{
node *t;
t=p;
if(p==NULL)
cout<<"\n No node present";
else
{
while(t->next !=NULL)
{
if(t->next->data == d)
{
t->next=t->next->next;
}
else
t=t->next;
}
}
}

////////////////////////////////////
// 8. show link list
///////////////////////////////////
void linklist::showLinks()
{
node *q;
q=p;
while(q != NULL)
{
cout<<"->> "<data;
q=q->next;
}
cout<<"\n M done printing";

}

/////////////////////////////////////
//9. Sort a link list
///////////////////////////////////
void linklist::sortLinklist()
{
int cnt=0,i=0,j=0;
node *current,*nxt,*prev;
current=p;
prev=p;
nxt=p->next;
cnt=this->count_nodes();
for(i=0;i {
for(j=0;j {
if(current->data > nxt->data)
{
current->next = nxt->next;
nxt->next = current;
if(current==p)
{
p=nxt;
prev=nxt;
}
else
{
prev->next=nxt;
prev=nxt;
}
if(nxt != NULL)
nxt=current->next;
}
else
{
prev=current;
current=nxt;
nxt=current->next;
}
}
current=p;
prev=p;
nxt=current->next;
}

cout<<"\nSorted List ";
current=p;
while(current!=NULL)
{
cout<<"->"<data;
current=current->next;
}
}
///////////////////////////////////
//10. Adding values
//////////////////////////////////

void linklist::addvalues()
{
node *temp;
temp = p;
int sum=0;

while(temp != NULL)
{
sum+=p->data;
temp=temp->next;
}
cout<<"Sum = "<
}
////////////////////////////////////
// MAIN
///////////////////////////////////
void main()
{
linklist ll;
ll.addLinkbegin(33);
ll.addLinkbegin(23);
ll.addvalues();
ll.addLinkbegin(13);
ll.addLinkbegin(33);
ll.addLinkbegin(63);
ll.add_at_end(76);
ll.add_at_end(70);
ll.add_at_end(98);
ll.add_at_pos(45,4);
ll.add_at_pos(46,1);
ll.add_at_pos(909,34);
ll.showLinks();
cout<<"\nCount now = "<ll.del_node_num(5);
ll.showLinks();
ll.del_node_val(13);
ll.del_node_val(33);
ll.showLinks();
ll.sortLinklist();
ll.showLinks();
ll.addvalues();
cout<<"\n count now ="<
}