6.19 Lab: Doubly-Linked List (Insert & Display)
This commit is contained in:
parent
a828b3525a
commit
fa7aed64b2
@ -5,6 +5,7 @@
|
||||
// IDE: CLion
|
||||
#include <iostream> // For cout and NULL
|
||||
#include "StudentList.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
// **************************************************
|
||||
@ -15,14 +16,13 @@ using namespace std;
|
||||
// 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()
|
||||
{
|
||||
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 */
|
||||
head->back = head;
|
||||
count = 0;
|
||||
}
|
||||
|
||||
@ -31,16 +31,14 @@ StudentList::StudentList()
|
||||
// stored in each node of the linked list
|
||||
// pointed to by head. from A to Z
|
||||
// **************************************************
|
||||
void StudentList::displayListForw() const
|
||||
{
|
||||
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)
|
||||
{
|
||||
while (pCur != head) {
|
||||
// Display the value in this node.
|
||||
cout << pCur->stu.gpa << " " << pCur->stu.name << endl;
|
||||
|
||||
@ -49,24 +47,35 @@ void StudentList::displayListForw() const
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// display list forwards: shows the value
|
||||
// display list backwards: shows the value
|
||||
// stored in each node of the linked list
|
||||
// pointed to by head, from Z to A
|
||||
// **************************************************
|
||||
void StudentList::displayListBack() const {
|
||||
ListNode *pCur; // To move through the list
|
||||
|
||||
void StudentList::displayListBack() const
|
||||
{
|
||||
/* Write your code here */
|
||||
// Position pCur: skip the head of the list.
|
||||
pCur = head->back;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// **************************************************
|
||||
// The insertNode function inserts a node with
|
||||
// stu copied to its value member.
|
||||
// **************************************************
|
||||
void StudentList::insertNode(Student dataIn)
|
||||
{
|
||||
void StudentList::insertNode(Student dataIn) {
|
||||
ListNode *newNode; // A new node
|
||||
ListNode *pCur; // To traverse the list
|
||||
|
||||
@ -78,8 +87,7 @@ void StudentList::insertNode(Student dataIn)
|
||||
pCur = head->forw;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
@ -88,7 +96,8 @@ void StudentList::insertNode(Student dataIn)
|
||||
pPre->forw = newNode;
|
||||
newNode->forw = pCur;
|
||||
|
||||
/* Write your code here */
|
||||
pCur->back = newNode;
|
||||
newNode->back = pPre;
|
||||
|
||||
// Update the counter
|
||||
count++;
|
||||
@ -98,7 +107,6 @@ void StudentList::insertNode(Student dataIn)
|
||||
// Destructor *
|
||||
// This function deletes every node in the list. *
|
||||
// **************************************************
|
||||
StudentList::~StudentList()
|
||||
{
|
||||
StudentList::~StudentList() {
|
||||
|
||||
}
|
||||
|
@ -9,18 +9,15 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
struct Student
|
||||
{
|
||||
struct Student {
|
||||
double gpa;
|
||||
std::string name;
|
||||
};
|
||||
|
||||
class StudentList
|
||||
{
|
||||
class StudentList {
|
||||
private:
|
||||
// Declare a structure for the list
|
||||
struct ListNode
|
||||
{
|
||||
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
|
||||
@ -32,14 +29,18 @@ private:
|
||||
|
||||
public:
|
||||
StudentList(); // Constructor
|
||||
~StudentList(); // Destructor
|
||||
~StudentList(); // Destructor
|
||||
|
||||
// Linked list operations
|
||||
int getCount() const {return count;}
|
||||
int getCount() const { return count; }
|
||||
|
||||
void insertNode(Student);
|
||||
|
||||
//bool deleteNode(double);
|
||||
//void searchList() const;
|
||||
void displayListForw() const;
|
||||
|
||||
void displayListBack() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -27,8 +27,7 @@ using namespace std;
|
||||
|
||||
void buildList(StudentList &);
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
// Define a StudentList object
|
||||
StudentList list;
|
||||
|
||||
@ -38,8 +37,7 @@ int main()
|
||||
string answer;
|
||||
cout << "Insert [Y/N]? ";
|
||||
getline(cin, answer);
|
||||
if (answer == "Y" || answer == "y")
|
||||
{
|
||||
if (answer == "Y" || answer == "y") {
|
||||
Student s;
|
||||
cout << "Enter gpa then enter name:" << endl;
|
||||
cin >> s.gpa >> s.name;
|
||||
@ -48,7 +46,7 @@ int main()
|
||||
}
|
||||
cout << endl;
|
||||
list.displayListForw();
|
||||
/* Write your code here: display the list from Z to A */
|
||||
list.displayListBack();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -59,19 +57,17 @@ 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)
|
||||
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);
|
||||
}
|
||||
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