initial commit

This commit is contained in:
2024-04-28 11:54:46 -07:00
commit a828b3525a
63 changed files with 2291 additions and 0 deletions

8
03-lists/.idea/.gitignore generated vendored Normal file
View 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
View File

@@ -0,0 +1 @@
03_lists

2
03-lists/.idea/03_lists.iml generated Normal file
View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

View 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
View 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
View 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
View 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);
}
}

View 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
View 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)

Binary file not shown.

157
03-lists/LinkedList.cpp Normal file
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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
View 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();
}
}