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();
}


Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!