initial commit
This commit is contained in:
commit
a828b3525a
117
.gitignore
vendored
Normal file
117
.gitignore
vendored
Normal file
@ -0,0 +1,117 @@
|
||||
# Created by https://www.toptal.com/developers/gitignore/api/clion
|
||||
# Edit at https://www.toptal.com/developers/gitignore?templates=clion
|
||||
|
||||
### CLion ###
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff
|
||||
**/.idea/**/workspace.xml
|
||||
**/.idea/**/tasks.xml
|
||||
**/.idea/**/usage.statistics.xml
|
||||
**/.idea/**/dictionaries
|
||||
**/.idea/**/shelf
|
||||
|
||||
# AWS User-specific
|
||||
**/.idea/**/aws.xml
|
||||
|
||||
# Generated files
|
||||
**/.idea/**/contentModel.xml
|
||||
|
||||
# Sensitive or high-churn files
|
||||
**/.idea/**/dataSources/
|
||||
**/.idea/**/dataSources.ids
|
||||
**/.idea/**/dataSources.local.xml
|
||||
**/.idea/**/sqlDataSources.xml
|
||||
**/.idea/**/dynamic.xml
|
||||
**/.idea/**/uiDesigner.xml
|
||||
**/.idea/**/dbnavigator.xml
|
||||
|
||||
# Gradle
|
||||
**/.idea/**/gradle.xml
|
||||
**/.idea/**/libraries
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/artifacts
|
||||
# .idea/compiler.xml
|
||||
# .idea/jarRepositories.xml
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
**/cmake-build-*/
|
||||
|
||||
# Mongo Explorer plugin
|
||||
**/.idea/**/mongoSettings.xml
|
||||
|
||||
# File-based project format
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
**/out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
**/.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
**/atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
**/.idea/replstate.xml
|
||||
|
||||
# SonarLint plugin
|
||||
**/.idea/sonarlint/
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
**/com_crashlytics_export_strings.xml
|
||||
**/crashlytics.properties
|
||||
**/crashlytics-build.properties
|
||||
**/fabric.properties
|
||||
|
||||
# Editor-based Rest Client
|
||||
**/.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
**/.idea/caches/build_file_checksums.ser
|
||||
|
||||
### CLion Patch ###
|
||||
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
|
||||
|
||||
# *.iml
|
||||
# modules.xml
|
||||
# .idea/misc.xml
|
||||
# *.ipr
|
||||
|
||||
# Sonarlint plugin
|
||||
# https://plugins.jetbrains.com/plugin/7973-sonarlint
|
||||
**/.idea/**/sonarlint/
|
||||
|
||||
# SonarQube Plugin
|
||||
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
|
||||
**/.idea/**/sonarIssues.xml
|
||||
|
||||
# Markdown Navigator plugin
|
||||
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
|
||||
**/.idea/**/markdown-navigator.xml
|
||||
**/.idea/**/markdown-navigator-enh.xml
|
||||
**/.idea/**/markdown-navigator/
|
||||
|
||||
# Cache file creation bug
|
||||
# See https://youtrack.jetbrains.com/issue/JBR-2257
|
||||
**/.idea/$CACHE_FILE$
|
||||
|
||||
# CodeStream plugin
|
||||
# https://plugins.jetbrains.com/plugin/12206-codestream
|
||||
**/.idea/codestream.xml
|
||||
|
||||
# Azure Toolkit for IntelliJ plugin
|
||||
# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
|
||||
**/.idea/**/azureSettings.xml
|
||||
|
||||
# End of https://www.toptal.com/developers/gitignore/api/clion
|
8
01-stacks/.idea/.gitignore
generated
vendored
Normal file
8
01-stacks/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
1
01-stacks/.idea/.name
generated
Normal file
1
01-stacks/.idea/.name
generated
Normal file
@ -0,0 +1 @@
|
||||
01_stacks
|
2
01-stacks/.idea/01-stacks.iml
generated
Normal file
2
01-stacks/.idea/01-stacks.iml
generated
Normal file
@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
5
01-stacks/.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
5
01-stacks/.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
@ -0,0 +1,5 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<state>
|
||||
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||
</state>
|
||||
</component>
|
6
01-stacks/.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
6
01-stacks/.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
</profile>
|
||||
</component>
|
4
01-stacks/.idea/misc.xml
generated
Normal file
4
01-stacks/.idea/misc.xml
generated
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
</project>
|
8
01-stacks/.idea/modules.xml
generated
Normal file
8
01-stacks/.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/01-stacks.iml" filepath="$PROJECT_DIR$/.idea/01-stacks.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
01-stacks/CMakeLists.txt
Normal file
6
01-stacks/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
project(01_stacks)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(01_stacks main.cpp)
|
BIN
01-stacks/Lab Stacks 4 - max (Stack ADT).zip
Normal file
BIN
01-stacks/Lab Stacks 4 - max (Stack ADT).zip
Normal file
Binary file not shown.
95
01-stacks/StackADT.h
Normal file
95
01-stacks/StackADT.h
Normal file
@ -0,0 +1,95 @@
|
||||
/*
|
||||
Written by: Iurii Tatishchev
|
||||
=============================
|
||||
Stack template
|
||||
*/
|
||||
|
||||
#ifndef STACK_ADT
|
||||
#define STACK_ADT
|
||||
|
||||
template<typename T>
|
||||
class Stack {
|
||||
private:
|
||||
// Structure for the stack nodes
|
||||
struct StackNode {
|
||||
T value; // Value in the node
|
||||
StackNode *next; // Pointer to next node
|
||||
};
|
||||
|
||||
StackNode *top; // Pointer to the stack top
|
||||
int length;
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Stack(){ top = nullptr; length = 0; }
|
||||
// Destructor
|
||||
~Stack();
|
||||
|
||||
// Stack operations:
|
||||
bool push(T item);
|
||||
T pop();
|
||||
T peek() { return top->value; };
|
||||
bool isEmpty() { return length == 0; };
|
||||
int getLength() { return length; } ;
|
||||
};
|
||||
|
||||
/*
|
||||
Member function push inserts the argument onto
|
||||
the stack.
|
||||
*/
|
||||
template<typename T>
|
||||
bool Stack<T>::push(T item) {
|
||||
StackNode *newNode; // Pointer to a new node
|
||||
|
||||
// Allocate a new node and store item there.
|
||||
newNode = new StackNode;
|
||||
if (!newNode)
|
||||
return false;
|
||||
newNode->value = item;
|
||||
|
||||
// Update links and counter
|
||||
newNode->next = top;
|
||||
top = newNode;
|
||||
length++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
Member function pop deletes the value at the top
|
||||
of the stack and returns it.
|
||||
Assume stack is not empty.
|
||||
*/
|
||||
template<typename T>
|
||||
T Stack<T>::pop() {
|
||||
T val = this->top->value;
|
||||
|
||||
StackNode* oldTop = this->top;
|
||||
this->top = this->top->next;
|
||||
this->length--;
|
||||
delete oldTop;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
/*
|
||||
Destructor:
|
||||
Traverses the list deleting each node (without calling pop)
|
||||
*/
|
||||
template<typename T>
|
||||
Stack<T>::~Stack() {
|
||||
StackNode *currNode;
|
||||
|
||||
// Position nodePtr at the top of the stack.
|
||||
currNode = top;
|
||||
|
||||
// Traverse the list deleting each node.
|
||||
while (currNode) {
|
||||
StackNode* nextNode = currNode->next;
|
||||
delete currNode;
|
||||
currNode = nextNode;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
1
01-stacks/in1.txt
Normal file
1
01-stacks/in1.txt
Normal file
@ -0,0 +1 @@
|
||||
10 20 30 1 40 0 50 -2 15 25 -3 60 70
|
118
01-stacks/main.cpp
Normal file
118
01-stacks/main.cpp
Normal file
@ -0,0 +1,118 @@
|
||||
/*
|
||||
Written by: Iurii Tatishchev
|
||||
=============================
|
||||
CIS 22C
|
||||
Project: Stack ADT
|
||||
*/
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "StackADT.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void printInfo();
|
||||
void processNumbers(const string&, Stack<int> &);
|
||||
void printStack(Stack<int> &);
|
||||
|
||||
int main() {
|
||||
printInfo();
|
||||
|
||||
cout << "Enter input file name: ";
|
||||
string filename;
|
||||
getline(cin, filename); // assume valid
|
||||
cout << endl;
|
||||
|
||||
// Create a stack
|
||||
Stack<int> s;
|
||||
|
||||
// Process the numbers in the file
|
||||
processNumbers(filename, s);
|
||||
|
||||
// Print remaining stack elements
|
||||
printStack(s);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
This function displays the project's title
|
||||
*/
|
||||
void printInfo() {
|
||||
cout << " ~*~ Project: Stack ADT ~*~ " << endl;
|
||||
}
|
||||
|
||||
void processNumbers(const string& filename, Stack<int> &s) {
|
||||
ifstream inFile(filename);
|
||||
// Check if the file was opened successfully
|
||||
if (!inFile.is_open()) {
|
||||
cout << "There was an error opening \"" << filename << "\". Exiting.\n";
|
||||
exit(1);
|
||||
}
|
||||
cout << "\nInput File: " << filename << "\n";
|
||||
|
||||
int number;
|
||||
Stack<int> maxStack;
|
||||
inFile >> number;
|
||||
while(inFile.good()) {
|
||||
switch (number) {
|
||||
// Each time we read 0, display the number of elements in the stack.
|
||||
case 0:
|
||||
cout << "Count: " << s.getLength() << "\n";
|
||||
break;
|
||||
// Each time we read 1, display the top element of the stack and the maximum element in the stack.
|
||||
case 1:
|
||||
// If there are no elements in the stack display "Top: Empty".
|
||||
if (s.isEmpty()) {
|
||||
cout << "Top: Empty\n";
|
||||
break;
|
||||
}
|
||||
cout << "Top: " << s.peek() << "\n";
|
||||
cout << "Max: " << maxStack.peek() << "\n";
|
||||
break;
|
||||
default:
|
||||
// Each time we read a number greater than 1, we push it onto the stack.
|
||||
if (number > 1) {
|
||||
s.push(number);
|
||||
maxStack.push(max(maxStack.isEmpty()? 0 : maxStack.peek(), number));
|
||||
}
|
||||
// Each time we read a negative number,
|
||||
// we pop and print the number removed from the stack and the largest value in the stack.
|
||||
// If there are no numbers in the stack, display "Pop: Empty".
|
||||
// If there's only one number in the stack, after removing it, print it.
|
||||
else {
|
||||
if (s.isEmpty()) {
|
||||
cout << "Pop: Empty\n";
|
||||
} else {
|
||||
cout << "Pop: " << s.pop() << "\n";
|
||||
maxStack.pop();
|
||||
if (!maxStack.isEmpty()) cout << "Max: " << maxStack.peek() << "\n";
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
inFile >> number;
|
||||
}
|
||||
// Don't forget to close the file
|
||||
inFile.close();
|
||||
}
|
||||
|
||||
/*
|
||||
This function pops and prints all elements in the stack.
|
||||
*/
|
||||
void printStack(Stack<int> &s) {
|
||||
cout << "Stack: ";
|
||||
if (s.isEmpty()) {
|
||||
cout << "Empty\n";
|
||||
return;
|
||||
}
|
||||
|
||||
while (s.getLength() != 1) {
|
||||
cout << s.pop() << ", ";
|
||||
}
|
||||
cout << s.pop() << ".\n";
|
||||
}
|
1
01-stacks/myFile.txt
Normal file
1
01-stacks/myFile.txt
Normal file
@ -0,0 +1 @@
|
||||
0 1 -6 10 50 30 -1 70 20 -5 -4 -3 -2
|
1
01-stacks/numbers1.txt
Normal file
1
01-stacks/numbers1.txt
Normal file
@ -0,0 +1 @@
|
||||
10 30 20 1 70 0 40 50 -2 0 85 95 -3 60 75
|
84
01-stacks/stacks_01_strings.cpp
Normal file
84
01-stacks/stacks_01_strings.cpp
Normal file
@ -0,0 +1,84 @@
|
||||
/**~*~*~*
|
||||
CIS 22C
|
||||
Project: Stack of strings
|
||||
|
||||
Written by: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
*~*/
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Stack_str {
|
||||
private:
|
||||
// Structure for the stack nodes
|
||||
struct StackNode {
|
||||
string value; // Value in the node
|
||||
StackNode *next; // Pointer to next node
|
||||
};
|
||||
|
||||
StackNode *top; // Pointer to the stack top
|
||||
int length;
|
||||
|
||||
public:
|
||||
Stack_str(){ top = NULL; length = 0; } //Constructor
|
||||
//~Stack_str(); // Destructor
|
||||
|
||||
// Stack operations
|
||||
bool isEmpty() {
|
||||
return length == 0;
|
||||
}
|
||||
|
||||
bool push(string);
|
||||
|
||||
// string pop();
|
||||
string peek() {
|
||||
return top->value;
|
||||
}
|
||||
|
||||
int getLength() {
|
||||
return length;
|
||||
}
|
||||
};
|
||||
|
||||
/**~*~*~*
|
||||
Member function push: pushes the argument onto the stack.
|
||||
*~**/
|
||||
bool Stack_str::push(string item) {
|
||||
StackNode *newNode; // Pointer to a new node
|
||||
|
||||
// Allocate a new node and store item there.
|
||||
newNode = new StackNode;
|
||||
if (!newNode)
|
||||
return false;
|
||||
newNode->value = item;
|
||||
|
||||
// Update links and counter
|
||||
newNode->next = top;
|
||||
top = newNode;
|
||||
length++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
Stack_str s;
|
||||
string item;
|
||||
|
||||
|
||||
getline(cin, item);
|
||||
while (item != "0") {
|
||||
s.push(item);
|
||||
cin.clear();
|
||||
getline(cin, item);
|
||||
}
|
||||
|
||||
cout << s.getLength() << "\n";
|
||||
cout << (s.isEmpty() ? "Empty Stack!" : s.peek()) << "\n";
|
||||
cout << s.getLength() << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
BIN
01-stacks/stacks_02_destructor
Executable file
BIN
01-stacks/stacks_02_destructor
Executable file
Binary file not shown.
89
01-stacks/stacks_02_destructor.cpp
Normal file
89
01-stacks/stacks_02_destructor.cpp
Normal file
@ -0,0 +1,89 @@
|
||||
/**~*~*~*
|
||||
CIS 22C
|
||||
Project: Stack of strings (Destructor)
|
||||
|
||||
Written by:
|
||||
IDE:
|
||||
*~*/
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Stack_str {
|
||||
private:
|
||||
// Structure for the stack nodes
|
||||
struct StackNode {
|
||||
string value; // Value in the node
|
||||
StackNode *next; // Pointer to next node
|
||||
};
|
||||
|
||||
StackNode *top; // Pointer to the stack top
|
||||
int length; // Number of nodes
|
||||
|
||||
public:
|
||||
Stack_str(){ top = NULL; length = 0; } // Constructor
|
||||
~Stack_str(); // Destructor
|
||||
|
||||
// Stack operations
|
||||
// bool isEmpty();
|
||||
bool push(string);
|
||||
// string pop();
|
||||
// string peek();
|
||||
// int getLength();
|
||||
};
|
||||
|
||||
/**~*~*~*
|
||||
Member function push: pushes the argument onto the stack.
|
||||
*~**/
|
||||
bool Stack_str::push(string item) {
|
||||
StackNode *newNode; // Pointer to a new node
|
||||
|
||||
// Allocate a new node and store item there.
|
||||
newNode = new StackNode;
|
||||
if (!newNode)
|
||||
return false;
|
||||
newNode->value = item;
|
||||
|
||||
// Update links and counter
|
||||
newNode->next = top;
|
||||
top = newNode;
|
||||
length++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**~*~*~*
|
||||
Destructor
|
||||
*~**/
|
||||
Stack_str::~Stack_str() {
|
||||
StackNode *currNode;
|
||||
|
||||
// Position nodePtr at the top of the stack.
|
||||
currNode = top;
|
||||
|
||||
// Traverse the list deleting each node.
|
||||
while (currNode) {
|
||||
StackNode* nextNode = currNode->next;
|
||||
cout << currNode->value << " - deleted!" << endl;
|
||||
delete currNode;
|
||||
currNode = NULL;
|
||||
currNode = nextNode;
|
||||
}
|
||||
cout << "Empty stack!" << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
Stack_str s;
|
||||
string item;
|
||||
|
||||
getline(cin, item);
|
||||
while (item != "0") {
|
||||
s.push(item);
|
||||
cin.clear();
|
||||
getline(cin, item);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
BIN
01-stacks/stacks_03_pop
Executable file
BIN
01-stacks/stacks_03_pop
Executable file
Binary file not shown.
102
01-stacks/stacks_03_pop.cpp
Normal file
102
01-stacks/stacks_03_pop.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
/**~*~*~*
|
||||
CIS 22C
|
||||
Project: Stack of strings (pop)
|
||||
|
||||
Written by: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
*~*/
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Stack_str {
|
||||
private:
|
||||
// Structure for the stack nodes
|
||||
struct StackNode {
|
||||
string value; // Value in the node
|
||||
StackNode *next; // Pointer to next node
|
||||
};
|
||||
|
||||
StackNode *top; // Pointer to the stack top
|
||||
int length;
|
||||
|
||||
public:
|
||||
Stack_str(){ top = NULL; length = 0; } //Constructor
|
||||
// ~Stack_str(); // Destructor
|
||||
|
||||
// Stack operations
|
||||
bool isEmpty() {
|
||||
return length == 0;
|
||||
}
|
||||
|
||||
bool push(string);
|
||||
|
||||
string pop();
|
||||
|
||||
string peek() {
|
||||
return top->value;
|
||||
}
|
||||
|
||||
int getLength() {
|
||||
return length;
|
||||
}
|
||||
};
|
||||
|
||||
/**~*~*~*
|
||||
Member function push: pushes the argument onto the stack.
|
||||
*~**/
|
||||
bool Stack_str::push(string item) {
|
||||
StackNode *newNode; // Pointer to a new node
|
||||
|
||||
// Allocate a new node and store num there.
|
||||
newNode = new StackNode;
|
||||
if (!newNode)
|
||||
return false;
|
||||
newNode->value = item;
|
||||
|
||||
// Update links and counter
|
||||
newNode->next = top;
|
||||
top = newNode;
|
||||
length++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**~*~*~*
|
||||
Member function pop pops the value at the top
|
||||
of the stack off, and returns it
|
||||
Assume stack is not empty
|
||||
*~**/
|
||||
string Stack_str::pop() {
|
||||
string val = this->top->value;
|
||||
|
||||
StackNode* oldTop = this->top;
|
||||
this->top = this->top->next;
|
||||
this->length--;
|
||||
delete oldTop;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
|
||||
Stack_str s;
|
||||
string item;
|
||||
|
||||
getline(cin, item);
|
||||
while (item != "0") {
|
||||
s.push(item);
|
||||
cin.clear();
|
||||
getline(cin, item);
|
||||
}
|
||||
|
||||
if (s.isEmpty()) cout << "Empty Stack!\n";
|
||||
|
||||
while (!s.isEmpty()) {
|
||||
cout << s.pop() << "\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
8
02-queues/.idea/.gitignore
generated
vendored
Normal file
8
02-queues/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
1
02-queues/.idea/.name
generated
Normal file
1
02-queues/.idea/.name
generated
Normal file
@ -0,0 +1 @@
|
||||
02_queues
|
2
02-queues/.idea/02-queues.iml
generated
Normal file
2
02-queues/.idea/02-queues.iml
generated
Normal file
@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
6
02-queues/.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
6
02-queues/.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
</profile>
|
||||
</component>
|
4
02-queues/.idea/misc.xml
generated
Normal file
4
02-queues/.idea/misc.xml
generated
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
</project>
|
8
02-queues/.idea/modules.xml
generated
Normal file
8
02-queues/.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/02-queues.iml" filepath="$PROJECT_DIR$/.idea/02-queues.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
02-queues/CMakeLists.txt
Normal file
6
02-queues/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
project(02_queues)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(02_queues main.cpp)
|
102
02-queues/QueueADT.h
Normal file
102
02-queues/QueueADT.h
Normal file
@ -0,0 +1,102 @@
|
||||
/*
|
||||
Written by: Iurii Tatishchev
|
||||
=================================
|
||||
Queue template
|
||||
*/
|
||||
#ifndef QUEUE_ADT_H
|
||||
#define QUEUE_ADT_H
|
||||
|
||||
template<class T>
|
||||
class Queue {
|
||||
private:
|
||||
// Structure for the stack nodes
|
||||
struct QueueNode {
|
||||
T value; // Value in the node
|
||||
QueueNode *next; // Pointer to next node
|
||||
};
|
||||
|
||||
QueueNode *front; // Pointer to the first node
|
||||
QueueNode *rear; // Pointer to the last node
|
||||
int length; // Number of nodes in the queue
|
||||
|
||||
public:
|
||||
Queue() { front = rear = nullptr; length = 0; } // Constructor
|
||||
~Queue(); // Destructor
|
||||
|
||||
// Queue operations
|
||||
bool isEmpty() { return length == 0; }
|
||||
|
||||
bool push(T);
|
||||
|
||||
T pop();
|
||||
|
||||
T peek() { return front->value; }
|
||||
|
||||
T peekRear() { return rear->value; }
|
||||
|
||||
int getLength() { return length; }
|
||||
};
|
||||
|
||||
/*
|
||||
Member function push: inserts the argument into the queue
|
||||
*/
|
||||
template<class T>
|
||||
bool Queue<T>::push(T item) {
|
||||
QueueNode *newNode; // Pointer to a new node
|
||||
|
||||
// Allocate a new node and store item there.
|
||||
newNode = new QueueNode;
|
||||
if (!newNode)
|
||||
return false;
|
||||
newNode->value = item;
|
||||
newNode->next = nullptr;
|
||||
|
||||
// Update links and counter
|
||||
if (!front) // front is NULL: empty queue
|
||||
front = newNode;
|
||||
else
|
||||
rear->next = newNode;
|
||||
|
||||
rear = newNode;
|
||||
length++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
Member function deletes the value at the front
|
||||
of the queue and returns it.
|
||||
Assume queue has at least one node
|
||||
*/
|
||||
template<class T>
|
||||
T Queue<T>::pop() {
|
||||
// Save item to return.
|
||||
T item = front->value;
|
||||
|
||||
// Save pointer to old front to delete it
|
||||
QueueNode* oldFront = front;
|
||||
|
||||
// Set front to next node and delete old front
|
||||
front = front->next;
|
||||
delete oldFront;
|
||||
|
||||
// Update length
|
||||
length--;
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
/*
|
||||
Destructor
|
||||
*/
|
||||
template<class T>
|
||||
Queue<T>::~Queue() {
|
||||
while (front != nullptr) {
|
||||
QueueNode* nextNode = front->next;
|
||||
delete front;
|
||||
front = nextNode;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
69
02-queues/main.cpp
Normal file
69
02-queues/main.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
Written by: Iurii Tatishchev
|
||||
=================================
|
||||
Project: Queue ADT
|
||||
Parallel queues. Create two queues:
|
||||
- the first queue contains the names of the students enrolled in CIS 22C
|
||||
- the second queue contains the number of units each student is taking this quarter
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "QueueADT.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
|
||||
// Create the first queue (strings)
|
||||
Queue<string> names;
|
||||
|
||||
// Loop to enter an unknown number of names, one per line.
|
||||
// The loop stops when you enter #.
|
||||
// As you are entering names, they are to be inserted into the first queue.
|
||||
string item;
|
||||
getline(cin, item);
|
||||
while (item != "#") {
|
||||
names.push(item);
|
||||
getline(cin, item);
|
||||
}
|
||||
|
||||
if (names.isEmpty()) {
|
||||
cout << "Empty Queues!\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Test the getLength function: - display the number of elements in the first queue
|
||||
cout << names.getLength();
|
||||
|
||||
// Create the second queue (doubles)
|
||||
Queue<double> numbers;
|
||||
|
||||
// Test the getLength function: - display the number of elements in the second queue
|
||||
// (it should be 0!)
|
||||
cout << " " << numbers.getLength() << "\n";
|
||||
|
||||
// Write another loop to enter the number of units (double) into a parallel queue.
|
||||
for (int i = 0; i < names.getLength(); i++) {
|
||||
double units;
|
||||
cin >> units;
|
||||
numbers.push(units);
|
||||
}
|
||||
|
||||
// Display the two queues in parallel.
|
||||
for (int i = names.getLength(); i > 0; i--) {
|
||||
names.push(names.pop());
|
||||
numbers.push(numbers.pop());
|
||||
cout << names.peekRear() << " " << numbers.peekRear() << "\n";
|
||||
}
|
||||
|
||||
// On the next line display the front and rear elements in the first queue.
|
||||
cout << "Front of the queues: " << names.peek() << " " << numbers.peek() << "\n";
|
||||
|
||||
// On the last line display the front and the rear elements in the second queue.
|
||||
cout << "Rear of the queues: " << names.peekRear() << " " << numbers.peekRear() << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
86
02-queues/queues_01_strings.cpp
Normal file
86
02-queues/queues_01_strings.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
/**~*~*~*
|
||||
CIS 22C
|
||||
Project: Queue of strings
|
||||
|
||||
Written by: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
*~*/
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
class Queue_str
|
||||
{
|
||||
private:
|
||||
// Structure for the queue nodes
|
||||
struct QueueNode {
|
||||
string value; // Value in the node
|
||||
QueueNode *next; // Pointer to next node
|
||||
};
|
||||
|
||||
QueueNode *front; // Pointer to the first node
|
||||
QueueNode *rear; // Pointer to the last node
|
||||
int length; // Number of nodes in the queue
|
||||
|
||||
public:
|
||||
Queue_str(){ front = rear = NULL; length = 0; } //Constructor
|
||||
//~Queue_str(); // Destructor
|
||||
|
||||
// Queue operations
|
||||
bool isEmpty() { return length == 0; }
|
||||
bool push(string);
|
||||
// string pop();
|
||||
string peek() { return front->value; }
|
||||
string peekRear() { return rear->value; }
|
||||
int getLength() { return length; }
|
||||
};
|
||||
|
||||
/**~*~*
|
||||
Member function push: inserts the argument into the queue
|
||||
*~**/
|
||||
bool Queue_str::push(string item)
|
||||
{
|
||||
QueueNode *newNode; // Pointer to a new node
|
||||
|
||||
// Allocate a new node and store num there.
|
||||
newNode = new QueueNode;
|
||||
if (!newNode)
|
||||
return false;
|
||||
newNode->value = item;
|
||||
newNode->next = NULL;
|
||||
|
||||
// Update links and counter
|
||||
if (!front) // front is NULL: empty queue
|
||||
front = newNode;
|
||||
else
|
||||
rear->next = newNode;
|
||||
|
||||
rear = newNode;
|
||||
length++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
Queue_str que;
|
||||
string item;
|
||||
|
||||
getline(cin, item);
|
||||
while (item != "#") {
|
||||
que.push(item);
|
||||
getline(cin, item);
|
||||
}
|
||||
|
||||
cout << que.getLength() << "\n";
|
||||
if (que.isEmpty()) {
|
||||
cout << "Empty Queue!\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
cout << que.peek() << "\n";
|
||||
cout << que.peekRear() << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
93
02-queues/queues_02_destructor.cpp
Normal file
93
02-queues/queues_02_destructor.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
/**~*~*~*
|
||||
CIS 22C
|
||||
Project: Queue of strings
|
||||
|
||||
Written by: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
*~*/
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Queue_str {
|
||||
private:
|
||||
// Structure for the queue nodes
|
||||
struct QueueNode {
|
||||
string value; // Value in the node
|
||||
QueueNode *next; // Pointer to next node
|
||||
};
|
||||
|
||||
QueueNode *front; // Pointer to the first node
|
||||
QueueNode *rear; // Pointer to the last node
|
||||
int length; // Number of nodes in the queue
|
||||
|
||||
public:
|
||||
Queue_str() { front = rear = NULL; length = 0; } //Constructor
|
||||
~Queue_str(); // Destructor
|
||||
|
||||
// Queue operations
|
||||
bool isEmpty() { return length == 0; }
|
||||
|
||||
bool push(string);
|
||||
|
||||
// string pop();
|
||||
string peek() { return front->value; }
|
||||
|
||||
string peekRear() { return rear->value; }
|
||||
|
||||
int getLength() { return length; }
|
||||
};
|
||||
|
||||
/**~*~*
|
||||
Member function push: inserts the argument into the queue
|
||||
*~**/
|
||||
bool Queue_str::push(string item) {
|
||||
QueueNode *newNode; // Pointer to a new node
|
||||
|
||||
// Allocate a new node and store num there.
|
||||
newNode = new QueueNode;
|
||||
if (!newNode)
|
||||
return false;
|
||||
newNode->value = item;
|
||||
newNode->next = NULL;
|
||||
|
||||
// Update links and counter
|
||||
if (!front) // front is NULL: empty queue
|
||||
front = newNode;
|
||||
else
|
||||
rear->next = newNode;
|
||||
|
||||
rear = newNode;
|
||||
length++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**~*~*~*
|
||||
Destructor
|
||||
*~**/
|
||||
Queue_str::~Queue_str()
|
||||
{
|
||||
while (front != nullptr) {
|
||||
QueueNode* nextNode = front->next;
|
||||
cout << front->value << " - deleted!" << endl;
|
||||
delete front;
|
||||
front = nextNode;
|
||||
}
|
||||
|
||||
cout << "Empty queue!" << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Queue_str que;
|
||||
string item;
|
||||
|
||||
getline(cin, item);
|
||||
while (item != "#") {
|
||||
que.push(item);
|
||||
getline(cin, item);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
BIN
02-queues/queues_03_pop
Executable file
BIN
02-queues/queues_03_pop
Executable file
Binary file not shown.
113
02-queues/queues_03_pop.cpp
Normal file
113
02-queues/queues_03_pop.cpp
Normal file
@ -0,0 +1,113 @@
|
||||
/**~*~*~*
|
||||
CIS 22C
|
||||
Project: Queue of strings (pop)
|
||||
|
||||
Written by: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
*~*/
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Queue_str {
|
||||
private:
|
||||
// Structure for the queue nodes
|
||||
struct QueueNode {
|
||||
string value; // Value in the node
|
||||
QueueNode *next; // Pointer to next node
|
||||
};
|
||||
|
||||
QueueNode *front; // Pointer to the first node
|
||||
QueueNode *rear; // Pointer to the last node
|
||||
int length; // Number of nodes in the queue
|
||||
|
||||
public:
|
||||
Queue_str() { front = rear = NULL; length = 0; } //Constructor
|
||||
~Queue_str(); // Destructor
|
||||
|
||||
// Queue operations
|
||||
bool isEmpty() { return length == 0; }
|
||||
|
||||
bool push(string);
|
||||
|
||||
string pop();
|
||||
string peek() { return front->value; }
|
||||
|
||||
string peekRear() { return rear->value; }
|
||||
|
||||
int getLength() { return length; }
|
||||
};
|
||||
|
||||
/**~*~*
|
||||
Member function push: inserts the argument into the queue
|
||||
*~**/
|
||||
bool Queue_str::push(string item) {
|
||||
QueueNode *newNode; // Pointer to a new node
|
||||
|
||||
// Allocate a new node and store num there.
|
||||
newNode = new QueueNode;
|
||||
if (!newNode)
|
||||
return false;
|
||||
newNode->value = item;
|
||||
newNode->next = NULL;
|
||||
|
||||
// Update links and counter
|
||||
if (!front) // front is NULL: empty queue
|
||||
front = newNode;
|
||||
else
|
||||
rear->next = newNode;
|
||||
|
||||
rear = newNode;
|
||||
length++;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**~*~*~*
|
||||
Destructor
|
||||
*~**/
|
||||
Queue_str::~Queue_str() {
|
||||
while (front != nullptr) {
|
||||
QueueNode* nextNode = front->next;
|
||||
delete front;
|
||||
front = nextNode;
|
||||
}
|
||||
}
|
||||
|
||||
/**~*~*~*
|
||||
Member function dequeue deletes the value at the front
|
||||
of the queue and returns it.
|
||||
Assume queue has at least one node
|
||||
*~**/
|
||||
string Queue_str::pop() {
|
||||
string item = front->value;
|
||||
QueueNode* oldFront = front;
|
||||
front = front->next;
|
||||
delete oldFront;
|
||||
length--;
|
||||
return item;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
Queue_str que;
|
||||
string item;
|
||||
|
||||
getline(cin, item);
|
||||
while (item != "#") {
|
||||
que.push(item);
|
||||
getline(cin, item);
|
||||
}
|
||||
|
||||
if (que.isEmpty()) {
|
||||
cout << "Empty Queue!\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (!que.isEmpty()) {
|
||||
cout << que.pop() << "\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
8
03-lists/.idea/.gitignore
generated
vendored
Normal file
8
03-lists/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
1
03-lists/.idea/.name
generated
Normal file
1
03-lists/.idea/.name
generated
Normal file
@ -0,0 +1 @@
|
||||
03_lists
|
2
03-lists/.idea/03_lists.iml
generated
Normal file
2
03-lists/.idea/03_lists.iml
generated
Normal file
@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
6
03-lists/.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
6
03-lists/.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
</profile>
|
||||
</component>
|
4
03-lists/.idea/misc.xml
generated
Normal file
4
03-lists/.idea/misc.xml
generated
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
</project>
|
8
03-lists/.idea/modules.xml
generated
Normal file
8
03-lists/.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/03_lists.iml" filepath="$PROJECT_DIR$/.idea/03_lists.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
53
03-lists/01_insert.cpp
Normal file
53
03-lists/01_insert.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
CIS 22C
|
||||
|
||||
This program builds and displays a sorted list.
|
||||
The list is sorted in ascending order by name.
|
||||
|
||||
Requirement:
|
||||
Change the insertNode() function to sort the list in descending order by gpa.
|
||||
|
||||
Written by: Iurii Tatishchev
|
||||
Reviewed & Modified by: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "StudentList.h"
|
||||
using namespace std;
|
||||
|
||||
void buildList(StudentList &);
|
||||
|
||||
int main()
|
||||
{
|
||||
// Define a StudentList object
|
||||
StudentList list;
|
||||
|
||||
buildList(list);
|
||||
list.displayList();
|
||||
|
||||
return 0;
|
||||
}
|
||||
/* **************************************************
|
||||
This function builds a sorted linked list with data from the keyboard.
|
||||
The list is sorted in ascending order by the students' names.
|
||||
It calls the insertNode() function that inserts the new data at the right location in the linked list.
|
||||
An input line contains the gpa of a student follow by its name (assume it is a single word name)
|
||||
To stop reading enter "#"
|
||||
************************************************** */
|
||||
void buildList(StudentList &list)
|
||||
{
|
||||
string line;
|
||||
getline(cin, line);
|
||||
while (line != "#")
|
||||
{
|
||||
stringstream temp(line); // create a stringstream named temp with data from line
|
||||
Student newStu;
|
||||
temp >> newStu.gpa; // read gpa from temp
|
||||
temp >> newStu.name; // read name from temp
|
||||
list.insertNode(newStu);
|
||||
getline(cin, line);
|
||||
}
|
||||
|
||||
}
|
82
03-lists/04_student_node_class.cpp
Normal file
82
03-lists/04_student_node_class.cpp
Normal file
@ -0,0 +1,82 @@
|
||||
/*
|
||||
CIS 22C
|
||||
|
||||
This program builds and displays a sorted list.
|
||||
The list is sorted in ascending order by name.
|
||||
|
||||
Requirement:
|
||||
Change the insertNode() function to sort the list in descending order by gpa.
|
||||
|
||||
Written by: Iurii Tatishchev
|
||||
Reviewed & Modified by: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "StudentList.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void buildList(StudentList &);
|
||||
|
||||
void deleteTestDriver(StudentList &);
|
||||
|
||||
int main() {
|
||||
// Define a StudentList object
|
||||
StudentList list;
|
||||
|
||||
buildList(list); // insert data into the list
|
||||
|
||||
list.displayList();
|
||||
string answer;
|
||||
cout << "Test Delete [Y/N]?\n";
|
||||
cin >> answer;
|
||||
if (answer == "Y" || answer == "y") {
|
||||
deleteTestDriver(list);
|
||||
list.displayList();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ***************************************************
|
||||
// This function builds a sorted linked list with data from the keyboard.
|
||||
// The list is sorted in ascending order by the students' names.
|
||||
// It calls the insertNode() function that inserts the new data at the right location in the linked list.
|
||||
// An input line contains the gpa of a student follow by its name (assume it is a single word name)
|
||||
// To stop reading enter "#"
|
||||
// ***************************************************
|
||||
void buildList(StudentList &list) {
|
||||
string line, name;
|
||||
double gpa;
|
||||
|
||||
getline(cin, line);
|
||||
while (line != "#") {
|
||||
stringstream temp(line); // create a stringstream named temp with data from line
|
||||
temp >> gpa; // read gpa from temp
|
||||
temp >> name; // read name from temp
|
||||
Student newStu(gpa, name);
|
||||
list.insertNode(newStu);
|
||||
getline(cin, line);
|
||||
}
|
||||
}
|
||||
|
||||
// ***************************************************
|
||||
// This function is a test driver for the
|
||||
// linked list delete function
|
||||
// ***************************************************
|
||||
void deleteTestDriver(StudentList &list) {
|
||||
string toDelete;
|
||||
|
||||
cout << "Enter strings to be deleted (# to stop)" << endl;
|
||||
cin >> toDelete;
|
||||
while (toDelete != "#") {
|
||||
cout << " " << toDelete;
|
||||
if (list.deleteNode(toDelete))
|
||||
cout << " - deleted\n";
|
||||
else
|
||||
cout << " - not found\n";
|
||||
cin >> toDelete;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
8
03-lists/CMakeLists.txt
Normal file
8
03-lists/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
project(03_lists)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(03_lists main.cpp StudentList.cpp
|
||||
LinkedList.cpp
|
||||
Park.cpp)
|
BIN
03-lists/Lab Singly-Linked Lists (Park).zip
Normal file
BIN
03-lists/Lab Singly-Linked Lists (Park).zip
Normal file
Binary file not shown.
157
03-lists/LinkedList.cpp
Normal file
157
03-lists/LinkedList.cpp
Normal file
@ -0,0 +1,157 @@
|
||||
// Implementation file for the LinkedList class
|
||||
// Written By: Iurii Tatishchev
|
||||
// Reviewed by: Iurii Tatishchev
|
||||
// IDE: CLion
|
||||
|
||||
#include <iostream>
|
||||
#include "LinkedList.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
// **************************************************
|
||||
// Constructor
|
||||
// This function allocates and initializes a sentinel node
|
||||
// A sentinel (or dummy) node is an extra node added before the first data record.
|
||||
// This convention simplifies and accelerates some list-manipulation algorithms,
|
||||
// by making sure that all links can be safely dereferenced and that every list
|
||||
// (even one that contains no data elements) always has a "first" node.
|
||||
// **************************************************
|
||||
LinkedList::LinkedList() {
|
||||
head = new Node; // head points to the sentinel node
|
||||
head->next = NULL;
|
||||
length = 0;
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// The insertNode() function inserts a new node in a
|
||||
// sorted linked list
|
||||
// **************************************************
|
||||
void LinkedList::insertNode(Park dataIn) {
|
||||
Node *newNode; // A new node
|
||||
Node *pCur; // To traverse the list
|
||||
Node *pPre; // The previous node
|
||||
|
||||
// Allocate a new node and store dataIn there.
|
||||
newNode = new Node;
|
||||
newNode->park = dataIn;
|
||||
|
||||
// Initialize pointers
|
||||
pPre = head;
|
||||
pCur = head->next;
|
||||
|
||||
// Find location: skip all nodes whose code is less than dataIn's code
|
||||
while (pCur && newNode->park.getCode() > pCur->park.getCode()) {
|
||||
pPre = pCur;
|
||||
pCur = pCur->next;
|
||||
}
|
||||
|
||||
// Insert the new node between pPre and pCur
|
||||
pPre->next = newNode;
|
||||
newNode->next = pCur;
|
||||
|
||||
// Update the counter
|
||||
length++;
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// The deleteNode() function searches for a node
|
||||
// in a sorted linked list; if found, the node is
|
||||
// deleted from the list and from memory.
|
||||
// **************************************************
|
||||
bool LinkedList::deleteNode(string target) {
|
||||
Node *pCur; // To traverse the list
|
||||
Node *pPre; // To point to the previous node
|
||||
bool deleted = false;
|
||||
|
||||
// Initialize pointers
|
||||
pPre = head;
|
||||
pCur = head->next;
|
||||
|
||||
// Find node containing the target: Skip all nodes whose code is less than the target
|
||||
while (pCur != NULL && pCur->park.getCode() < target) {
|
||||
pPre = pCur;
|
||||
pCur = pCur->next;
|
||||
}
|
||||
|
||||
// If found, delete the node
|
||||
if (pCur && pCur->park.getCode() == target) {
|
||||
pPre->next = pCur->next;
|
||||
delete pCur;
|
||||
deleted = true;
|
||||
length--;
|
||||
}
|
||||
return deleted;
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// displayList() shows the value
|
||||
// stored in each node of the linked list
|
||||
// pointed to by head, except the sentinel node
|
||||
// **************************************************
|
||||
void LinkedList::displayList() const {
|
||||
Node *pCur; // To move through the list
|
||||
|
||||
// Position pCur: skip the head of the list.
|
||||
pCur = head->next;
|
||||
|
||||
// While pCur points to a node, traverse the list.
|
||||
while (pCur) {
|
||||
// Display the value in this node.
|
||||
pCur->park.hDdisplay();
|
||||
|
||||
// Move to the next node.
|
||||
pCur = pCur->next;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// The searchList() function looks for a target park
|
||||
// in the sorted linked list: if found, returns true
|
||||
// and copies the data in that node to the output parameter
|
||||
// **************************************************
|
||||
bool LinkedList::searchList(string target, Park &dataOut) const {
|
||||
bool found = false; // assume target not found
|
||||
Node *pCur; // To move through the list
|
||||
|
||||
// Position pCur: skip the head of the list.
|
||||
pCur = head->next;
|
||||
|
||||
// While pCur points to a node, traverse the list.
|
||||
while (pCur && pCur->park.getCode() != target) {
|
||||
pCur = pCur->next;
|
||||
}
|
||||
|
||||
// If the target was found, copy the data
|
||||
if (pCur) {
|
||||
dataOut = pCur->park;
|
||||
found = true;
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// Destructor
|
||||
// This function deletes every node in the list.
|
||||
// **************************************************
|
||||
LinkedList::~LinkedList() {
|
||||
Node *pCur; // To traverse the list
|
||||
Node *pNext; // To hold the address of the next node
|
||||
|
||||
// Position nodePtr: skip the head of the list
|
||||
pCur = head->next;
|
||||
// While pCur is not at the end of the list...
|
||||
while (pCur != NULL) {
|
||||
// Save a pointer to the next node.
|
||||
pNext = pCur->next;
|
||||
|
||||
// Delete the current node.
|
||||
delete pCur;
|
||||
|
||||
// Position pCur at the next node.
|
||||
pCur = pNext;
|
||||
}
|
||||
|
||||
delete head; // delete the sentinel node
|
||||
}
|
37
03-lists/LinkedList.h
Normal file
37
03-lists/LinkedList.h
Normal file
@ -0,0 +1,37 @@
|
||||
// Specification file for the LinkedList class
|
||||
// Written By: Iurii Tatishchev
|
||||
// Reviewed by: Iurii Tatishchev
|
||||
// IDE: CLion
|
||||
|
||||
#ifndef LINKED_LIST_H
|
||||
#define LINKED_LIST_H
|
||||
|
||||
#include "Park.h"
|
||||
|
||||
class LinkedList {
|
||||
private:
|
||||
struct Node {
|
||||
Park park;
|
||||
Node *next;
|
||||
};
|
||||
|
||||
Node *head;
|
||||
int length;
|
||||
|
||||
public:
|
||||
LinkedList(); // constructor
|
||||
~LinkedList(); // destructor
|
||||
|
||||
// Linked list operations
|
||||
int getLength() const { return length; }
|
||||
|
||||
void insertNode(Park);
|
||||
|
||||
bool deleteNode(string);
|
||||
|
||||
void displayList() const;
|
||||
|
||||
bool searchList(string, Park &) const;
|
||||
};
|
||||
|
||||
#endif
|
34
03-lists/ListNode.h
Normal file
34
03-lists/ListNode.h
Normal file
@ -0,0 +1,34 @@
|
||||
// Specification file for the ListNode class
|
||||
// Written by: A. Student
|
||||
// Reviewed by: <Write your name here>
|
||||
// IDE
|
||||
|
||||
#ifndef LISTNODE_H
|
||||
#define LISTNODE_H
|
||||
|
||||
#include <iostream>
|
||||
#include "Student.h"
|
||||
|
||||
using std::string;
|
||||
|
||||
class ListNode {
|
||||
private:
|
||||
Student stu; // store data
|
||||
ListNode *next; // a pointer to the next node in the list
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
ListNode(const Student &dataIn, ListNode *next = NULL) { stu = dataIn; }
|
||||
|
||||
// setters
|
||||
// set a pointer to a pointer in the node
|
||||
void setNext(ListNode *nextPtr) { next = nextPtr; }
|
||||
|
||||
// getters
|
||||
// return pointer in the node
|
||||
ListNode *getNext() const { return next; }
|
||||
|
||||
Student getData() { return stu; } // return stu object in the listnode
|
||||
};
|
||||
|
||||
#endif
|
58
03-lists/Park.cpp
Normal file
58
03-lists/Park.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
// Implementation file for the Park class
|
||||
// Written By: Iurii Tatishchev
|
||||
// Reviewed by: Iurii Tatishchev
|
||||
// IDE: CLion
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
|
||||
#include "Park.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
// **************************************************
|
||||
// Constructor
|
||||
// **************************************************
|
||||
Park::Park() {
|
||||
code = "";
|
||||
state = "";
|
||||
name = "";
|
||||
description = "";
|
||||
year = -1;
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// Overloaded Constructor
|
||||
// **************************************************
|
||||
Park::Park(string cd, string st, string nm, string dsc, int yr) {
|
||||
code = cd;
|
||||
state = st;
|
||||
name = nm;
|
||||
description = dsc;
|
||||
year = yr;
|
||||
}
|
||||
|
||||
// ***********************************************************
|
||||
// Displays the values of a Park object member variables
|
||||
// on one line (horizontal display)
|
||||
// ***********************************************************
|
||||
void Park::hDdisplay() const {
|
||||
cout << code << " ";
|
||||
cout << state << " ";
|
||||
cout << year << " ";
|
||||
cout << name << " " << endl;
|
||||
}
|
||||
|
||||
// ***********************************************************
|
||||
// Displays the values of a Park object member variables
|
||||
// one per line (vertical display)
|
||||
// ***********************************************************
|
||||
void Park::vDisplay() const {
|
||||
cout << name << endl;
|
||||
cout << " \"" << description << "\"" << endl;
|
||||
cout << year << endl;
|
||||
cout << state << endl;
|
||||
}
|
||||
|
64
03-lists/Park.h
Normal file
64
03-lists/Park.h
Normal file
@ -0,0 +1,64 @@
|
||||
// Specification file for the Park class
|
||||
// Written By: Iurii Tatishchev
|
||||
// Reviewed by: Iurii Tatishchev
|
||||
// IDE: CLion
|
||||
|
||||
#ifndef PARK_H
|
||||
#define PARK_H
|
||||
|
||||
// #include<iostream>
|
||||
#include<string>
|
||||
// #include<cstdlib>
|
||||
|
||||
// using namespace std;
|
||||
// ^^^^^ This statement
|
||||
// in a header file of a complex project could create
|
||||
// namespace management problems for the entire project
|
||||
// (such as name collisions).
|
||||
// Do not write using namespace at the top level in a header file!
|
||||
|
||||
using std::string;
|
||||
|
||||
class Park {
|
||||
private:
|
||||
string code; // the unique park identifier
|
||||
string state;
|
||||
string name;
|
||||
string description;
|
||||
int year;
|
||||
|
||||
public:
|
||||
//constructors
|
||||
Park();
|
||||
|
||||
Park(string, string, string, string, int);
|
||||
|
||||
//setters
|
||||
void setCode(string cd) { code = cd; }
|
||||
|
||||
void setState(string st) { state = st; }
|
||||
|
||||
void setName(string nm) { name = nm; }
|
||||
|
||||
void setDesc(int dsc) { description = dsc; }
|
||||
|
||||
void setYear(int yr) { year = yr; }
|
||||
|
||||
//getters
|
||||
string getCode() const { return code; }
|
||||
|
||||
string getState() const { return state; }
|
||||
|
||||
string getName() const { return name; }
|
||||
|
||||
string getDesc() const { return description; }
|
||||
|
||||
int getYear() const { return year; }
|
||||
|
||||
//other functions
|
||||
void hDdisplay() const;
|
||||
|
||||
void vDisplay() const;
|
||||
};
|
||||
|
||||
#endif
|
34
03-lists/Student.h
Normal file
34
03-lists/Student.h
Normal file
@ -0,0 +1,34 @@
|
||||
// Specification file for the Student class
|
||||
// Written by: Iurii Tatishchev
|
||||
// Reviewed & Modified by: Iurii Tatishchev
|
||||
// IDE: CLion
|
||||
|
||||
#ifndef STUDENT_H
|
||||
#define STUDENT_H
|
||||
|
||||
//using namespace std; //<==== This statement
|
||||
// in a header file of a complex project could create
|
||||
// namespace management problems for the entire project
|
||||
// (such as name collisions).
|
||||
// Do not write namespace using statements at the top level in a header file!
|
||||
|
||||
#include <string>
|
||||
using std::string;
|
||||
|
||||
class Student {
|
||||
private:
|
||||
double gpa;
|
||||
string name;
|
||||
|
||||
public:
|
||||
Student() { gpa = 0 ; name = ""; }
|
||||
Student(double gpa, string name) { this->gpa = gpa; this->name = name; }
|
||||
|
||||
// Setters and getters
|
||||
void setGpa(double gpa) { this->gpa = gpa; }
|
||||
void setName(string name) { this->name = name; }
|
||||
double getGpa() const { return gpa; }
|
||||
string getName() const { return name; }
|
||||
|
||||
};
|
||||
#endif
|
130
03-lists/StudentList.cpp
Normal file
130
03-lists/StudentList.cpp
Normal file
@ -0,0 +1,130 @@
|
||||
// Implementation file for the Student List class
|
||||
// Written by: Iurii Tatishchev
|
||||
// Reviewed, Debugged, & Modified by: Iurii Tatishchev
|
||||
// IDE: CLion
|
||||
|
||||
#include <iostream> // For cout and NULL
|
||||
#include "StudentList.h"
|
||||
using namespace std;
|
||||
|
||||
// **************************************************
|
||||
// Constructor
|
||||
// This function allocates and initializes a sentinel node
|
||||
// A sentinel (or dummy) node is an extra node added before the first data record.
|
||||
// This convention simplifies and accelerates some list-manipulation algorithms,
|
||||
// by making sure that all links can be safely dereferenced and that every list
|
||||
// (even one that contains no data elements) always has a "first" node.
|
||||
// **************************************************
|
||||
StudentList::StudentList() {
|
||||
head = new ListNode(Student()); // head points to the sentinel node
|
||||
head->setNext(NULL);
|
||||
count = 0;
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// displayList shows the value
|
||||
// stored in each node of the linked list
|
||||
// pointed to by head.
|
||||
// **************************************************
|
||||
void StudentList::displayList() const {
|
||||
ListNode *pCur; // To move through the list
|
||||
|
||||
// Position pCur: skip the head of the list.
|
||||
pCur = head->getNext();
|
||||
|
||||
// While pCur points to a node, traverse the list.
|
||||
while (pCur)
|
||||
{
|
||||
// Display the value in this node.
|
||||
// cout << pCur->stu.getGpa() << " " << pCur->stu.getName() << endl;
|
||||
cout << pCur->getData().getGpa() << " " << pCur->getData().getName() << endl;
|
||||
|
||||
// Move to the next node.
|
||||
pCur = pCur->getNext();
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// The insertNode function inserts a node with
|
||||
// dataIn copied to its stu member.
|
||||
// **************************************************
|
||||
void StudentList::insertNode(Student dataIn) {
|
||||
ListNode *newNode; // A new node
|
||||
ListNode *pCur; // To traverse the list
|
||||
ListNode *pPre; // The previous node
|
||||
|
||||
// Allocate a new node and store num there.
|
||||
newNode = new ListNode(dataIn);
|
||||
|
||||
// Initialize pointers
|
||||
pPre = head;
|
||||
pCur = head->getNext();
|
||||
|
||||
// Find location: skip all nodes are alphabetically less than dataIn
|
||||
while (pCur != NULL && pCur->getData().getName() < dataIn.getName()) {
|
||||
pPre = pCur;
|
||||
pCur = pCur->getNext();
|
||||
}
|
||||
|
||||
// Insert the new node between pPre and pCur
|
||||
pPre->setNext(newNode);
|
||||
newNode->setNext(pCur);
|
||||
|
||||
// Update the counter
|
||||
count++;
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// The deleteNode function searches for a node
|
||||
// with target as its value. The node, if found, is
|
||||
// deleted from the list and from memory.
|
||||
// **************************************************
|
||||
bool StudentList::deleteNode(string target) {
|
||||
ListNode *pCur; // To traverse the list
|
||||
ListNode *pPre; // To point to the previous node
|
||||
bool deleted = false;
|
||||
|
||||
// Initialize pointers
|
||||
pPre = head;
|
||||
pCur = head->getNext();
|
||||
// Find node containing the target: Skip all nodes whose name is less than the target
|
||||
while (pCur != NULL && pCur->getData().getName() < target) {
|
||||
pPre = pCur;
|
||||
pCur = pCur->getNext();
|
||||
}
|
||||
|
||||
// If found, delete the node
|
||||
if (pCur != NULL && pCur->getData().getName() == target) {
|
||||
pPre->setNext(pCur->getNext());
|
||||
delete pCur;
|
||||
deleted = true;
|
||||
count--;
|
||||
}
|
||||
return deleted;
|
||||
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// Destructor
|
||||
// This function deletes every node in the list.
|
||||
// **************************************************
|
||||
StudentList::~StudentList() {
|
||||
ListNode *pCur; // To traverse the list
|
||||
ListNode *pNext; // To point to the next node
|
||||
|
||||
// Position nodePtr at the head of the list.
|
||||
pCur = head->getNext();
|
||||
// While pCur is not at the end of the list...
|
||||
while (pCur != NULL) {
|
||||
// Save a pointer to the next node.
|
||||
pNext = pCur->getNext();
|
||||
// Delete the current node
|
||||
delete pCur;
|
||||
|
||||
// Position pCur at the next node.
|
||||
pCur = pNext;
|
||||
}
|
||||
delete head; // delete the sentinel node
|
||||
|
||||
}
|
29
03-lists/StudentList.h
Normal file
29
03-lists/StudentList.h
Normal file
@ -0,0 +1,29 @@
|
||||
// Specification file for the StudentList class
|
||||
// Written by: Iurii Tatishchev
|
||||
// Reviewed by: Iurii Tatishchev
|
||||
// IDE: CLion
|
||||
|
||||
#ifndef STUDENTLIST_H
|
||||
#define STUDENTLIST_H
|
||||
|
||||
#include "ListNode.h"
|
||||
|
||||
class StudentList {
|
||||
private:
|
||||
ListNode *head; // List head pointer
|
||||
int count; // To keep track of the number of nodes in the list
|
||||
|
||||
public:
|
||||
StudentList(); // Constructor
|
||||
~StudentList(); // Destructor
|
||||
|
||||
// Linked list operations
|
||||
int getCount() const { return count; }
|
||||
void insertNode(Student);
|
||||
bool deleteNode(string);
|
||||
//void searchList() const;
|
||||
void displayList() const;
|
||||
|
||||
};
|
||||
|
||||
#endif // STUDENTLIST_H
|
161
03-lists/main.cpp
Normal file
161
03-lists/main.cpp
Normal file
@ -0,0 +1,161 @@
|
||||
/*
|
||||
CIS 22C: Homework 3
|
||||
|
||||
Build and procees a sorted linked list of Park objects.
|
||||
The list is sorted in ascending order by the park code.
|
||||
Assume that the park code is unique.
|
||||
|
||||
Written by: Iurii Tatishchev
|
||||
Reviewed & Modified by: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
Reads data about National Parks from a text file and inserts them into a sorted linked list. The list is sorted in ascending order by the unique park identifier. For instance, the unique park identifier for the Arches National Park is ARC.
|
||||
Displays the list and the number of parks in the list.
|
||||
Searches the list: prompts the user to enter a park code (i.e the unique park identifier), searches for that code: if found, displays related data, otherwise displays an error message, then searches again for another code, until the user enter “Q” to stop searching.
|
||||
Deletes nodes from the list: prompts the user to enter a code, searches for that code: if found, it removes it from the list, otherwise displays an error message, then searches again for another code, until the user enter “Q” to stop deleting.
|
||||
|
||||
Read and understand this program (draw UML diagrams, the linked list, and hierarchy charts). Then do the following:
|
||||
|
||||
In main.cpp: provide calling statements for the basic linked list functions
|
||||
Build and run the program: search will not work, but display and delete should work
|
||||
In LinkedList.cpp: finish writing the searchList() function
|
||||
Build and run the program: now search should work too
|
||||
In Park.h and Park.cpp overload the stream insertion operator. The overloaded operator is going to replace the hDisplay() function in the Park class, and it is going to be used in LinkedList.cpp, in displayList() as shown below
|
||||
Build and run the program
|
||||
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include "LinkedList.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void buildList(const string &filename, LinkedList &list);
|
||||
|
||||
void deleteManager(LinkedList &list);
|
||||
|
||||
void searchManager(const LinkedList &list);
|
||||
|
||||
void displayManager(const LinkedList &list);
|
||||
|
||||
int main() {
|
||||
|
||||
string inputFileName = "national_parks.txt";
|
||||
LinkedList list;
|
||||
|
||||
buildList(inputFileName, list);
|
||||
displayManager(list);
|
||||
searchManager(list);
|
||||
deleteManager(list);
|
||||
displayManager(list);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
This function reads data about national parks from a file and inserts them
|
||||
into a sorted linked list. The list is sorted in ascending order by code.
|
||||
*/
|
||||
void buildList(const string &filename, LinkedList &list) {
|
||||
ifstream inputFile(filename);
|
||||
cout << "Reading data from \"" << filename << "\"" << endl;
|
||||
|
||||
if (!inputFile) {
|
||||
cout << "Error opening the input file: \"" << filename << "\"" << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
string line;
|
||||
while (getline(inputFile, line)) {
|
||||
int year;
|
||||
string code, name, state, dsc;
|
||||
|
||||
stringstream temp(line); // create temp with data from line
|
||||
temp >> code; // read from temp
|
||||
temp >> state;
|
||||
temp >> year;
|
||||
temp.ignore(); // to ignore space in front of name
|
||||
getline(temp, name, ';'); // stop reading name at ';'
|
||||
temp.ignore(); // to ignore space in front of description
|
||||
getline(temp, dsc);
|
||||
// create a Park object and initialize it with data from file
|
||||
Park aPark(code, state, name, dsc, year);
|
||||
list.insertNode(aPark);
|
||||
}
|
||||
|
||||
inputFile.close();
|
||||
}
|
||||
|
||||
/*
|
||||
Delete manager: delete items from the list until the user enters Q to quit
|
||||
deleting
|
||||
Input Parameter: list
|
||||
*/
|
||||
void deleteManager(LinkedList &list) {
|
||||
string targetCode = "";
|
||||
|
||||
cout << endl << " Delete" << endl;
|
||||
cout << "=======" << endl;
|
||||
|
||||
while (targetCode != "Q") {
|
||||
cout << "Enter a park code (or Q to stop deleting):" << endl;
|
||||
cin >> targetCode;
|
||||
|
||||
if (targetCode != "Q") {
|
||||
if (list.deleteNode(targetCode))
|
||||
cout << " " << targetCode << " has been deleted!" << endl;
|
||||
else
|
||||
cout << "Park \"" << targetCode << "\" was not found in this list." << endl;
|
||||
}
|
||||
}
|
||||
cout << "___________________END DELETE SECTION_____" << endl;
|
||||
}
|
||||
|
||||
/*
|
||||
Search manager: search the list until the user enters Q to quit searching
|
||||
Input Parameter: list
|
||||
*/
|
||||
void searchManager(const LinkedList &list) {
|
||||
string targetCode = "";
|
||||
Park aPark;
|
||||
|
||||
cout << endl << " Search" << endl;
|
||||
cout << "=======" << endl;
|
||||
|
||||
while (targetCode != "Q") {
|
||||
cout << "Enter a park code (or Q to stop searching):" << endl;
|
||||
cin >> targetCode;
|
||||
|
||||
if (targetCode != "Q") {
|
||||
if (list.searchList(targetCode, aPark))
|
||||
aPark.vDisplay();
|
||||
else
|
||||
cout << "Park \"" << targetCode << "\" was not found in this list." << endl;
|
||||
}
|
||||
}
|
||||
cout << "___________________END SEARCH SECTION _____" << endl;
|
||||
}
|
||||
|
||||
/*
|
||||
Display manager:
|
||||
- displays the number of national parks in this list
|
||||
- calls the displayList() function upon request
|
||||
Input Parameter: list
|
||||
*/
|
||||
void displayManager(const LinkedList &list) {
|
||||
string action;
|
||||
|
||||
cout << "Number of National Parks in this list: " << list.getLength() << endl;
|
||||
|
||||
cout << "\nDisplay list [Y/N]? ";
|
||||
cin >> action;
|
||||
if (action == "Y" || action == "y") {
|
||||
cout << endl;
|
||||
list.displayList();
|
||||
}
|
||||
}
|
8
04-dl-lists/.idea/.gitignore
generated
vendored
Normal file
8
04-dl-lists/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
1
04-dl-lists/.idea/.name
generated
Normal file
1
04-dl-lists/.idea/.name
generated
Normal file
@ -0,0 +1 @@
|
||||
04_dl_lists
|
2
04-dl-lists/.idea/04-dl-lists.iml
generated
Normal file
2
04-dl-lists/.idea/04-dl-lists.iml
generated
Normal file
@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
6
04-dl-lists/.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
6
04-dl-lists/.idea/inspectionProfiles/Project_Default.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||
</profile>
|
||||
</component>
|
4
04-dl-lists/.idea/misc.xml
generated
Normal file
4
04-dl-lists/.idea/misc.xml
generated
Normal file
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
</project>
|
8
04-dl-lists/.idea/modules.xml
generated
Normal file
8
04-dl-lists/.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/04-dl-lists.iml" filepath="$PROJECT_DIR$/.idea/04-dl-lists.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
6
04-dl-lists/.idea/vcs.xml
generated
Normal file
6
04-dl-lists/.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
7
04-dl-lists/CMakeLists.txt
Normal file
7
04-dl-lists/CMakeLists.txt
Normal file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
project(04_dl_lists)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(04_dl_lists main.cpp
|
||||
StudentList.cpp)
|
104
04-dl-lists/StudentList.cpp
Normal file
104
04-dl-lists/StudentList.cpp
Normal file
@ -0,0 +1,104 @@
|
||||
// Sorted Circular Doubly-Linked List with Sentinel Node
|
||||
// Implementation file for the Student List class
|
||||
// Written by: Iurii Tatishchev
|
||||
// Reviewed & Modified by: Iurii Tatishchev
|
||||
// IDE: CLion
|
||||
#include <iostream> // For cout and NULL
|
||||
#include "StudentList.h"
|
||||
using namespace std;
|
||||
|
||||
// **************************************************
|
||||
// Constructor
|
||||
// This function allocates and initializes a sentinel node
|
||||
// A sentinel (or dummy) node is an extra node added before the first data record.
|
||||
// This convention simplifies and accelerates some list-manipulation algorithms,
|
||||
// by making sure that all links can be safely dereferenced and that every list
|
||||
// (even one that contains no data elements) always has a "first" node.
|
||||
// **************************************************
|
||||
StudentList::StudentList()
|
||||
{
|
||||
head = new ListNode; // head points to the sentinel node
|
||||
|
||||
head->stu.gpa = -1;
|
||||
head->stu.name = "";
|
||||
head->forw = head;
|
||||
/* Write your code here */
|
||||
count = 0;
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// display list forwards: shows the value
|
||||
// stored in each node of the linked list
|
||||
// pointed to by head. from A to Z
|
||||
// **************************************************
|
||||
void StudentList::displayListForw() const
|
||||
{
|
||||
ListNode *pCur; // To move through the list
|
||||
|
||||
// Position pCur: skip the head of the list.
|
||||
pCur = head->forw;
|
||||
|
||||
// While pCur points to a node, traverse the list.
|
||||
while (pCur != head)
|
||||
{
|
||||
// Display the value in this node.
|
||||
cout << pCur->stu.gpa << " " << pCur->stu.name << endl;
|
||||
|
||||
// Move to the next node.
|
||||
pCur = pCur->forw;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
// **************************************************
|
||||
// display list forwards: shows the value
|
||||
// stored in each node of the linked list
|
||||
// pointed to by head, from Z to A
|
||||
// **************************************************
|
||||
|
||||
void StudentList::displayListBack() const
|
||||
{
|
||||
/* Write your code here */
|
||||
|
||||
cout << endl;
|
||||
}
|
||||
// **************************************************
|
||||
// The insertNode function inserts a node with
|
||||
// stu copied to its value member.
|
||||
// **************************************************
|
||||
void StudentList::insertNode(Student dataIn)
|
||||
{
|
||||
ListNode *newNode; // A new node
|
||||
ListNode *pCur; // To traverse the list
|
||||
|
||||
// Allocate a new node and store num there.
|
||||
newNode = new ListNode;
|
||||
newNode->stu = dataIn;
|
||||
|
||||
// Initialize pointers
|
||||
pCur = head->forw;
|
||||
|
||||
// Find location: skip all nodes whose name is less than dataIn's name
|
||||
while (pCur != head && pCur->stu.name < dataIn.name)
|
||||
{
|
||||
pCur = pCur->forw;
|
||||
}
|
||||
|
||||
// Insert the new node between pPre and pCur
|
||||
ListNode *pPre = pCur->back; // The previous node
|
||||
pPre->forw = newNode;
|
||||
newNode->forw = pCur;
|
||||
|
||||
/* Write your code here */
|
||||
|
||||
// Update the counter
|
||||
count++;
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// Destructor *
|
||||
// This function deletes every node in the list. *
|
||||
// **************************************************
|
||||
StudentList::~StudentList()
|
||||
{
|
||||
|
||||
}
|
45
04-dl-lists/StudentList.h
Normal file
45
04-dl-lists/StudentList.h
Normal file
@ -0,0 +1,45 @@
|
||||
// Sorted Circular Doubly-Linked List with Sentinel Node
|
||||
// Specification file for the Student List class
|
||||
// Written by: Iurii Tatishchev
|
||||
// Reviewed by: Iurii Tatishchev
|
||||
// IDE: CLion
|
||||
|
||||
#ifndef STUDENTLIST_H
|
||||
#define STUDENTLIST_H
|
||||
|
||||
#include <string>
|
||||
|
||||
struct Student
|
||||
{
|
||||
double gpa;
|
||||
std::string name;
|
||||
};
|
||||
|
||||
class StudentList
|
||||
{
|
||||
private:
|
||||
// Declare a structure for the list
|
||||
struct ListNode
|
||||
{
|
||||
Student stu; // The value in this node
|
||||
ListNode *forw; // To point to the next node
|
||||
ListNode *back; // To point to the previous node
|
||||
};
|
||||
|
||||
ListNode *head; // List head pointer
|
||||
int count; // To keep track of the number of nodes in the list
|
||||
|
||||
|
||||
public:
|
||||
StudentList(); // Constructor
|
||||
~StudentList(); // Destructor
|
||||
|
||||
// Linked list operations
|
||||
int getCount() const {return count;}
|
||||
void insertNode(Student);
|
||||
//bool deleteNode(double);
|
||||
//void searchList() const;
|
||||
void displayListForw() const;
|
||||
void displayListBack() const;
|
||||
};
|
||||
#endif
|
78
04-dl-lists/main.cpp
Normal file
78
04-dl-lists/main.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
CIS 22C
|
||||
|
||||
Sorted Circular Doubly-Linked List with Sentinel Node
|
||||
|
||||
This program:
|
||||
- Creates a sorted linked list (student name and gpa) . The list is sorted in ascending order by name.
|
||||
- Displays the list forwards(A to Z)
|
||||
- Displays the list backwards(Z to A)
|
||||
|
||||
Requirements: Finish writing the following three functions:
|
||||
- default constructor
|
||||
- insertNode()
|
||||
- displayListBack()
|
||||
|
||||
Written by: Iurii Tatishchev
|
||||
Reviewed by: Iurii Tatishchev
|
||||
IDE: CLion
|
||||
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "StudentList.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void buildList(StudentList &);
|
||||
|
||||
int main()
|
||||
{
|
||||
// Define a StudentList object
|
||||
StudentList list;
|
||||
|
||||
buildList(list); // insert data into the list
|
||||
cout << "There are " << list.getCount() << " elements in the list." << endl;
|
||||
|
||||
string answer;
|
||||
cout << "Insert [Y/N]? ";
|
||||
getline(cin, answer);
|
||||
if (answer == "Y" || answer == "y")
|
||||
{
|
||||
Student s;
|
||||
cout << "Enter gpa then enter name:" << endl;
|
||||
cin >> s.gpa >> s.name;
|
||||
list.insertNode(s);
|
||||
cout << "There are " << list.getCount() << " elements in the list." << endl;
|
||||
}
|
||||
cout << endl;
|
||||
list.displayListForw();
|
||||
/* Write your code here: display the list from Z to A */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* **************************************************
|
||||
This function builds a sorted linked list with data from the keyboard.
|
||||
The list is sorted in ascending order by the students' names.
|
||||
It calls the insertNode() function that inserts the new data at the right location in the linked list.
|
||||
An input line contains the gpa of a student follow by its name (assume it is a single word name)
|
||||
To stop reading enter "#"
|
||||
************************************************** */
|
||||
void buildList(StudentList &list)
|
||||
{
|
||||
string line;
|
||||
getline(cin, line);
|
||||
while (line != "#")
|
||||
{
|
||||
stringstream temp(line); // create a stringstream named temp with data from line
|
||||
Student newStu;
|
||||
temp >> newStu.gpa; // read gpa from temp
|
||||
temp >> newStu.name; // read name from temp
|
||||
list.insertNode(newStu);
|
||||
getline(cin, line);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user