Implementing Linked list Data structure in C++ Example Source Code

How to Implement Linked list Data structure in C++ Example Source Code

#include <iostream.h>
#include <conio.h>
#include <string.h>

class node
{
friend class list;
private:
int data ;
node* next ;
};

class list

{
private:
node* first;
node* last ;
public:
list()
{
first = NULL;
last = NULL;
}

void add_node()
{
if (first==NULL)

{
first = new node ;
cout << "\n Enter integer for node: " ;
cin >> first->data ;
first->next = NULL ;
last = first ;
}

else
{
node* temp = new node ;
cout << "\n Enter integer for node: ";
cin >> temp->data ;
temp->next = NULL ;
last->next = temp ;
last = temp ;
}

}

void display_all_nodes()
{
node* temp = first ;
int small = temp->data ;
while(temp != last->next)
{
if (temp->data < small)
small = temp->data ;
cout << endl << temp->data ;
temp = temp->next ;
}
cout << "\n The smallest number is : " << small ;

}
int length() ;

void remove_a_node_interface() ;

void remove_a_node(int s) ;

void add_new_node(node*) ;

void mixing(list l1,list l2) ;

int smallest();

};


void list::remove_a_node_interface()
{
cout << "\n Enter data of the node to delete";
int string;
cin >> string;
remove_a_node(string);
}

void list::remove_a_node(int s)
{

node* temp = first;
node* previous = first ;

while(temp !=last->next)
{
if (temp->data==s)
{
if(temp==first)
{
first = first->next ;
delete temp ;
break;
}

if(temp==last )
{
last = previous ;
delete temp ;
break;
}

temp = temp->next ;
delete (previous->next) ;
previous->next = temp ;
}

previous = temp ;
temp = temp->next ;
}

}

int list::length()
{
int count = 0;
node* temp = first ;
while (temp!= NULL )
{
count++;
temp=temp->next;
}
cout << "\n The length of list is: " << count ;
return count ;
}


void list::add_new_node(node* helper)
{
if (first)
{
first = helper ;
last = helper ;
last->next = NULL ;
}
else
{
last->next= helper ;
last = helper ;
last->next=NULL;
}
}

void list::mixing(list l1, list l2)
{
node* mixer1 = l1.first ;
node* mixer2 = l2.first ;
while (mixer1 && mixer2)
{
add_new_node(mixer1);
add_new_node(mixer2);
mixer1 = mixer1->next;
mixer2 = mixer2->next;
}

while(mixer1)
{
add_new_node(mixer1);
mixer1 = mixer1->next ;
}

while(mixer2)
{
add_new_node(mixer2);
mixer2 = mixer2->next;
}


}



int list::smallest()
{
node* temp = first ;
int small = temp->data ;
while(temp != last->next)
{
if (temp->data < small)
small = temp->data ;
temp = temp->next ;
}
cout << "\n The smallest number is : " << small ;

return small ;
}




void main()
{
list link1;
list link2;
list link3;
link1.add_node();
link1.add_node();
link1.add_node();
link1.add_node();
link1.add_node();
link1.display_all_nodes();
link1.remove_a_node_interface();
link1.display_all_nodes();
link1.length();
link2.add_node();
link2.add_node();
link2.add_node();
link2.add_node();
link2.add_node();
link2.display_all_nodes();
link2.remove_a_node_interface();
link2.display_all_nodes();
link2.length();
//cout << link1.smallest();
//cout << link2.smallest();
link3.mixing(link1,link2);
link3.length();
link3.display_all_nodes();
getch();
}