-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.cpp
More file actions
44 lines (38 loc) · 905 Bytes
/
linkedlist.cpp
File metadata and controls
44 lines (38 loc) · 905 Bytes
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
//search an element in a linked list
#include<iostream>
using namespace std;
class node{
public:
int data;
node* next;
// constructor
node(int newdata){
data = newdata;
next = NULL;
}
};
//check whether key is present in the linked list
bool searchkey(node* head, int key) {
node* current = head;
while(current != NULL) {
if(current->data == key) {
return true;
}
current = current->next;
}
return false;
}
int main() {
// created a hard coded linked list
node* head = new node(14);
head->next = new node(15);
head->next->next = new node(16);
head->next->next->next = new node(17);
head->next->next->next->next = new node(18);
int key = 200;
if(searchkey(head, key))
cout << "yes";
else
cout << "no";
return 0;
}