6.19 Lab: Doubly-Linked List (Insert & Display)

This commit is contained in:
Iurii Tatishchev 2024-04-28 12:14:36 -07:00
parent a828b3525a
commit fa7aed64b2
Signed by: CaZzzer
GPG Key ID: 9A156B7DA6398968
3 changed files with 49 additions and 44 deletions

View File

@ -5,6 +5,7 @@
// IDE: CLion // IDE: CLion
#include <iostream> // For cout and NULL #include <iostream> // For cout and NULL
#include "StudentList.h" #include "StudentList.h"
using namespace std; using namespace std;
// ************************************************** // **************************************************
@ -15,14 +16,13 @@ using namespace std;
// by making sure that all links can be safely dereferenced and that every list // 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. // (even one that contains no data elements) always has a "first" node.
// ************************************************** // **************************************************
StudentList::StudentList() StudentList::StudentList() {
{
head = new ListNode; // head points to the sentinel node head = new ListNode; // head points to the sentinel node
head->stu.gpa = -1; head->stu.gpa = -1;
head->stu.name = ""; head->stu.name = "";
head->forw = head; head->forw = head;
/* Write your code here */ head->back = head;
count = 0; count = 0;
} }
@ -31,16 +31,14 @@ StudentList::StudentList()
// stored in each node of the linked list // stored in each node of the linked list
// pointed to by head. from A to Z // pointed to by head. from A to Z
// ************************************************** // **************************************************
void StudentList::displayListForw() const void StudentList::displayListForw() const {
{
ListNode *pCur; // To move through the list ListNode *pCur; // To move through the list
// Position pCur: skip the head of the list. // Position pCur: skip the head of the list.
pCur = head->forw; pCur = head->forw;
// While pCur points to a node, traverse the list. // While pCur points to a node, traverse the list.
while (pCur != head) while (pCur != head) {
{
// Display the value in this node. // Display the value in this node.
cout << pCur->stu.gpa << " " << pCur->stu.name << endl; cout << pCur->stu.gpa << " " << pCur->stu.name << endl;
@ -49,24 +47,35 @@ void StudentList::displayListForw() const
} }
cout << endl; cout << endl;
} }
// ************************************************** // **************************************************
// display list forwards: shows the value // display list backwards: shows the value
// stored in each node of the linked list // stored in each node of the linked list
// pointed to by head, from Z to A // pointed to by head, from Z to A
// ************************************************** // **************************************************
void StudentList::displayListBack() const {
ListNode *pCur; // To move through the list
void StudentList::displayListBack() const // Position pCur: skip the head of the list.
{ pCur = head->back;
/* Write your code here */
// 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->back;
}
cout << endl; cout << endl;
} }
// ************************************************** // **************************************************
// The insertNode function inserts a node with // The insertNode function inserts a node with
// stu copied to its value member. // stu copied to its value member.
// ************************************************** // **************************************************
void StudentList::insertNode(Student dataIn) void StudentList::insertNode(Student dataIn) {
{
ListNode *newNode; // A new node ListNode *newNode; // A new node
ListNode *pCur; // To traverse the list ListNode *pCur; // To traverse the list
@ -78,8 +87,7 @@ void StudentList::insertNode(Student dataIn)
pCur = head->forw; pCur = head->forw;
// Find location: skip all nodes whose name is less than dataIn's name // Find location: skip all nodes whose name is less than dataIn's name
while (pCur != head && pCur->stu.name < dataIn.name) while (pCur != head && pCur->stu.name < dataIn.name) {
{
pCur = pCur->forw; pCur = pCur->forw;
} }
@ -88,7 +96,8 @@ void StudentList::insertNode(Student dataIn)
pPre->forw = newNode; pPre->forw = newNode;
newNode->forw = pCur; newNode->forw = pCur;
/* Write your code here */ pCur->back = newNode;
newNode->back = pPre;
// Update the counter // Update the counter
count++; count++;
@ -98,7 +107,6 @@ void StudentList::insertNode(Student dataIn)
// Destructor * // Destructor *
// This function deletes every node in the list. * // This function deletes every node in the list. *
// ************************************************** // **************************************************
StudentList::~StudentList() StudentList::~StudentList() {
{
} }

View File

@ -9,18 +9,15 @@
#include <string> #include <string>
struct Student struct Student {
{
double gpa; double gpa;
std::string name; std::string name;
}; };
class StudentList class StudentList {
{
private: private:
// Declare a structure for the list // Declare a structure for the list
struct ListNode struct ListNode {
{
Student stu; // The value in this node Student stu; // The value in this node
ListNode *forw; // To point to the next node ListNode *forw; // To point to the next node
ListNode *back; // To point to the previous node ListNode *back; // To point to the previous node
@ -35,11 +32,15 @@ public:
~StudentList(); // Destructor ~StudentList(); // Destructor
// Linked list operations // Linked list operations
int getCount() const {return count;} int getCount() const { return count; }
void insertNode(Student); void insertNode(Student);
//bool deleteNode(double); //bool deleteNode(double);
//void searchList() const; //void searchList() const;
void displayListForw() const; void displayListForw() const;
void displayListBack() const; void displayListBack() const;
}; };
#endif #endif

View File

@ -27,8 +27,7 @@ using namespace std;
void buildList(StudentList &); void buildList(StudentList &);
int main() int main() {
{
// Define a StudentList object // Define a StudentList object
StudentList list; StudentList list;
@ -38,8 +37,7 @@ int main()
string answer; string answer;
cout << "Insert [Y/N]? "; cout << "Insert [Y/N]? ";
getline(cin, answer); getline(cin, answer);
if (answer == "Y" || answer == "y") if (answer == "Y" || answer == "y") {
{
Student s; Student s;
cout << "Enter gpa then enter name:" << endl; cout << "Enter gpa then enter name:" << endl;
cin >> s.gpa >> s.name; cin >> s.gpa >> s.name;
@ -48,7 +46,7 @@ int main()
} }
cout << endl; cout << endl;
list.displayListForw(); list.displayListForw();
/* Write your code here: display the list from Z to A */ list.displayListBack();
return 0; return 0;
} }
@ -59,12 +57,10 @@ It calls the insertNode() function that inserts the new data at the right locati
An input line contains the gpa of a student follow by its name (assume it is a single word name) An input line contains the gpa of a student follow by its name (assume it is a single word name)
To stop reading enter "#" To stop reading enter "#"
************************************************** */ ************************************************** */
void buildList(StudentList &list) void buildList(StudentList &list) {
{
string line; string line;
getline(cin, line); getline(cin, line);
while (line != "#") while (line != "#") {
{
stringstream temp(line); // create a stringstream named temp with data from line stringstream temp(line); // create a stringstream named temp with data from line
Student newStu; Student newStu;
temp >> newStu.gpa; // read gpa from temp temp >> newStu.gpa; // read gpa from temp