-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewSequence.h
80 lines (66 loc) · 3.18 KB
/
newSequence.h
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
//
// newSequence.h
// HW1
//
// Created by Angela Kan on 1/19/21.
//
#ifndef NEWSEQUENCE_INCLUDED
#define NEWSEQUENCE_INCLUDED
//#include <string>
using ItemType = unsigned long;
const int DEFAULT_MAX_ITEMS = 150;
class Sequence
{
public:
Sequence(int max = DEFAULT_MAX_ITEMS); // Create an empty sequence (i.e., one with no items)
~Sequence(); //Destructor
Sequence(const Sequence& other); //Copy constructor
Sequence& operator=(const Sequence& item); //Assignment operator
bool empty() const; // Return true if the sequence is empty, otherwise false.
int size() const; // Return the number of items in the sequence.
int insert(int pos, const ItemType& value);
// Insert value into the sequence so that it becomes the item at
// position pos. The original item at position pos and those that
// follow it end up at positions one higher than they were at before.
// Return pos if 0 <= pos <= size() and the value could be
// inserted. (It might not be, if the sequence has a fixed capacity,
// e.g., because it's implemented using a fixed-size array.) Otherwise,
// leave the sequence unchanged and return -1. Notice that
// if pos is equal to size(), the value is inserted at the end.
int insert(const ItemType& value);
// Let p be the smallest integer such that value <= the item at
// position p in the sequence; if no such item exists (i.e.,
// value > all items in the sequence), let p be size(). Insert
// value into the sequence so that it becomes the item in position
// p. The original item at position p and those that follow it end
// up at positions one higher than before. Return p if the value
// was actually inserted. Return -1 if the value was not inserted
// (perhaps because the sequence has a fixed capacity and is full).
bool erase(int pos);
// If 0 <= pos < size(), remove the item at position pos from
// the sequence (so that all items that followed that item end up at
// positions one lower than they were at before), and return true.
// Otherwise, leave the sequence unchanged and return false.
int remove(const ItemType& value);
// Erase all items from the sequence that == value. Return the
// number of items removed (which will be 0 if no item == value).
bool get(int pos, ItemType& value) const;
// If 0 <= pos < size(), copy into value the item at position pos
// of the sequence and return true. Otherwise, leave value unchanged
// and return false.
bool set(int pos, const ItemType& value);
// If 0 <= pos < size(), replace the item at position pos in the
// sequence with value and return true. Otherwise, leave the sequence
// unchanged and return false.
int find(const ItemType& value) const;
// Let p be the smallest integer such that value == the item at
// position p in the sequence; if no such item exists, let p be -1.
// Return p.
void swap(Sequence& other);
// Exchange the contents of this sequence with the other one.
private:
int m_max;
int m_size;
ItemType* m_seq;
};
#endif /* NEWSEQUENCE_INCLUDED */