forked from TARANG0503/DSA-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.cpp
47 lines (43 loc) · 904 Bytes
/
linkedlist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* Searching an element in the linked list */
#include<iostream>
#include<conio.h>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
/* Push element in the linked list */
void push(struct Node** head,int x)
{
struct Node* newnode = new Node;
newnode->data = x;
newnode->next = *head;
*head = newnode;
}
/* Searching an element in the linked list */
bool search(struct Node* head,int x)
{
if(head == NULL)
return false;
if(head->data==x)
return true;
return search(head->next,x);
}
int main()
{
struct Node* head = NULL;
int n,x,y;
cout<<"How many numbers?"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter the number:"<<endl;;
cin>>x;
push(&head,x);
}
cout<<"Enter the element to be searched: "<<endl;
cin>>y;
search(head,y)?cout<<"yes":cout<<"No";
return 0;
}