6.20 Lab: Doubly-Linked List (Delete)
This commit is contained in:
parent
fa7aed64b2
commit
a3cc673c86
@ -103,6 +103,33 @@ void StudentList::insertNode(Student dataIn) {
|
|||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// **************************************************
|
||||||
|
// The deleteNode function deletes a node with
|
||||||
|
// the same key as its paramenter, if found
|
||||||
|
// **************************************************
|
||||||
|
bool StudentList::deleteNode(string target) {
|
||||||
|
ListNode *pCur; // To traverse the list
|
||||||
|
bool success = false;
|
||||||
|
|
||||||
|
pCur = head->forw;
|
||||||
|
while (pCur != head && pCur->stu.name < target) {
|
||||||
|
pCur = pCur->forw;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pCur != head && pCur->stu.name == target) {
|
||||||
|
ListNode *pPrev = pCur->back;
|
||||||
|
ListNode *pNext = pCur->forw;
|
||||||
|
pPrev->forw = pNext;
|
||||||
|
pNext->back = pPrev;
|
||||||
|
delete pCur;
|
||||||
|
success = true;
|
||||||
|
count--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// **************************************************
|
// **************************************************
|
||||||
// Destructor *
|
// Destructor *
|
||||||
// This function deletes every node in the list. *
|
// This function deletes every node in the list. *
|
||||||
|
@ -7,8 +7,6 @@
|
|||||||
#ifndef STUDENTLIST_H
|
#ifndef STUDENTLIST_H
|
||||||
#define STUDENTLIST_H
|
#define STUDENTLIST_H
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
struct Student {
|
struct Student {
|
||||||
double gpa;
|
double gpa;
|
||||||
std::string name;
|
std::string name;
|
||||||
@ -36,7 +34,8 @@ public:
|
|||||||
|
|
||||||
void insertNode(Student);
|
void insertNode(Student);
|
||||||
|
|
||||||
//bool deleteNode(double);
|
bool deleteNode(std::string);
|
||||||
|
|
||||||
//void searchList() const;
|
//void searchList() const;
|
||||||
void displayListForw() const;
|
void displayListForw() const;
|
||||||
|
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
CIS 22C
|
CIS 22C: Sorted Circular Doubly-Linked List with Sentinel Node
|
||||||
|
|
||||||
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.
|
||||||
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 forwards(A to Z)
|
||||||
|
- Deletes nodes from the list
|
||||||
- Displays the list backwards(Z to A)
|
- Displays the list backwards(Z to A)
|
||||||
|
|
||||||
Requirements: Finish writing the following three functions:
|
Your task is to finish writing the following three functions:
|
||||||
- default constructor
|
- default constructor (reuse code from your previous Lab)
|
||||||
- insertNode()
|
- insertNode() (reuse code from your previous Lab)
|
||||||
- displayListBack()
|
- displayListBack() (reuse code from your previous Lab)
|
||||||
|
- deleteNode()
|
||||||
|
|
||||||
Written by: Iurii Tatishchev
|
Written by: Iurii Tatishchev
|
||||||
Reviewed by: Iurii Tatishchev
|
Reviewed by: Iurii Tatishchev
|
||||||
@ -33,15 +33,14 @@ int main() {
|
|||||||
|
|
||||||
buildList(list); // insert data into the list
|
buildList(list); // insert data into the list
|
||||||
cout << "There are " << list.getCount() << " elements in the list." << endl;
|
cout << "There are " << list.getCount() << " elements in the list." << endl;
|
||||||
|
|
||||||
string answer;
|
string answer;
|
||||||
cout << "Insert [Y/N]? ";
|
cout << "Delete [Y/N]? ";
|
||||||
getline(cin, answer);
|
getline(cin, answer);
|
||||||
if (answer == "Y" || answer == "y") {
|
if (answer == "Y" || answer == "y") {
|
||||||
Student s;
|
string targetName;
|
||||||
cout << "Enter gpa then enter name:" << endl;
|
cout << "Enter name to be removed from the list:" << endl;
|
||||||
cin >> s.gpa >> s.name;
|
getline(cin, targetName);
|
||||||
list.insertNode(s);
|
cout << targetName << (list.deleteNode(targetName) ? " - deleted!" : " - not found!") << endl;
|
||||||
cout << "There are " << list.getCount() << " elements in the list." << endl;
|
cout << "There are " << list.getCount() << " elements in the list." << endl;
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
@ -69,6 +68,4 @@ void buildList(StudentList &list) {
|
|||||||
getline(cin, line);
|
getline(cin, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user