Sie sind auf Seite 1von 13

IMPLEMENTATION OF LINKED LIST

USING TEMPLATES

1.Declare a structure named as node
to implement the node of a linked list.

struct node
{
type data;
node *next;
};

2.Define a class named as list and
Declare it as a class template.

template<class type>
class list
{
public:
list();
int length(void)const;
void makeempty(void);
void insert(void);
void remove(void);
void display(void);
private:
node<type>* linklist;
int count;
};

3. In main ()
Declare list1 as an object of type class list and
pass int as an template parameter.
Int type list class is generated.
void main()
{
int ch;
list<int>list1;


4. Print the menu and Read the value
of ch

while(ch!=4)
{
cout<<"\n Single Linked List \n\tMenu";
cout<<"\n 1.Insert \n 2.Delete\n 3.Empty\n
4.Exit\n";
cout<<"\nEnter your Choice:";
cin>>ch;

5. ch=1
Invoke the member function insert through the object
list1to insert the integer element into the linked list.

switch(ch)
{
case 1:
list1.insert();


template<class type>
void list<type>::insert(void)
{
node<type> *temp;
type item;
cout<<"\nEnter the item to be inserted:";
cin>>item;
temp=new node<type>;
temp->data=item;
temp->next=linklist;
linklist=temp;
count++;
}

Invoke the member function display through
the object list1 to display the elements of the
linked list.
switch(ch)
{
case 1:
list1.insert();
list1.display();
break;

template<class type>
void list<type>::display(void)
{
node<type> *cur = linklist;
cout<<"\nThe linked list is \n";
while(cur!=NULL)
{
cout<<setprecision(2)<<cur-
>data<<"->";
cur=cur->next;
}
cout<<"NULL\n";
}

6.ch=2 Invoke the member fun length thro list1 and
check whether the value returned by the function
length is greater than 0 or not.

case 2:
if(list2.length()>0)
{

template<class type>
Int list<type>::length(void)const
{
return count;
}

If it is > 0, Invoke the member fun remove
through the object list1 to remove the element
from the linked list.
if(list2.length()>0)
{
list2.remove();

template<class type>
void list<type>::remove(void){
node<type> *cur=linklist;
type item;
cout<<"\nEnter the item to be removed:";
cin>>item;
node<type> *temp;
if(item==linklist->data)
{
temp=cur;
linklist=linklist->next;
}
Else {
while(!(item==(cur->next->data)))
cur=cur->next;
temp=cur->next;
cur->next=(cur->next)->next;
}
delete temp;
count--;
}

Invoke the member function display through
the object list1 to display the elements of the
linked list.
else do the following
Print list is empty

7. If ch=3 then perform the following
(i) Invoke the member fun makeempty through
the object list1
case 3:
list1.makeempty();
break;

template<class type>
void list<type>::makeempty(void)
{
node<type> *temp;
while(linklist!=NULL)
{
temp=linklist;
linklist=linklist->next;
delete temp;
}
count=0;
cout<<"\nNow the list is empty";
}

8. If ch=4,then use break statement to
come out from the switch block
case 4:
break;
default:
cout<<"\nInvalid Choice\n";

Declare list2 as an object of type class list
and pass float as an template argument.

Float type list class is generated and perform
the same operations as mentioned above

list<float>list2;
while(1)
{
cout<<"\n Single Linked List \n\t Menu";
cout<<"\n 1.Insert \n 2.Delete\n 3.Empty\n 4.Exit\n";
cout<<"\nEnter your Choice:";
cin>>ch;

Das könnte Ihnen auch gefallen