Skip to content

linked list file #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2022
Merged
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
76 changes: 76 additions & 0 deletions linkedlist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <iostream>

/* The program demonstrates a basic linked list implemented using classes */

using namespace std;

class Node
{
public:
int data;
Node *next = NULL;
};

class LinkedList
{
public:
Node *head = NULL;
Node *tail = NULL;
};

void addNode(LinkedList* list, Node* node)
{
if(list->head == NULL && list->tail == NULL) { // For the first node only
list->tail = node;
list->head = node;
}
else {
list->tail->next = node;
list->tail = node;
}
}

void display(LinkedList* list)
{
Node *ptr = list->head;

while (ptr != NULL) { // Loop to traverse the list
cout << ptr->data << " ";
ptr = ptr->next;
}
}

void search(LinkedList* list, int value)
{
Node *ptr = list->head;

while(ptr != NULL) {
if(value == ptr->data) {
cout << "Value exists in the list" << endl;
break;
}
ptr = ptr->next;
}
if(ptr == NULL)
cout << "Value does not exist in the list" << endl;
}

int main()
{
LinkedList example_list;

int arr[] = {5, 8, -2, 66, 78, 21, 90, 0, 2};

for(int i = 0; i < 9; i++) {
Node *temp_node = new Node();
temp_node->data = arr[i];
addNode(&example_list, temp_node);
}

search(&example_list, 78);
search(&example_list, 9);

display(&example_list);

return 0;
}