Program: 15
---------------------------------------------------------------------------------------------------------------------
#include<iostream>
#include<stdio.h>
using namespace std;
typedef struct node
{
int data;
node *next;
}node;
class list
{
//private part of class
int n;
node *first;
//public part of class
public:
//linked list constructor
list()
{
first=NULL;
}
//function for creating linked lists
void create(int a)
{
char ch;
node *current,*temp;
if(first==NULL)
{
first=new node;
first->data=a;
first->next=NULL;
current=first;
}
else
{
n=count();
this->insert(n+1,a);
}
}
//insert function
void insert(int n,int a)
{
int b=count();
if(n<=b+1)
{
node *current,*temp;
current=first;
temp=new node;
temp->data=a;
temp->next=NULL;
if(n==1)
{
temp->next=first;
first=temp;
}
else
{
for(int i=1;i<n-1;i++)
current=current->next;
temp->next=current->next;
current->next=temp;
}
}
else
cout<<"Can't be inserted\n";
}
//search function
void search(int n)
{
int flag=0;
//cout<<"Enter data to be searched:\n";
//cin>>n;
node *current,*prev,*temp;
int b=count();
current=first;
for(int i=1;i<=b;i++)
{
if(current->data==n)
{
flag=1;
break;
}
prev=current;
current=current->next;
}
if(flag==1)
{
int c;
cout<<"Data found:\nEnter what you wannna do:\n 1.delete data\n 2.replace it\n 3.do nothing\n";
cin>>c;
switch(c)
{
case 1:temp=current;
prev->next=current->next;
delete(temp);
cout<<"Data deleted:\n";
break;
case 2:cout<<"Enter new data:\n";
cin>>current->data;
cout<<"data replaced:\n";
break;
case 3:break;
default:cout<<"wrong choice:\n";
}
}
else
cout<<"Data not found:\n";
}
int count()
{
node *current;
int c=0;
current=first;
while(current!=NULL)
{
c++;
current=current->next;
}
return c;
}
//function to display linked list
void display()
{
//getchar();
fflush(stdin);
node *current;
current=first;
cout<<"The data in linked list:\n";
while(current!=NULL)
{
cout<<current->data<<" -> ";
current=current->next;
}
cout<<endl;
}
};
int main()
{
list a[10];
int n;
char ch;
//list l1;
cout<<"Enter the numbers u want to insert(neg number to terminate):\n";
cin>>n;
for(int i=0;n>=0;i++)
{
a[n%10].create(n);
cin>>n;
}
do
{
cout<<"WANT to search any element:\n";
cin>>ch;
if(ch=='y')
{
cout<<"enter the elemnt:\n";
cin>>n;
a[n%10].search(n);
}
}while(ch=='y');
cout<<"all elements r:\n";
for(int i=0;i<10;i++)
{
a[i].display();
}
cout<<"program ended:\n";
}
-------------------------------------------------------------------------------------------------------------------
Program Finish.........