-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedLists.tpp
133 lines (113 loc) · 2.59 KB
/
LinkedLists.tpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// Created by ehsan on 1/23/17.
//
#include "LinkedLists.h"
using namespace std;
template <typename E>
SLinkedList<E>::SLinkedList():head(NULL){};
template <typename E>
bool SLinkedList<E>::empty() const {
return (head==NULL); }
template <typename E>
void SLinkedList<E>::addFront(const E& e) {
SNode<E> * node = new SNode<E>;
node->elem=e;
node->next=head;
head=node; }
template <typename E>
void SLinkedList<E>::removeFront() {
SNode<E> * temp = head;
head = head->next;
delete temp; }
template <typename E>
const E& SLinkedList<E>::front() const {
return head->elem;
}
template <typename E>
SLinkedList<E>::~SLinkedList() {
while (!empty()){
removeFront();
}
}
template <typename E>
void SLinkedList<E>::printAll() const {
SNode<E> * itr =head;
while (itr != NULL){
cout<<itr->elem<<" ";
itr = itr->next;
}
}
// Functions for Doubly linked list
template <typename F>
DLinkedList<F>::DLinkedList():header(new DNode<F>),trailer(new DNode<F>){
header->next=trailer;
trailer->prev = header;
}
template <typename F>
bool DLinkedList<F>::empty() const {
return (header->next==trailer);
}
template <typename F>
void DLinkedList<F>::addFront(const F& e) {
add(header->next,e);
}
template <typename F>
void DLinkedList<F>::addBack(const F& e) {
add(trailer,e);
}
template <typename F>
void DLinkedList<F>::removeFront() {
remove(header->next);
/*
DNode<E> * temp = header;
header->next->prev=header;
header = header->next;
delete temp;
*/
}
template <typename F>
void DLinkedList<F>::removeBack() {
remove(trailer->prev);
/*
DNode<E>* temp;
temp = trailer;
trailer->prev->next=trailer->next;
trailer=trailer->prev;
delete temp;
*/
}
template <typename F>
const F& DLinkedList<F>::front() const {
return header->next->elem;
}
template <typename F>
DLinkedList<F>::~DLinkedList() {
while (!empty()){
removeFront();
}
}
template <typename F>
void DLinkedList<F>::printAll() const {
DNode<F> * itr = header->next;
while (itr != trailer){
cout<<(int)itr->elem<<" ";
itr = itr->next;
}
}
/*Adds an element before V*/
template <typename F>
void DLinkedList<F>::add(DNode<F> *v, const F &e) {
DNode<F> * newNode = new DNode<F>;
newNode->elem = e;
newNode->next = v;
newNode->prev = v->prev;
v->prev = v->prev->next=newNode;
//v->prev = newNode;
}
template <typename F>
void DLinkedList<F>::remove(DNode<F> *v) {
DNode<F> * temp = v;
v->prev->next=v->next;
v->next->prev = v->prev;
delete temp;
}