C++ <类C:单链表的插入、查找、删除>

运行结果:

 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值