-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequence.h
More file actions
139 lines (114 loc) · 5.51 KB
/
Copy pathSequence.h
File metadata and controls
139 lines (114 loc) · 5.51 KB
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
134
135
#pragma once
// FILE: sequence.h
// CLASS PROVIDED: Sequence
#include <iostream>
#include <exception>
using namespace std;
class Sequence
{
// The following declarations describe the functions that you must implement.
// You SHOULD NOT CHANGE any of the following PUBLIC declarations.
public:
/// @brief Sequence::value_type is the data type of values in the sequence.
/// Any primative C++ data type(int, double, bool, char, etc.), or a class
/// with a default constructor, a destructor, an assignment operator, and a
/// copy constructor can be used.
typedef int value_type;
/// @brief Sequence::size_type is the data type of any variable the number of
/// items in a sequence.
typedef unsigned int size_type;
// CONSTRUCTORS for the sequence class:
/// @brief Creates an empty sequence (numElts == 0) or
/// a sequence of numElts items indexed from 0 ... (numElts - 1).
/// @param sz number of elements to create the sequence with
Sequence(size_type sz = 0);
/// @brief Creates a (deep) copy of sequence seq.
/// @param s Sequence to be copied
Sequence(const Sequence& s);
/// @brief Destroys all items in the sequence and release the memory associated
/// with the sequence
~Sequence();
/// @brief The current sequence is released and replaced by a (deep) copy of
/// sequence (seq). A reference to the copied sequence is returned.
/// @param s the sequence to be copied
/// @return *this with elements from s copied into
Sequence& operator=(const Sequence& s);
/// @brief The position satisfies ( position >= 0 && position <= last_index( ) ).
/// The return value is a reference to the item at index position in the sequence.
/// throws an exception if the position is outside the bounds of the sequence
/// @param p position of item being accessed
/// @return reference to the item at index position in the sequence
value_type& operator[](size_type p);
/// @brief The value of item is append to the sequence.
/// @param v item to be added
void push_back(const value_type& v);
/// @brief The size of the sequence is greater than zero. Postcondition: The item
/// at the end of the sequence is deleted and size of the sequence is reduced by one.
void pop_back();
/// @brief The position satisfies ( position >= 0 && position <= last_index( ) ).
// The value of item is inserted at position and the size of sequence is increased
// by one.
/// throws an exception if the position is outside the bounds of the sequence
/// @param p position where to inset value in the sequence
/// @param v the value being inserted
void insert(size_type p, value_type v);
/// @brief The size of the sequence is greater than zero
/// throws an exception if the sequence is empty
/// @return a reference to the first item in the sequence.
const value_type& front() const;
/// @brief The size of the sequence is greater than zero.
/// throws an exception if the sequence is empty
/// @return a reference to the last item in the sequence.
const value_type& back() const;
/// @brief A true return value indicates size of the sequence is zero.
/// @return true if sequence is empty, false if sequence has size > 0
bool empty() const;
/// @brief The return value is the number of items in the sequence.
/// @return number of items in the sequence
size_type size() const;
/// @brief All items in the sequence deleted and the memory associated with
/// the sequence is released.
void clear();
/// @brief The position satisfies
/// ( position >= 0 && position + count - 1 <= last_index( ) )
/// throws an exception if the sequence when called with invalid position and/or count
/// The positions / items in the sequence at
/// ( position ... (position + count - 1) )
/// are deleted and their memory is released
/// @param p position of first item to be erased
/// @param n number of items to be erased
void erase(size_type p, size_type n = 1);
/// @return Creates a string made of the values of the elt of each node
string toString();
/// @brief Output the sequence. Do not change this declaration
/// @param os reference to an ostream object to output the sequence to
/// @param s Sequence object to be output
/// @return updates output stream reference
friend ostream& operator<<(ostream& os, const Sequence& s);
// YOU CAN MODIFY the following private declarations as needed to implement your sequence class
private:
/// @brief private inner class for linked list nodes
/// All data elements are public, since only class sequence can see SequenceNodes
class SequenceNode {
public:
SequenceNode* next; // pointer to next Node. If node is the tail, next is nullptr
SequenceNode* prev; // pointer to previous Node. If node is the head, prev is nullptr
value_type elt; // the element being stored in the node
/// @brief empty constructor, ensure next and prev are nullptr
SequenceNode(): next(nullptr), prev(nullptr)
{}
/// @brief parameterized constructor, next and prev are set to nullptr and the
/// node's element is set to the given value
/// @param item value the node's element will be set to
SequenceNode(value_type item): next(nullptr), prev(nullptr), elt(item)
{}
/// @brief node destructor. Think about what it would mean and whether anything
/// should be done here
~SequenceNode() {};
};
// MEMBER DATA. These are the data items that each sequence object will contain. For a
// doubly-linked list, each sequence will have a head and tail pointer, and numElts
SequenceNode* head;
SequenceNode* tail;
size_type numElts; // Number of elements in the sequence
}; // End of class Sequence