File tree Expand file tree Collapse file tree
source/chpt_3_ArraysLinkedListsAndRecursion Expand file tree Collapse file tree Load Diff This file was deleted.
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 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+ };
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments