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
04-dl-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
04-dl-lists/.idea/.name generated Normal file
View File

@@ -0,0 +1 @@
04_dl_lists

2
04-dl-lists/.idea/04-dl-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
04-dl-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
04-dl-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/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
View 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>

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