运行结果:
Rolls 10
Jam 3
Tea 2
Enter you want to insert after the item: Rolls
Enter the insert item: Lisa
Enter the insert count: 11
Rolls 10
Lisa 11
Jam 3
Tea 2
Do you want to continue <y/n>?: y
Enter you want to insert after the item: Tea
Enter the insert item: Kevin
Enter the insert count: 13
Rolls 10
Lisa 11
Jam 3
Tea 2
Kevin 13
Do you want to continue <y/n>?: y
Enter you want to insert after the item: Jam
Enter the insert item: Sam
Enter the insert count: 3
Rolls 10
Lisa 11
Jam 3
Sam 3
Tea 2
Kevin 13
Do you want to continue <y/n>?: n
Enter you want to delete item name: Kevin
Rolls 10
Lisa 11
Jam 3
Sam 3
Tea 2
Do you want to continue <y/n>?: y
Enter you want to delete item name: Rolls
Lisa 11
Jam 3
Sam 3
Tea 2
Do you want to continue <y/n>?: y
Enter you want to delete item name: Sam
Lisa 11
Jam 3
Tea 2
Do you want to continue <y/n>?: n
linkList.cpp
#include <iostream>
#include <cstddef>
#include <string>
#include <cstdlib>
using namespace std;
struct Node {
string item;
int count;
Node* link;
};
typedef Node* NodePtr;
void apped(NodePtr& head, string item, int count);
void show(NodePtr& head);
void insertAfter(NodePtr pos, string item, int count);
void remove(NodePtr pos);
NodePtr searchFront(NodePtr head, string afterMe);
int main() {
using std::cout;
using std::cin;
NodePtr head = nullptr;
string s, str, name;
int cn;
apped(head, "Tea", 2);
apped(head, "Jam", 3);
apped(head, "Rolls", 10);
show(head);
char c;
do {
cout << "Enter you want to insert after the item: ";
cin >> s;
NodePtr pos = searchFront(head, s);
if(pos != nullptr) {
cout << "Enter the insert item: ";
cin >> str;
cout << "Enter the insert count: ";
cin >> cn;
if (head == pos)
insertAfter(pos, str, cn);
else
insertAfter(pos->link, str, cn);
show(head);
}
else {
cout << "Not found on the list.\n";
exit(EXIT_FAILURE);
}
cout << "Do you want to continue <y/n>?: ";
cin >> c;
} while (c == 'y' || c == 'Y');
do {
cout << "Enter you want to delete item name: ";
cin >> name;
NodePtr pos = searchFront(head, name);
if(pos != nullptr) {
if (head == pos) {
head = head->link;
pos->link = nullptr;
delete pos;
}
else
remove(pos);
show(head);
}
else
cout << "Not found on the list.\n";
cout << "Do you want to continue <y/n>?: ";
cin >> c;
} while (c == 'y' || c == 'Y');
return 0;
}
NodePtr searchFront(NodePtr head, string afterMe) {
using std::cout;
NodePtr tempPtr = head;
NodePtr FrontPtr = head;
if (head == nullptr)
cout << "The list link is empty.";
else {
while (tempPtr != nullptr) {
if (tempPtr->item == afterMe)
return FrontPtr;
FrontPtr = tempPtr;
tempPtr = tempPtr->link;
}
}
return nullptr;
}
void apped(NodePtr& head, string item, int count) {
NodePtr afterPtr = new Node;
afterPtr->item = item;
afterPtr->count = count;
afterPtr->link = head;
head = afterPtr;
}
void show(NodePtr& head) {
using std::cout;
NodePtr tempPtr = head;
while (tempPtr != nullptr) {
cout << tempPtr->item << " " << tempPtr->count << '\n';
tempPtr = tempPtr->link;
}
}
void insertAfter(NodePtr pos, string item, int count) {
using std::cout;
NodePtr tempPtr = new Node;
tempPtr->item = item;
tempPtr->count = count;
tempPtr->link = pos->link;
pos->link = tempPtr;
}
void remove(NodePtr pos) {
NodePtr pNext = pos;
pos->link = pos->link->link;
delete pNext->link;
}