user9366862
user9366862

Reputation:

Searching for a particular value in a single linked list

I am trying to implement a generic single linked list. So far I have everything done correctly but I cannot get my search function to work properly. It should print "yes"in the output but nothing happens.

Here is my code:

#ifndef LinkedList_hpp
#define LinkedList_hpp

#include <iostream>

template<class T>
struct Node {
    T data;
    Node<T>* next;
};

template<class T>
class SingleLinkedList {
private:
    Node<T>* head;
    Node<T>* tail;

public:
    SingleLinkedList() {
        head = nullptr;
        tail = nullptr;
    }

    void createNode(const T& theData) {
        Node<T>* temp = new Node<T>;
        temp->data = theData;
        temp->next = nullptr;
        if(head == nullptr) {
            head = temp;
            tail = temp;
            temp = nullptr;
        }
        else {
            tail->next = temp;
            tail = temp;
        }
    }

    void display() {
        Node<T>* temp = new Node<T>;
        temp = head;
        while(temp != nullptr) {
            std::cout << temp->data << "\t";
            temp = temp->next;
        }
    }

    void insert_start(const T& theData) {
        Node<T>* temp = new Node<T>;
        temp->data = theData;
        temp->next = head;
        head = temp;
    }

    void insert_position(int pos, const T& theData) {
        Node<T>* previous = new Node<T>;
        Node<T>* current = new Node<T>;
        Node<T>* temp = new Node<T>;
        current = head;
        for(int i  = 1; i < pos; i++) {
            previous = current;
            current = current->next;
        }
        temp->data = theData;
        previous->next = temp;
        temp->next = current;
    }

    void delete_first() {
        Node<T>* temp = new Node<T>;
        temp = head;
        head = head->next;
        delete temp;
    }

    void delete_last() {
        Node<T>* previous = new Node<T>;
        Node<T>* current = new Node<T>;
        current = head;
        while(current->next != nullptr) {
            previous = current;
            current = current->next;
        }
        tail = previous;
        previous->next = nullptr;
        delete current;
    }

    void delete_position(int pos) {
        Node<T>* previous = new Node<T>;
        Node<T>* current = new Node<T>;
        current = head;
        for(int i = 1; i < pos; i++) {
            previous = current;
            current = current->next;
        }
        previous->next = current->next;
    }

    bool search(Node<T>* head, int x) {
        struct Node<T>* current = head;  
        while (current != NULL) {
            if (current->data == x)
                return true;
            current = current->next;
        }
        return false;
    }
};

#endif /* LinkedList_hpp */

Here is the main.cpp:

#include <iostream>
#include "LinkedList.hpp"


int main(int argc, const char * argv[]) {

    SingleLinkedList<int> obj;
    obj.createNode(2);
    obj.createNode(4);
    obj.createNode(6);
    obj.createNode(8);
    obj.createNode(10);
    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"---------------Displaying All nodes---------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.display();

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"-----------------Inserting At End-----------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.createNode(55);
    obj.display();

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"----------------Inserting At Start----------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.insert_start(50);
    obj.display();

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"-------------Inserting At Particular--------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.insert_position(5,60);
    obj.display();

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"----------------Deleting At Start-----------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.delete_first();
    obj.display();

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"-----------------Deleing At End-------------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.delete_last();
    obj.display();

    std::cout<<"\n--------------------------------------------------\n";
    std::cout<<"--------------Deleting At Particular--------------";
    std::cout<<"\n--------------------------------------------------\n";
    obj.delete_position(4);
    obj.display();

    std::cout<<"\n--------------------------------------------------\n";
    system("pause");

    Node<int>* head = NULL;
    obj.search(head, 8) ? printf("Yes") : printf("No");


    return 0;
}

You can see that I search for the value 8 and it should print yes since 8 is still in the linked list.

Upvotes: 1

Views: 179

Answers (2)

The Dark
The Dark

Reputation: 8514

This line:

Node<int>* head = NULL;

mean you are passing NULL into your search method.

Your search method should not take a head parameter at all - head is already a member of your class.

Also note: you have lots of memory leaks like this:

Node<T>* temp = new Node<T>;
temp = head;

You don't need to allocate memory for a pointer that you are just going to overwrite with a new value, just use

Node<T>* temp = head;

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409166

This is your search function:

bool search(Node<T>* head, int x) {
    struct Node<T>* current = head;  
    while (current != NULL) {
        if (current->data == x)
            return true;
        current = current->next;
    }
    return false;
}

It uses the argument head passed into as the first argument, instead of the SingleLinkedList<T>::head member variable.

And since you call it passing a null pointer as the first argument, you will not find anything.

Simple fix: Remove the first argument from the search function:

bool search(T x) { ... }

As you see I also changed the argument of the value you search for to the template type.

Upvotes: 1

Related Questions