Wednesday 26 September 2012

Print data at only even location in single linked list

#include<conio.h>
#include<iostream.h>
struct node
{
int data;
struct node *link;
};
void main()
{
int i,n,d;
struct node *temp,*ptr,*start=NULL;
temp=new node;
temp->data=10;
temp->link=NULL;
start=temp;
cout<<"How many node do u want:-";
cin>>n;
for(i=1;i<=n;i++)
{
cin>>d;
 temp=new node;
temp->data=d;
temp->link=start;
start=temp;
}

cout<<"data is:-"<<endl;
for(temp=start;temp!=NULL;temp=temp->link)
{
cout<<" "<<temp->data<<" ";
}

cout<<endl<<"data print at only  even location "<<endl;


temp=start;
while(temp!=NULL)
{
for(i=1;i<=2;i++)
{
ptr=temp;
temp=temp->link;
}
cout<<" "<<ptr->data<<" ";
}

getch();
}

No comments:

Post a Comment