//Insertion element at middle of the SLL (or) Insertion element at ith node
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
template<class T>
class sll;
template<class T>
class node
{
private:
T data;
node<T>* link;
friend class sll<T>;
};
template <class T>
class sll
{
private:
node<T> * first;
public:
//Mem.Fun declarations
sll();
int isempty();
void display();
int length();
void append(node<T> *k);
void create();
void insert(int i, T x);
};
//Member Functions defination
template<class T>
sll<T>::sll()
{
first=NULL;//first points to no where,it is an empty node
}
template<class T>
int sll<T>::isempty()
{
return(first==NULL);//return true when linked list is empty and false otherwise
}
template<class T>
void sll<T>::display()
{
node<T>* p;//p is pointer to a node
if(isempty())
cout<<"Linked List is empty\n";
else
{
cout<<"Linked list is as follows\n";
p=first;
while(p!=NULL)
{
cout<<p->data<<"\t"<<p->link<<"\n";
p=p->link;
}
}//end of else
}//end of display()
template<class T>
void sll<T>::append(node<T> *k)
{
node<T> *last;
k->link=NULL;
if(isempty())
first=k;
else
{
last=first;
while(last->link!=NULL)//keep moving last pointer until last node is reached
{
last=last->link;
}
last->link=k;//attach last node and new node k
}
}
template <class T>
void sll<T>::create()
{
T x;
node<T> *k;
first=NULL;
cout<<"enter values terminated by -1 or !\n";
cin>>x;
while(x!=-1&&x!='!')
{
k=new node<T>;
k->data=x;
append(k);
cin>>x;
}
}//end of create()
//inserting data at ith node
template<class T>
void sll<T>::insert(int i, T x)
{
node<T> *p,*temp;
if(i<0||i>length())
{
cout<<"Non-Existing Node\n";
}
else
{
p=new node<T>;
//store data into the node
p->data=x;
if(i= =0) //insert at the begining or Linked list is empty
{
p->link=NULL;
first=p;
}
else //insert in the middle or at the end of the Linked List
{
temp=first;
//move the temp ptr to ith node (desired node)
for(int j=1;j<=i-1;j++)
temp=temp->link;
//insert new node p after temp node
p->link=temp->link;
temp->link=p;
} //inner else
}//outer else
}//end of insert()
void main()
{
sll<int> a;//creating object a, d.c is called
clrscr();
int i,x;
a.create();
cout<<"enter which position you want to insert(ith node value)\n";
cin>>i;
cout<<"enter data to be inserted\n";
cin>>x;
a.insert(i,x);
a.display();
getch();
}
output:
Singly Linked List Operations
Types of Linked list
Dobule Linked List
Circular Linked List
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
template<class T>
class sll;
template<class T>
class node
{
private:
T data;
node<T>* link;
friend class sll<T>;
};
template <class T>
class sll
{
private:
node<T> * first;
public:
//Mem.Fun declarations
sll();
int isempty();
void display();
int length();
void append(node<T> *k);
void create();
void insert(int i, T x);
};
//Member Functions defination
template<class T>
sll<T>::sll()
{
first=NULL;//first points to no where,it is an empty node
}
template<class T>
int sll<T>::isempty()
{
return(first==NULL);//return true when linked list is empty and false otherwise
}
template<class T>
void sll<T>::display()
{
node<T>* p;//p is pointer to a node
if(isempty())
cout<<"Linked List is empty\n";
else
{
cout<<"Linked list is as follows\n";
p=first;
while(p!=NULL)
{
cout<<p->data<<"\t"<<p->link<<"\n";
p=p->link;
}
}//end of else
}//end of display()
template<class T>
void sll<T>::append(node<T> *k)
{
node<T> *last;
k->link=NULL;
if(isempty())
first=k;
else
{
last=first;
while(last->link!=NULL)//keep moving last pointer until last node is reached
{
last=last->link;
}
last->link=k;//attach last node and new node k
}
}
template <class T>
void sll<T>::create()
{
T x;
node<T> *k;
first=NULL;
cout<<"enter values terminated by -1 or !\n";
cin>>x;
while(x!=-1&&x!='!')
{
k=new node<T>;
k->data=x;
append(k);
cin>>x;
}
}//end of create()
//inserting data at ith node
template<class T>
void sll<T>::insert(int i, T x)
{
node<T> *p,*temp;
if(i<0||i>length())
{
cout<<"Non-Existing Node\n";
}
else
{
p=new node<T>;
//store data into the node
p->data=x;
if(i= =0) //insert at the begining or Linked list is empty
{
p->link=NULL;
first=p;
}
else //insert in the middle or at the end of the Linked List
{
temp=first;
//move the temp ptr to ith node (desired node)
for(int j=1;j<=i-1;j++)
temp=temp->link;
//insert new node p after temp node
p->link=temp->link;
temp->link=p;
} //inner else
}//outer else
}//end of insert()
void main()
{
sll<int> a;//creating object a, d.c is called
clrscr();
int i,x;
a.create();
cout<<"enter which position you want to insert(ith node value)\n";
cin>>i;
cout<<"enter data to be inserted\n";
cin>>x;
a.insert(i,x);
a.display();
getch();
}
output:
Singly Linked List Operations
Types of Linked list
Dobule Linked List
Circular Linked List
No comments:
Post a Comment