-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedLists.h
77 lines (60 loc) · 1.51 KB
/
LinkedLists.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// Created by ehsan on 1/23/17.
//
#ifndef DATASTRUCTURES_LINKEDLISTS_H
#define DATASTRUCTURES_LINKEDLISTS_H
#include <iostream>
template <typename E> class SLinkedList;
template <typename E> class DLinkedList;
// Implementing singly linked list
template <typename E>
class SNode {
private:
E elem;
SNode<E> * next;
friend class SLinkedList<E>; //Provide SLinkedList access to these private variables
};
template <typename E>
class SLinkedList {
public:
SLinkedList();
~SLinkedList();
bool empty() const;
const E& front() const;
void addFront(const E& e);
void removeFront();
void printAll() const;
private:
SNode<E> * head;
};
// Implementing doubly linked list
template <typename F>
class DNode {
private:
F elem;
DNode<F> * next;
DNode<F> * prev;
friend class DLinkedList<F>; //Provide SLinkedList access to these private variables
};
template <typename F>
class DLinkedList {
public:
DLinkedList();
~DLinkedList();
bool empty() const;
const F& front() const;
void addFront(const F& e);
void addBack(const F& e);
void removeFront();
void removeBack();
void printAll() const;
private:
DNode<F> * header;
DNode<F> * trailer;
protected:
void add (DNode<F>* v, const F& e); //Insert new node before v
void remove (DNode<F>* v); //Remove node v
};
// See http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file
#include "LinkedLists.tpp"
#endif //DATASTRUCTURES_LINKEDLISTS_H