Guys I have written the code for Linked List in C++ using OOP concept in a simple way.
#include<iostream>
#include<conio.h>
using namespace std;
class Lis
{
private:
int data;
Lis *next , *start;
int head ;
public:
Lis()
{
data = 0;
next = NULL;
head = 0;
start = NULL;
}
void add()
{
Lis *p = new Lis();
char ch;
do
{
if (head == 0)
{
start = p;
p -> next = NULL;
head = 1;
}
else
{
Lis *tmp = new Lis();
p -> next = tmp;
tmp ->next = NULL;
p = tmp;
}
cout << "ENTER DATA TO NODE " <<endl;
cin >> p -> data;
cout<<"WANT TO ENTER ANOTHER NODE (Y/N) " <<endl;
cin>>ch;
}while(ch =='Y');
}
void addfront()
{
Lis *tmp = new Lis();
cout<<"ENTER DATA " << endl;
cin>> tmp -> data;
tmp -> next = start;
start = tmp;
display();
cout<<" NODE SUCCESSFULLY ADDED IN FRONT " << endl;
cin.get();
}
void del()
{
int loc = 0;
cout << " ENTER THE LOCATION FROM WHICH YOU WANT TO DELETE NODE " <<endl;
cin >> loc;
Lis *tmp;
tmp = start;
for(int i = 1 ; i < loc - 1 ; i ++ )
{
tmp = tmp -> next;
}
Lis *save = tmp;
tmp = tmp -> next;
save -> next = tmp -> next;
delete(tmp);
display();
cout << "NODE SUCCCESSFULLY DELETED " << endl;
cin.get();
}
void display()
{
Lis *show;
show = start;
while(show)
{
cout<<show -> data <<endl;
show = show -> next;
}
}
};
int main()
{
//Lis obj;
Lis obj;
int x = 0;
char ch = 'Y' ;
do
{
clrscr();
cout << "Press 1 for making a List " << endl;
cout << "Press 2 for adding node at beginning " << endl;
cout << "Press 3 for deleting a node from List " << endl;
cout << "Press 4 for display the List " << endl;
cout << "Press 5 to Exit " << endl;
x = 0;
cout <<"Enter Choice "<< endl;
cin >> x;
switch (x)
{
case 1:
obj.add();
break;
case 2:
obj.addfront();
break;
case 3:
obj.del();
break;
case 4:
obj.display();
break;
case 5:
ch = 'N';
break;
default:
cout << " Enter Valid Choice " << endl;
}
}while (ch == 'Y' );
return 0;
}
By: Saurabh Bhatia
#include<iostream>
#include<conio.h>
using namespace std;
class Lis
{
private:
int data;
Lis *next , *start;
int head ;
public:
Lis()
{
data = 0;
next = NULL;
head = 0;
start = NULL;
}
void add()
{
Lis *p = new Lis();
char ch;
do
{
if (head == 0)
{
start = p;
p -> next = NULL;
head = 1;
}
else
{
Lis *tmp = new Lis();
p -> next = tmp;
tmp ->next = NULL;
p = tmp;
}
cout << "ENTER DATA TO NODE " <<endl;
cin >> p -> data;
cout<<"WANT TO ENTER ANOTHER NODE (Y/N) " <<endl;
cin>>ch;
}while(ch =='Y');
}
void addfront()
{
Lis *tmp = new Lis();
cout<<"ENTER DATA " << endl;
cin>> tmp -> data;
tmp -> next = start;
start = tmp;
display();
cout<<" NODE SUCCESSFULLY ADDED IN FRONT " << endl;
cin.get();
}
void del()
{
int loc = 0;
cout << " ENTER THE LOCATION FROM WHICH YOU WANT TO DELETE NODE " <<endl;
cin >> loc;
Lis *tmp;
tmp = start;
for(int i = 1 ; i < loc - 1 ; i ++ )
{
tmp = tmp -> next;
}
Lis *save = tmp;
tmp = tmp -> next;
save -> next = tmp -> next;
delete(tmp);
display();
cout << "NODE SUCCCESSFULLY DELETED " << endl;
cin.get();
}
void display()
{
Lis *show;
show = start;
while(show)
{
cout<<show -> data <<endl;
show = show -> next;
}
}
};
int main()
{
//Lis obj;
Lis obj;
int x = 0;
char ch = 'Y' ;
do
{
clrscr();
cout << "Press 1 for making a List " << endl;
cout << "Press 2 for adding node at beginning " << endl;
cout << "Press 3 for deleting a node from List " << endl;
cout << "Press 4 for display the List " << endl;
cout << "Press 5 to Exit " << endl;
x = 0;
cout <<"Enter Choice "<< endl;
cin >> x;
switch (x)
{
case 1:
obj.add();
break;
case 2:
obj.addfront();
break;
case 3:
obj.del();
break;
case 4:
obj.display();
break;
case 5:
ch = 'N';
break;
default:
cout << " Enter Valid Choice " << endl;
}
}while (ch == 'Y' );
return 0;
}
By: Saurabh Bhatia
No comments:
Post a Comment