Skip to content

Commit 4ed6f6d

Browse files
committed
updates
1 parent 310cf72 commit 4ed6f6d

File tree

15 files changed

+530
-539
lines changed

15 files changed

+530
-539
lines changed

Bag Linked List/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 3.7)
2+
project(Bag_Linked_List)
3+
4+
set(CMAKE_CXX_STANDARD 11)
5+
6+
set(SOURCE_FILES
7+
bag_linked_list.cpp
8+
bag_linked_list.h
9+
main.cpp
10+
node.cpp
11+
node.h)
12+
13+
add_executable(Bag_Linked_List ${SOURCE_FILES})
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
CLASS implemented: bag_linked_list (see bag_linked_list.h for documentation)
3+
INVARIANT for the bag ADT:
4+
1. The items in the bag are stored on a linked list;
5+
2. The head pointer of the list is stored in the member variable head_ptr;
6+
3. The total number of items in the list is stored in the member variable
7+
many_nodes.
8+
*/
9+
10+
#include <cassert> // Provides assert
11+
#include <cstdlib> // Provides NULL, rand, size_t
12+
#include "node.h" // Provides node and the linked list functions
13+
#include "bag_linked_list.h"
14+
15+
using namespace std;
16+
17+
bag::bag() {
18+
head_ptr = nullptr;
19+
many_nodes = 0;
20+
}
21+
22+
bag::bag(const bag &source) {
23+
node *tail_ptr; // Needed for argument of list_copy
24+
25+
list_copy(source.head_ptr, head_ptr, tail_ptr);
26+
many_nodes = source.many_nodes;
27+
}
28+
29+
bag::~bag() {
30+
list_clear(head_ptr);
31+
many_nodes = 0;
32+
}
33+
34+
bag::size_type bag::count(const value_type &target) const {
35+
size_type answer = 0;
36+
// Use const node* since we don't change the nodes.
37+
const node *cursor = list_search(head_ptr, target);
38+
while (cursor != nullptr) {
39+
// Each time that cursor is not nullptr, we have another occurrence of
40+
// target, so we add one to answer, and move cursor to the next
41+
// occurrence of the target.
42+
++answer;
43+
cursor = cursor->link();
44+
cursor = list_search(cursor, target);
45+
}
46+
return answer;
47+
}
48+
49+
bag::size_type bag::erase(const value_type &target) {
50+
size_type answer = 0;
51+
node *target_ptr = list_search(head_ptr, target);
52+
while (target_ptr != nullptr) {
53+
// Each time that target_ptr is not nullptr, we have another occurrence
54+
// of target. We remove this target using the same technique that
55+
// was used in erase_one.
56+
target_ptr->set_data(head_ptr->data());
57+
target_ptr = target_ptr->link();
58+
target_ptr = list_search(target_ptr, target);
59+
list_head_remove(head_ptr);
60+
--many_nodes;
61+
++answer;
62+
}
63+
return answer;
64+
}
65+
66+
bool bag::erase_one(const value_type &target) {
67+
node *target_ptr;
68+
69+
target_ptr = list_search(head_ptr, target);
70+
if (target_ptr == nullptr)
71+
return false; // target isn't in the bag, so no work to do
72+
target_ptr->set_data(head_ptr->data());
73+
list_head_remove(head_ptr);
74+
--many_nodes;
75+
return true;
76+
}
77+
78+
bag::value_type bag::grab() const {
79+
size_type i;
80+
const node *cursor; // Use const node* since we don't change the nodes.
81+
82+
assert(size() > 0);
83+
i = (rand() % size()) + 1;
84+
cursor = list_locate(head_ptr, i);
85+
return cursor->data();
86+
}
87+
88+
void bag::insert(const value_type &entry) {
89+
list_head_insert(head_ptr, entry);
90+
++many_nodes;
91+
}
92+
93+
void bag::operator+=(const bag &addend) {
94+
node *copy_head_ptr;
95+
node *copy_tail_ptr;
96+
97+
if (addend.many_nodes > 0) {
98+
list_copy(addend.head_ptr, copy_head_ptr, copy_tail_ptr);
99+
copy_tail_ptr->set_link(head_ptr);
100+
head_ptr = copy_head_ptr;
101+
many_nodes += addend.many_nodes;
102+
}
103+
}
104+
105+
void bag::operator=(const bag &source) {
106+
node *tail_ptr; // Needed for argument to list_copy
107+
108+
if (this == &source)
109+
return;
110+
111+
list_clear(head_ptr);
112+
many_nodes = 0;
113+
list_copy(source.head_ptr, head_ptr, tail_ptr);
114+
many_nodes = source.many_nodes;
115+
}
116+
117+
bag operator+(const bag &b1, const bag &b2) {
118+
bag answer;
119+
120+
answer += b1;
121+
answer += b2;
122+
return answer;
123+
}

Bag Linked List/bag_linked_list.h

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
TYPEDEFS for the bag class:
3+
bag::value_type
4+
is the data type of the items in the bag. It may be any
5+
of the C++ built-in types (int, char, etc.), or a class with a default
6+
constructor, a copy constructor, an assignment
7+
operator, and a test for equality (x == y).
8+
9+
bag::size_type
10+
is the data type of any variable that keeps track of how many items are
11+
in a bag
12+
13+
CONSTRUCTOR for the bag class:
14+
bag( )
15+
Postcondition: The bag is empty.
16+
17+
MODIFICATION MEMBER FUNCTIONS for the bag class:
18+
size_type erase(const value_type& target)
19+
Postcondition: All copies of target have been removed from the bag.
20+
The return value is the number of copies removed (which could be zero).
21+
22+
bool erase_one(const value_type& target)
23+
Postcondition: If target was in the bag, then one copy of target has
24+
been removed from the bag; otherwise the bag is unchanged. A true
25+
return value indicates that one copy was removed; false indicates that
26+
nothing was removed.
27+
28+
void insert(const value_type& entry)
29+
Postcondition: A new copy of entry has been inserted into the bag.
30+
31+
void operator +=(const bag& addend)
32+
Postcondition: Each item in addend has been added to this bag.
33+
34+
CONSTANT MEMBER FUNCTIONS for the bag class:
35+
size_type size( ) const
36+
Postcondition: Return value is the total number of items in the bag.
37+
38+
size_type count(const value_type& target) const
39+
Postcondition: Return value is number of times target is in the bag.
40+
41+
value_type grab( ) const
42+
Precondition: size( ) > 0.
43+
Postcondition: The return value is a randomly selected item from the bag.
44+
45+
NONMEMBER FUNCTIONS for the bag class:
46+
bag operator +(const bag& b1, const bag& b2)
47+
Postcondition: The bag returned is the union of b1 and b2.
48+
49+
VALUE SEMANTICS for the bag class:
50+
Assignments and the copy constructor may be used with bag objects.
51+
52+
DYNAMIC MEMORY USAGE by the bag:
53+
If there is insufficient dynamic memory, then the following functions throw
54+
bad_alloc: The constructors, insert, operator +=, operator +, and the
55+
assignment operator.
56+
*/
57+
58+
#ifndef BAG_LINKED_LIST_H
59+
#define BAG_LINKED_LIST_H
60+
61+
#include <cstdlib>
62+
#include "node.h"
63+
64+
class bag {
65+
66+
public:
67+
68+
typedef std::size_t size_type;
69+
typedef node::value_type value_type;
70+
71+
bag();
72+
73+
bag(const bag &source);
74+
75+
~bag();
76+
77+
size_type erase(const value_type &target);
78+
79+
bool erase_one(const value_type &target);
80+
81+
void insert(const value_type &entry);
82+
83+
void operator+=(const bag &addend);
84+
85+
void operator=(const bag &source);
86+
87+
size_type size() const { return many_nodes; }
88+
89+
size_type count(const value_type &target) const;
90+
91+
value_type grab() const;
92+
93+
private:
94+
95+
node *head_ptr; // List head pointer
96+
size_type many_nodes; // Number of nodes on the list
97+
98+
};
99+
100+
// NONMEMBER FUNCTIONS for the bag class:
101+
bag operator+(const bag &b1, const bag &b2);
102+
103+
104+
#endif
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
/** FILE: bagLL_demo.cpp
2-
Demonstration program for the 3rd version of the bag (bag3.h and bag3.cxx).
3-
This is a the same as the demonstration program for bag1,
4-
except that we no longer need to check whether the bag reaches its
5-
capacity.*/
1+
/**
2+
Demonstration program for the 3rd version of the bag (bag3.h and bag3.cxx).
3+
This is a the same as the demonstration program for bag1,
4+
except that we no longer need to check whether the bag reaches its
5+
capacity.
6+
*/
67

7-
#include <iostream> // Provides cout and cin
8-
#include "bag_linked_list.h" // With Item defined as an int
8+
#include <iostream>
9+
#include "bag_linked_list.h"
910

1011
using namespace std;
11-
using namespace main_savitch_5;
1212

1313
// PROTOTYPES for functions used by this demonstration program:
1414
void get_ages(bag &ages);

Bag Linked List/node.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
IMPLEMENTS: The functions of the node class and the
3+
linked list toolkit (see node.h for documentation).
4+
INVARIANT for the node class:
5+
The data of a node is stored in data_field, and the link in link_field.
6+
*/
7+
8+
#include "node.h"
9+
#include <cassert>
10+
11+
using namespace std;
12+
13+
size_t list_length(const node *head_ptr) {
14+
const node *cursor;
15+
size_t answer = 0;
16+
17+
for (cursor = head_ptr; cursor != nullptr; cursor = cursor->link())
18+
++answer;
19+
20+
return answer;
21+
}
22+
23+
void list_head_insert(node *&old_head_ptr, const node::value_type &entry) {
24+
old_head_ptr = new node(entry, old_head_ptr);
25+
}
26+
27+
void list_insert(node *previous_ptr, const node::value_type &entry) {
28+
node *insert_ptr;
29+
insert_ptr = new node(entry, previous_ptr->link());
30+
previous_ptr->set_link(insert_ptr);
31+
}
32+
33+
node *list_search(node *head_ptr, const node::value_type &target) {
34+
node *cursor;
35+
for (cursor = head_ptr; cursor != nullptr; cursor = cursor->link())
36+
if (target == cursor->data())
37+
return cursor;
38+
return nullptr;
39+
}
40+
41+
const node *list_search(const node *head_ptr, const node::value_type &target) {
42+
const node *cursor;
43+
for (cursor = head_ptr; cursor != nullptr; cursor = cursor->link())
44+
if (target == cursor->data())
45+
return cursor;
46+
return nullptr;
47+
}
48+
49+
node *list_locate(node *head_ptr, size_t position) {
50+
assert (position > 0);
51+
node *cursor = head_ptr;
52+
for (size_t i = 1; (i < position) && (cursor != nullptr); i++)
53+
cursor = cursor->link();
54+
return cursor;
55+
}
56+
57+
const node *list_locate(const node *head_ptr, size_t position) {
58+
assert (0 < position);
59+
const node *cursor = head_ptr;
60+
for (size_t i = 1; (i < position) && (cursor != nullptr); i++)
61+
cursor = cursor->link();
62+
return cursor;
63+
}
64+
65+
void list_head_remove(node *&head_ptr) {
66+
node *remove_ptr = head_ptr;
67+
head_ptr = head_ptr->link();
68+
delete remove_ptr;
69+
}
70+
71+
void list_remove(node *previous_ptr) {
72+
node *remove_ptr;
73+
remove_ptr = previous_ptr->link();
74+
previous_ptr->set_link(remove_ptr->link());
75+
delete remove_ptr;
76+
}
77+
78+
void list_clear(node *&head_ptr) {
79+
while (head_ptr != nullptr)
80+
list_head_remove(head_ptr);
81+
}
82+
83+
void list_copy(const node *source_ptr, node *&head_ptr, node *&tail_ptr) {
84+
head_ptr = nullptr;
85+
tail_ptr = nullptr;
86+
87+
// Handle the case of the empty list.
88+
if (source_ptr == nullptr)
89+
return;
90+
91+
// Make the head node for the newly created list, and put data in it.
92+
list_head_insert(head_ptr, source_ptr->data());
93+
tail_ptr = head_ptr;
94+
95+
// Copy the rest of the nodes one at a time, adding at the tail of new list.
96+
source_ptr = source_ptr->link();
97+
while (source_ptr != nullptr) {
98+
list_insert(tail_ptr, source_ptr->data());
99+
tail_ptr = tail_ptr->link();
100+
source_ptr = source_ptr->link();
101+
}
102+
}

0 commit comments

Comments
 (0)