Output |
#include <iostream>
using namespace std;
struct node {
int info;
node* link;
}*start,*nNode;
node * createNew(int in) {
nNode = new node;
nNode->info=in;
return (nNode);
}
node display(node* temp) {
while(temp!=NULL) {
cout<<temp->info<<" ";
temp=temp->link;
}
cout<<"\n";
}
node * search(node* tempPtr,int item) {
while(tempPtr!=NULL && tempPtr->info!=item) {
tempPtr=tempPtr->link;
}
return (tempPtr);
}
int main()
{
int n=2,s,item;
int a[8]={7,9,8,5,4,6,2,1};
start=NULL;
node* tempPtr;
for (int i = 0; i < 8; ++i)
{
if(start==NULL) {
start=createNew(a[i]);
start->link=NULL;
} else {
nNode=createNew(a[i]);
nNode->link=start;
start=nNode;
}
}
cout<<"Elements of linked list: \n";
display(start);
cout<<"Enter element you want to search: ";
cin>>item;
tempPtr=search(start,item);
if (tempPtr!=NULL && tempPtr->info==item) {
cout<<"Item found in list.\n";
} else {
cout<<"Item doesn't exists in list.\n";
}
return(0);
}
This comment has been removed by the author.
ReplyDelete