Create an app using LinkedList (pass an objects into the linkedlist) C++

Viewed 26

I wrote a LinkedList.h (it should be generic so I use templates). Then now, I try to create an app which takes two data's(maybe string or int, two of them are possible) from user then store them into the LinkedList. Normally I can use this app with store these two data's seperately but the problem comes in when I try to store them with in an object(these two data's inside an object, and one pointer should point one object). I dont sure; can I store data's in objects with using LinkedList structure but I have to implement this app with LinkedList pass with objects. Here my code's. I felt confused...

LinkedList.h (BELOW)

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;

template <class T> class Node {
public:
    T data;
    Node<T>* next_ptr;

    Node() {                                                        //Default Constructor
        data = "";  //Here I got error, data cant be equal zero     //In default, data should be zero and *****
        next_ptr = NULL;                                            //next pointer should be NULL
    }

    Node(T data) {              //Default Constructor with parameter
        this->data = data;      //Given data is Node's data
        this->next_ptr = NULL;  //Next is NULL
    }
};

template <class T> class LinkedList {
    Node<T>* head_ptr;              //head of linked list

public:
    LinkedList() {                  //Default Constructor
        head_ptr = NULL;            //In default, head should be NULL
    }

    void addItem(T&);                   //For adding item to the list

    void showItem();                    //For showing items in the list
};

template <class T> void LinkedList<T>::addItem(T& new_data) {
    Node<T> *new_node = new Node<T>();  //new node creating
    Node<T> *last_ptr = head_ptr;       //last is head now, to set node's location

    new_node->data = new_data;      //new_node's data set to new_data
    new_node->next_ptr = NULL;      //new_node's next_ptr set to the NULL

    if (head_ptr == NULL) {         //if head is NULL,
        head_ptr = new_node;        //new_node should be head
        return;
    }

    while (last_ptr->next_ptr != NULL) { //In this loop, last_ptr
        last_ptr = last_ptr->next_ptr;   //come to the one node before from 
    }                                    //end node. 

    last_ptr->next_ptr = new_node;       //last node's next is new node now.
    return;                              //above while loop exist for this.
}                                        //bring last_ptr to one before end node's

template <class T> void LinkedList<T>::showItem() {

    Node<T> *show_ptr = head_ptr;               //Creating new pointer for the listing

    while (show_ptr != NULL) {                  //Show till pointer be the null
        cout << show_ptr->data << endl;
        show_ptr = show_ptr->next_ptr;          //scroll pointer                             ******
//In the above line I got error, because the <T> type I think. How can I achieve this?
    }
    return;
}

Here is the main cpp file.. (BELOW)

#include <iostream>
#include <string>
#include <stdio.h>
#include <windows.h>
#include "LinkedList.h"

#define _CRT_SECURE_NO_WARNINGS
#define clear() printf("\033[H\033[J")


using namespace std;

class lib_instance {
    string bookname, username;

public:
    lib_instance();
    lib_instance(string b, string u) : bookname(b), username(u) {}
    lib_instance(string b) : bookname(b) {}
};

class library_apps {
public:
    LinkedList<lib_instance> list;

    void add_Item() {

        string book;
        string name;

        cout << "Please enter a book name: ";
        cin >> book;
        cout << "Please enter your name: ";
        cin >> name;
        lib_instance newItem(book, name);
        list.addItem(newItem);
        return;
    };

    void show_Item() {

        cout << "\nBooks" << endl;
        list.showItem();
        cout << endl << "Names" << endl;
        list.showItem();
        cout << endl;
        return;
    };
};


int main() {

    library_apps apps;
    char input;
    int a = 0;

    cout << "\n\tWelcome to the Library App\n" << endl;
    while (a != 5) {
        cout << "\n****What do you want to? :\n";
        cout << "1.Add a book and name to the library\n";
        cout << "5.Show/list books and names in the library\n" << endl;
        cin >> input;

        if (input != '1') {
            if (input != '5') {
                cout << "Please enter valid input...\n\n\n";
                Sleep(1500);                //Sleeps 1.5sec for show the message
                clear();                    //Clears the screen
                main();                     //return back to the main
            }
        }

        if (input == '1') {         //If input  equal with 1, adds items
            apps.add_Item();
        }

        if (input == '5') {         //If input  equal with 5, shows items
            apps.show_Item();
        }

        Sleep(3000);                //Delay
        clear();                    //Clear the screen and show main menu
    }
    return 0;
}

lib_instance::lib_instance() {}

In the .h file, I showed which lines has error with an asteriks --> "******" in the end of line. Main idea is, the linkedlist has generic type so I could use every type to create linkedlist. When I sent "object" to the linkedlist, problems are comes in. (I have to use with objects). Probably my way is wrong way but in some way I could do this.

enter image description here

I think that's the structure, and that's the structure I'm trying to implement.

Edit1: And also, I dont know how can I access the data's which are inside object.

Edit2(Error): binary '<<': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion)

error line --> "show_ptr = show_ptr->next_ptr;" end of the .h file

0 Answers
Related