program to create and display link list



#include
#include
struct node
{
int data; //data field//
node *next; //pointer to next field//
};
class link_list
{
private:
node *start;
public:
link_list() //constructor//
{
start=NULL;
}
void add_data(int i)
{
node *new_link=new node;
new_link->data=i;
new_link->next=start;
//make new node to print to the first node//

start=new_link;
//make new node as the first node in the list//
}
void display(int no);
void search(int item);
void insloc(int item,int loc);
int find(int item);
};

void link_list::display(int no)
{
int i=1;
int n=no;
cout<<"Element of link list\n";
cout<<"Node no:\t"<<"Node data:\t"<<"Node location:\t"<<"Node link:\t";
node *move=start;
node *temp;
while(move)
{
cout< temp=move;
move=move->next; //move to next node//
if(i==n )
{
cout<data<<"\t"<\t"<<"NULL";
// delete temp;
break;
}

cout<data<<"\t"<\t"< // delete temp;
i++;
}
}

void link_list :: search(int item)
{
node *ptr=start;
while(ptr)
{
if(item>ptr->data)
{
ptr=ptr->next;
}
else
{
if(item==ptr->data)
{
cout<<"element"< return;
}
else
{
cout<<"element not fount";
return;
}
}
}
cout<<"element not found";
}
void link_list::insloc(int item,int loc)
{
node *newlink=new node;
newlink->data=item;
node *ptr=start;
//node *temp;
int i=1;
if(loc==0)
{
newlink->next=start;
start=newlink;
return;
}


while(ptr)
{
if(i==loc)
{
newlink->next=ptr->next;
ptr->next=newlink;
return;
}
i++;
ptr=ptr->next;
}
}
int link_list::find(int item)
{
int loc;
if(start==NULL;)
{
loc=0;
return loc;
}
node *ptr=start;
int save=1;
if(itemdata)
{
loc=0;
return loc;
}
ptr=ptr->next;
while(ptr)
{
if(itemdata)
{
loc=save;
return loc;
}
save++;
ptr=ptr->next;
}
loc=save;
return loc;
}
void main()
{

int i,a[10],item,n,loc;
link_list link;

cout<<"Enter number of elements";
cin>>n;
cout<<"Enter elements\n";
for(i=0;i {
cin>>a[i];
link.add_data(a[i]);
}
link.display(n);
cout<<"\nEnter item to insert";
cin>>item;
/* cout<<"\n enter location to insert";
cin>>loc;*/
loc=link.find(item);
link.insloc(item,loc);
n=n+1;
link.display(n);
getch();
}


No comments:

Post a Comment