Wednesday, 23 January 2013

linked list all functions

complete linked list functions by me
#include<iostream>
#include<conio.h>
using namespace std;
class Node{
int data;
Node *next;
public:
Node(){
data=0;
next=NULL;
}
void set_data(int da){
data=da;
}
int get_data(){
return data;
}
void set_next(Node *n)
{
next=n;
}
Node *get_next(){
return next;
}
};
//***********************************************List class******************************************//
class list{
Node *head;
Node *current;
public:
list(){
head=NULL;
current=NULL;
}

//***********************************************Add_first******************************************//
void insert(int a){
Node *temp=new Node();
temp->set_data(a);
if(head==NULL){
head=temp;
}
else{
temp->set_next(head); head=temp;

}
}
//***********************************************Add Last******************************************//
void insert_last(int a){
Node *temp1=new Node();
temp1->set_data(a); if(head==NULL){
head=temp1;
}
else{
current=head;
while(current->get_next()!=NULL){
current=current->get_next();
}
current->set_next(temp1);
temp1->set_next(NULL);

}
}
//**********************************************Add location********************************************//
void add_location(int a,int key){
Node *temp2=new Node();
temp2->set_data(a);
if(head==NULL){
cout<<"list is empty"<<endl;
}
else{
current=head;
while(current->get_data()!=key){
current=current->get_next();
}
temp2->set_next(current->get_next());
current->set_next(temp2);
}
}
//*************************************************** Return a value through index**************************//
int return_v(int a){
Node *temp3=new Node();
temp3->set_data(a);
if(head==NULL){
cout<<"list is empty"<<endl;
}
else{
current=head;
for(int i=1;i<a;i++){
current=current->get_next();
}
return current->get_data();
}
}void swap(int a){
Node *current==head;
Node *temp5=head;
if(current->get_data()==a)
{
current=current->set_next();
         
}
   temp5=current->get_next( );
}
//***********************************************Print function******************************************//
void print(){
current=head;
while(current!=NULL){
cout<<"List is"<<current->get_data()<<endl;
current=current->get_next();
}
}
};
void main(){
list obj;
obj.insert(1);
obj.insert(4);
obj.insert(6);
obj.insert(3);
obj.insert(2);
obj.insert(7);
cout<<"add_first"<<endl;
obj.insert_last(9);
obj.insert_last(8);
cout<<"add_last"<<endl;
obj.add_location(3,6);
cout<<" "<<endl;
for(int i=0;i<9;i++){
int a=0;
cout<<"enter the list index u want to get value "<<endl;
cin>>a;
int c=obj.return_v(a);
cout<<c;
}
obj.print();
obj.swap(3);
}
For link:plz visit this link;

https://www.google.com/search?num=10&hl=en&site=imghp&tbm=isch&source=hp&biw=1241&bih=556&q=add+first+in+linked+list&oq=add+first+in+linked+list&gs_l=img.3...9819.18768.0.19364.24.12.0.12.0.1.1662.4477.1j6j1j0j1j2j8-1.12.0...0.0...1ac.1.RX8XYyyYBR4#hl=en&tbo=d&site=imghp&tbm=isch&sa=1&q=add+node+at+lastin+singly+linked+list&oq=add+node+at+lastin+singly+linked+list&gs_l=img.3...232607.233826.2.234139.7.6.0.0.0.0.267.533.2-2.2.0...0.0...1c.1.vvwgfEReJBE&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.&fp=4a96d70726ca0141&biw=1241&bih=517