Using C++ implement the linked list to perform insert, delete and display the data


https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJfShSmc4uzJ32B_Cnh6KDE7EeSGsUpqwnmXpsZMSrMSp0wjgo7gGWZu_JIDHC_zUiWwWkPBhm3Pz_vS0k-tjHPGbd4J7sxNkEVv5q-YbMtId0PtXLQqi2n_k4Dee10jKN6F-IOkz4Wqgg/s1600/c+and+c%252B%252B+meansofmine.jpg
Develop a template of linked list calss and its methods

Aim
To implement the linked list to perform insert, delete and display the data by using c++.

Algorithm

Step 1: Start the Program Execution
Step 2: Create the Classes and declare all the Variables and Member functions.
Step 3: Create the class with LIST..
Step 4: Create a member function for constructor and to insert the element
in the linked list.
Step 5: Use the pointer to refer the next list.
Step 6: Display the list value.
Step 7:  Stop the Program Execution.


Program:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
template<class type>
struct node
{
type data;
node *next;
};
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;
};
template<class type>
void list<type>::display(void)
{
node<type>*cur=linklist;
cout<<"\n The linked list is...\n";
while(cur!=NULL)
{
cout<<cur->data<<"->";
cur=cur->next;
}
cout<<"NULL\n";
}
template<class type>
list<type>::list()
{
count=0;
linklist=NULL;
}
template<class type>
int list<type>::length(void)const
{
return count;
}
template<class type>
void list<type>::makeempty(void)
{
node<type>*temp;
while(linklist!=NULL)
{
temp=linklist;
linklist=linklist->next;
delete temp;
}
count=0;
cout<<"\n Now list is empty";
}
template<class type>
void list<type>::insert(void)
{
node<type>*temp;
type item;
cout<<"\n Enter the item to insert...";
cin>>item;
temp=new node<type>;
temp->data=item;
temp->next=linklist;
linklist=temp;
count++;
}
template<class type>
void list<type>::remove(void)
{
node<type>*cur=linklist;
type item;
cout<<"\n Enter the iten to remove...";
cin>>item;
node<type>*temp;
if(item==linklist->data)
{
temp=cur;
linklist=linklist->next;
}
else
    {
while(!(item==(cur->next->data)))
cur=cur->next;
cur->next=(cur->next)->next;
 }
delete temp;
count--;
}
void main()
{
int ch;
list<int>list1;
clrscr();
while(1)
{
cout<<"\n single linked list-menu\n";
cout<<"\n 1.insert\n2.delete\n3.empty\n4.exit\n";
cout<<"\nEnter your choice...";
cin>>ch;
switch(ch)
{
case 1:
       list1.insert();
       list1.display();
       break;
case 2:
      if(list1.length()>0)
      {
      list1.remove();
      list1.display();
      }
      else
      cout<<"\n list empty";
      break;
case 3:
      list1.makeempty();
      break;
case 4:
      exit(0);
      default:
      cout<<"\n invalid choice\n";
      }
      }
      }



OUTPUT:

singly linked list-menu
1,insert
2.remove
3.empty
4.exit

Enter your choice…1
Enter item to insert…10

single linked list-menu
 1.insert
 2.delete
 3.empty
 4.exit

Enter your choice...2
Enter the item to remove...10

 The linked list is...
NULL

 single linked list-menu
 1.insert
 2.delete
 3.empty
 4.exit

Enter your choice...3
Now list is empty

single linked list-menu
 1.insert
 2.delete
 3.empty
 4.exit

Enter your choice...4

1 comments:

Charl Floor said...

what does cur means , it does not need to declare or what???....please

Post a Comment