Source Code :
#include<iostream.h>
class node {
public:
int data;
node *link;
};
class linklist {
node *p;
public:
linklist() {
p=NULL;
}
void append(int);
void addatbeg(int);
void addafter(int,int);
void display();
int count();
void del(int);
};
int main( ) {
linklist obj;
int ch,n;
while(true) {
cout<<endl<<"MAIN MENU ";
cout<<endl<<"1. Add Element";
cout<<endl<<"2. Add Element at Beginning";
cout<<endl<<"3. Add Element at Specific Location";
cout<<endl<<"4. Display Linked List";
cout<<endl<<"5. Count no. of Elements";
cout<<endl<<"6. Delete an Element";
cout<<endl<<"7. Exit";
cout<<endl<<"Enter Choice : ";
cin>>ch;
switch(ch) {
case 1: {
cout<<endl<<"Enter Element : ";
cin>>n;
obj.append(n);
}break;
case 2: {
cout<<endl<<"Enter Element : ";
cin>>n;
obj.addatbeg(n);
}break;
case 3: {
int l;
cout<<endl<<"Enter Element : ";
cin>>n;
cout<<endl<<"Enter Location : ";
cin>>l;
obj.addafter(l,n);
}break;
case 4: {
obj.display();
}break;
case 5: {
cout<<endl<<"No. of Element : "<<obj.count();
}break;
case 6: {
cout<<endl<<"Enter Element to be Deleted : ";
cin>>n;
obj.del(n);
}break;
case 7:
exit(0);
default: {
cout<<endl<<"Invalid Choice";
}
}
}
}
void linklist::append(int num) {
node *temp,*r;
if ( p == NULL ) {
temp = new node;
temp -> data = num ;
temp -> link = NULL ;
p = temp ;
}
else {
temp = p ;
while ( temp -> link != NULL ) {
temp = temp -> link ;
}
r = new node;
r -> data = num ;
r -> link = NULL
temp -> link = r ;
}
void linklist::addatbeg (int num) {
node *temp ;
temp =new node;
temp -> data = num ;
temp -> link = p ;
p = temp ;
void linklist::addafter (int loc, int num ) {
node *temp, *r,*q ;
int i ;
temp =p ;
for (i = 0;i<loc-1;i++) {
temp = temp -> link ;
if (temp == NULL) {
cout<<endl<<"There are less than "<<loc<<" elements in list";
return ;
}
}
q = new node ;
q -> data = num ;
q -> link = temp -> link ;
temp -> link = q ;
}
void linklist::display() {
node *q;
q=p;
cout<<endl<<"Elements : ";
while ( q != NULL ) {
cout<<q -> data<<", ";
q = q -> link ;
}
cout<<endl;
}
int linklist::count() {
int c = 0 ;
node *temp;
temp=p;
while ( temp != NULL ) {
temp = temp -> link ;
c++ ;
}
return c ;
}
void linklist::del( int num ) {
struct node *old, *temp ;
temp = p ;
while ( temp != NULL ) {
if ( temp -> data == num ) {
if ( temp == p )
p = temp -> link ;
else
old -> link = temp -> link ;
free ( temp ) ;
return ;
}
else {
old = temp ;
temp = temp -> link ;
}
}
cout<<endl<<"Element "<<num<<" not found";
}
Output :
MAIN MENU
1. Add Element
2. Add Element at Beginning
3. Add Element at Specific Location
4. Display Linked List
5. Count no. of Elements
6. Delete an Element
7. Exit
Enter Choice : 1
Enter Element : 2
MAIN MENU
1. Add Element
2. Add Element at Beginning
3. Add Element at Specific Location
4. Display Linked List
5. Count no. of Elements
6. Delete an Element
7. Exit
Enter Choice : 2
Enter Element : 6
MAIN MENU
1. Add Element
2. Add Element at Beginning
3. Add Element at Specific Location
4. Display Linked List
5. Count no. of Elements
6. Delete an Element
7. Exit
Enter Choice : 3
Enter Element : 4
Enter Location : 1
MAIN MENU
1. Add Element
2. Add Element at Beginning
3. Add Element at Specific Location
4. Display Linked List
5. Count no. of Elements
6. Delete an Element
7. Exit
Enter Choice : 4
Elements : 6, 4, 2,
MAIN MENU
1. Add Element
2. Add Element at Beginning
3. Add Element at Specific Location
4. Display Linked List
5. Count no. of Elements
6. Delete an Element
7. Exit
Enter Choice : 6
Enter Element to be Deleted : 4
MAIN MENU
1. Add Element
2. Add Element at Beginning
3. Add Element at Specific Location
4. Display Linked List
5. Count no. of Elements
6. Delete an Element
7. Exit
Enter Choice : 5
No. of Element : 2
No comments:
Post a Comment