Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/483475102315839488
removed print method as that was for debug only
Source Link
#include <iostream>

#pragma once
template <typename T>
class Node
{
const char * const _key;
Node *_next;
T _value;

public:
Node(const char * const key, const T &value) : _key(key), _value(value), _next(nullptr) {}

const char * const getKey()
{
    return _key;
}

Node *getNext()
{
    return _next;
}

void setNext(Node *next)
{
    _next = next;
}

T &getValue()
{
    return _value;
}
 
void print()
{
    cout << _key << ", " << _value;
}
};
#include <iostream>
#include "Node.h"

#pragma once

template <typename T>

class LinkedList
{
int _count;
Node<T> *_first;

public:
LinkedList() : _count(0), _first(nullptr) {}

Node<T> *add(const char * const key, const T &value)
{
    Node<T> *first = new Node<T>(key, value);
    first->setNext(_first);
    _first = first;
    _count++;
    return _first;
}

void remove(const char * const key)
{
    Node<T> *next = _first;
    Node<T> *previous = nullptr;

    while (next)
    {
        if (next->getKey() == key)
        {
            if (next == _first)
                _first = next->getNext();
            else
                previous->setNext(next->getNext());
            _count--;
            return;
        }
        previous = next;
        next = next->getNext();
    }
}

int getCount() 
{ 
    return _count;
}

Node<T> * const getFirst()
{
    return _first;
}

void print()
{
    Node<T> *next = _first;

    while (next)
    {
        next->print();
        cout << "\n";
        next = next->getNext();
    }
}
};
#include <iostream>

#pragma once
template <typename T>
class Node
{
const char * const _key;
Node *_next;
T _value;

public:
Node(const char * const key, const T &value) : _key(key), _value(value), _next(nullptr) {}

const char * const getKey()
{
    return _key;
}

Node *getNext()
{
    return _next;
}

void setNext(Node *next)
{
    _next = next;
}

T &getValue()
{
    return _value;
}
 
void print()
{
    cout << _key << ", " << _value;
}
};
#include <iostream>
#include "Node.h"

#pragma once

template <typename T>

class LinkedList
{
int _count;
Node<T> *_first;

public:
LinkedList() : _count(0), _first(nullptr) {}

Node<T> *add(const char * const key, const T &value)
{
    Node<T> *first = new Node<T>(key, value);
    first->setNext(_first);
    _first = first;
    _count++;
    return _first;
}

void remove(const char * const key)
{
    Node<T> *next = _first;
    Node<T> *previous = nullptr;

    while (next)
    {
        if (next->getKey() == key)
        {
            if (next == _first)
                _first = next->getNext();
            else
                previous->setNext(next->getNext());
            _count--;
            return;
        }
        previous = next;
        next = next->getNext();
    }
}

int getCount() 
{ 
    return _count;
}

Node<T> * const getFirst()
{
    return _first;
}

void print()
{
    Node<T> *next = _first;

    while (next)
    {
        next->print();
        cout << "\n";
        next = next->getNext();
    }
}
};
#include <iostream>

#pragma once
template <typename T>
class Node
{
const char * const _key;
Node *_next;
T _value;

public:
Node(const char * const key, const T &value) : _key(key), _value(value), _next(nullptr) {}

const char * const getKey()
{
    return _key;
}

Node *getNext()
{
    return _next;
}

void setNext(Node *next)
{
    _next = next;
}

T &getValue()
{
    return _value;
}
};
#include <iostream>
#include "Node.h"

#pragma once

template <typename T>

class LinkedList
{
int _count;
Node<T> *_first;

public:
LinkedList() : _count(0), _first(nullptr) {}

Node<T> *add(const char * const key, const T &value)
{
    Node<T> *first = new Node<T>(key, value);
    first->setNext(_first);
    _first = first;
    _count++;
    return _first;
}

void remove(const char * const key)
{
    Node<T> *next = _first;
    Node<T> *previous = nullptr;

    while (next)
    {
        if (next->getKey() == key)
        {
            if (next == _first)
                _first = next->getNext();
            else
                previous->setNext(next->getNext());
            _count--;
            return;
        }
        previous = next;
        next = next->getNext();
    }
}

int getCount() 
{ 
    return _count;
}

Node<T> * const getFirst()
{
    return _first;
}
};
deleted 35 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Linked Listlist implementation correctness C++

Node:Node:

Linked List:Linked List:

Thanks in advance!

p.s.

I am not too certain how to properly define value inside Node class, because I want to support both primitive types and objects.

p.p.s.

thisThis actually worked for me, although this is probably not correct? if (next->getKey() == key)

if (next->getKey() == key)

Linked List implementation correctness C++

Node:

Linked List:

Thanks in advance!

p.s.

I am not too certain how to properly define value inside Node class, because I want to support both primitive types and objects.

p.p.s.

this actually worked for me, although this is probably not correct? if (next->getKey() == key)

Linked list implementation correctness

Node:

Linked List:

I am not too certain how to properly define value inside Node class, because I want to support both primitive types and objects.

This actually worked for me, although this is probably not correct?

if (next->getKey() == key)
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Source Link
Loading