-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL2.cpp
More file actions
56 lines (45 loc) · 1.09 KB
/
Copy pathL2.cpp
File metadata and controls
56 lines (45 loc) · 1.09 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
// Search an element
#include<iostream>
#include<stdlib.h>
using namespace std;
struct Node
{
int key;
struct Node* next;
};
void push(struct Node** head_ref, int new_key)
{
/* allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct 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;
}
bool search(struct Node* head, int x)
{
struct Node* current = head; // Initialize current
while (current != NULL)
{
if (current->key == x)
return true;
current = current->next;
}
return false;
}
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
/* 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, 11)? cout<<"Yes" : cout<<"No";
return 0;
}