Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions data_structures/linked_list/searchelemnet_in_linkedlist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <iostream>
using namespace std;

// Node structure
struct Node {
int data;
Node* next;
};

// Function to insert a new node at the end
void insert(Node*& head, int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = nullptr;

if (head == nullptr) {
head = newNode;
return;
}

Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}

// Function to search for an element in the linked list
bool search(Node* head, int key) {
Node* current = head;
while (current != nullptr) {
if (current->data == key)
return true;
current = current->next;
}
return false;
}

// Function to display the linked list
void display(Node* head) {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}

// Main function
int main() {
Node* head = nullptr;
int n, value, key;

cout << "Enter number of elements: ";
cin >> n;

cout << "Enter elements:\n";
for (int i = 0; i < n; i++) {
cin >> value;
insert(head, value);
}

cout << "Linked List: ";
display(head);

cout << "Enter element to search: ";
cin >> key;

if (search(head, key))
cout << "Element " << key << " found in the list." << endl;
else
cout << "Element " << key << " not found in the list." << endl;

return 0;
}