#include<bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* nextNode;
};
bool searchElement(Node*node, int data)
{
Node* curr=node;
while(curr!=NULL)
{
if(curr->data==data)
{
return true;
}
curr=curr->nextNode;
}
return false;
}
void display(Node *head){
while(head !=NULL){
cout<<head->data<<"=> \t";
head = head->nextNode;
}
}
void insert(Node** head, int data)
{
Node* temp= new Node();
temp->data=data;
temp->nextNode=*head;
*head=temp;
}
int main()
{
Node* head=NULL;
int data;
cout<<"Enter data :"<<endl;
cin>>data;
insert(&head,21);
insert(&head,32);
insert(&head,45);
insert(&head,56);
if(searchElement(head,data)==1){
cout<<"The element already exist\n";
}
else{
cout<<"Element inserted successfully\n";
insert(&head,data);
}
cout<<"The list is: \n";
display(head);
}
Comments
Leave a comment