-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchinLinkedList.cpp
More file actions
61 lines (48 loc) · 1.07 KB
/
SearchinLinkedList.cpp
File metadata and controls
61 lines (48 loc) · 1.07 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
using namespace std;
/* Link list node */
class Node
{
public:
int key;
Node* next;
};
void push(Node** head_ref, int new_key)
{
/* allocate node */
Node* new_node = new Node();
/* put in the key */
new_node->key = new_key;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Checks whether the value x is present in linked list */
bool search(Node* head, int x)
{
Node* current = head; // Initialize current
while (current != NULL)
{
if (current->key == x)
return true;
current = current->next;
}
return false;
}
/* Driver program to test count function*/
int main()
{
/* Start with the empty list */
Node* head = NULL;
int x = 21;
/* Use push() to construct below list
14->21->11->30->10 */
push(&head, 10);
push(&head, 30);
push(&head, 11);
push(&head, 21);
push(&head, 14);
search(head, 21)? cout<<"Yes" : cout<<"No";
return 0;
}