initial commit
This commit is contained in:
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;
|
||||
}
|
||||
Reference in New Issue
Block a user