Skip to content

Commit 2ac0028

Browse files
committed
list
1 parent f2d6cc5 commit 2ac0028

4 files changed

Lines changed: 70 additions & 8 deletions

File tree

.vscode/settings.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

source/chpt_3_ArraysLinkedListsAndRecursion/highScore/Scores.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Scores {
3838
entries[i + 1] = entry;
3939
}
4040

41-
GameEntry remove(int i) throw(IndexOutOfBounds) {
41+
GameEntry remove(int i) {
4242
if ((i < 0) || (i >= numEntries)) {
4343
throw IndexOutOfBounds("Invalid Index");
4444
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
#include <cstddef>
3+
4+
template <typename E>
5+
class SNode;
6+
7+
template <typename E>
8+
class SLinkedList {
9+
public:
10+
SLinkedList() {
11+
head = NULL;
12+
}
13+
~SLinkedList() {
14+
while (!empty()) {
15+
removeFront();
16+
}
17+
}
18+
19+
bool empty() const {
20+
return head == NULL;
21+
}
22+
const E& front() const {
23+
return head->element;
24+
}
25+
void addFront(const E& element) {
26+
SNode<E>* newNode = new SNode<E>;
27+
newNode->element = element;
28+
newNode->next = head;
29+
head = newNode;
30+
}
31+
void removeFront() {
32+
SNode<E>* frontNode = head;
33+
head = frontNode->next;
34+
delete frontNode;
35+
}
36+
37+
private:
38+
SNode<E>* head;
39+
};
40+
41+
template <typename E>
42+
class SNode {
43+
private:
44+
E element;
45+
SNode<E>* next;
46+
friend class SLinkedList<E>;
47+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "SLinkedList.h"
2+
#include <iostream>
3+
#include <ostream>
4+
5+
using namespace std;
6+
7+
int main(int argc, char const *argv[]) {
8+
9+
SLinkedList<int> sLinkedList;
10+
11+
sLinkedList.addFront(10);
12+
cout << sLinkedList.front() << endl;
13+
14+
sLinkedList.addFront(22);
15+
cout << sLinkedList.front() << endl;
16+
17+
sLinkedList.removeFront();
18+
cout << sLinkedList.front() << endl;
19+
20+
cin.get();
21+
return 0;
22+
}

0 commit comments

Comments
 (0)