#include <iostream>
using namespace std;
struct node
{
int num;
node *nextptr;
}*stnode; //node defined
void makeList(int n);
void showList();
void searchList(int item, int n);
int main()
{
int n,num,item;
cout<<"Enter the number of nodes: ";
cin>>n;
makeList(n);
cout<<"\nLinked list data: \n";
showList();
cout<<"\nEnter element you want to search: ";
cin>>item;
searchList(item,n);
return 0;
}
void makeList(int n) //function to create linked list.
{
struct node *frntNode, *tmp;
int num, i;
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode == NULL)
{
cout<<" Memory can not be allocated";
}
else
{
cout<<"Enter the data for node 1: ";
cin>>num;
stnode-> num = num;
stnode-> nextptr = NULL; //Links the address field to NULL
tmp = stnode;
for(i=2; i<=n; i++)
{
frntNode = (struct node *)malloc(sizeof(struct node));
if(frntNode == NULL) //If frntnode is null no memory cannot be allotted
{
cout<<"Memory can not be allocated";
break;
}
else
{
cout<<"Enter the data for node "<<i<<": "; // Entering data in nodes.
cin>>num;
frntNode->num = num;
frntNode->nextptr = NULL;
tmp->nextptr = frntNode;
tmp = tmp->nextptr;
}
}
}
}
void showList() //function to print linked list
{
struct node *tmp;
if(stnode == NULL)
{
cout<<"No data found in the list";
}
else
{
tmp = stnode;
cout<<"Linked List: ";
while(tmp != NULL)
{
cout<<"\t"<num;
tmp = tmp->nextptr;
}
}
}
void searchList(int item , int n) //function to search element in the linked list
{
struct node *tmp;
int i=0,flag;
tmp = stnode;
if(stnode == NULL)
{
cout<<"\nEmpty List\n";
}
else
{
while (tmp!=NULL)
{
if(tmp->num == item) //If element is present in the list
{
cout<<"Item found at location: "<<(i+1); flag=0; } else { flag++; } i++; tmp = tmp -> nextptr;
}
if(flag==n) //If element is not present in the list
{
cout<<"Item not found\n";
}
}
}
Output:
Enter the number of nodes: 5
Enter the data for node 1: 41
Enter the data for node 2: 25
Enter the data for node 3: 36
Enter the data for node 4: 96
Enter the data for node 5: 58
Linked list data:
Linked List: 41 25 36 96 58
Enter element you want to search: 36
Item found at location: 3
Login/Signup to comment