Linked List

Hi, I created a singly linked List and would like to know what you guys think such as any ways to improve or mistake I have?
Thanks in Advance

LinkedList.h
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
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include <string>
#include <iostream>

using namespace std;

class LinkedList
{
    public:
        LinkedList();
        ~LinkedList();
        bool insert(int position ,string userInput);
        int getSize(){
            return size;
        }

    private:
        struct Node{
            string data;
            Node * next;
        };
        Node * head;
        Node *find( int position);
        int size;
};

#endif // LINKEDLIST_H


LinkedList.cpp
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
#include "LinkedList.h"

LinkedList::LinkedList():head(NULL), size(0)
{}

LinkedList::~LinkedList()
{}

bool LinkedList::insert(int position,string userInput)
{
    Node * newOne = new Node;
    newOne->data = userInput;

    if(position == 1)
    {
        newOne->next = head;
        head = newOne;
    }
    else{
        Node * previous = find(position - 1);
        newOne->next = previous->next;
        previous->next = newOne;
    }
    size++;

    return true;
}

LinkedList::Node*LinkedList::find(int position)
{
    Node * current = head;
    for(int i = 1; i < position; i++){
        current = current->next;
    }
    return current;
}

you could make the destructor actually destroy the nodes instead of leaking memory.

What is the scope of this task anyway, what functionality are you providing? How is it going to be different from the standard C++ list?
It was just for fun. I just wanted to know if the approach is correct
Topic archived. No new replies allowed.