-
Notifications
You must be signed in to change notification settings - Fork 0
/
LList.cpp
97 lines (80 loc) · 1.83 KB
/
LList.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
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
//
// Created by violet on 6/30/22.
//
#include "Lnode.h"
#include "LList.h"
#include <iostream>
using namespace std;
template <class T>
LList<T>::LList(){
head = tail = nullptr;
_size = 0;
}
template <class T>
void LList<T>::append(T data_val) {
//create a new node and set the data member as well
auto *newNode = new LNode<T>();
newNode->data() = data_val;
if (_size == 0){
head = tail = newNode;
_size ++;
}
else if (_size == 1){
head->next(newNode);
newNode->prev(head);
tail = newNode;
_size++;
}
else{
LNode<T> *cur = head;
while(cur != nullptr){
if(cur->next() == nullptr){
cur->next(newNode);
newNode->prev(cur);
tail = newNode;
_size++;
return;
}
cur = cur->next();
}
}
}
template <class T>
void LList<T>::print(){
LNode<T> *cur = head;
LNode<T> *backwards_cur = tail;
cout<<"FORWARDS"<<endl;
while (cur != nullptr) {
cout << cur->data()<< "->";
cur = cur -> next();
}
cout << "\nBACKWARDS" << endl;
while(backwards_cur != nullptr){
cout<< backwards_cur->data() <<"->";
backwards_cur = backwards_cur -> prev();
}
}
template<class T>
LList<T>::~LList() {
LNode<T> *cur = head;
LNode<T> *next;
while (cur != nullptr) {
next = cur->next();
delete cur;
cur = next;
_size--;
}
}
template<class T>
bool LList<T>::isPalindrome() {
LNode<T> *cur = head;
LNode<T> *backwards_cur = tail;
while (cur != nullptr) {
if (cur->data() != backwards_cur->data()) {
return false;
}
cur = cur->next();
backwards_cur = backwards_cur->prev();
}
return true;
}