C++实现线性链表的基本操作(WAP万革始)

题目:实现一个线性链表,完成如下操作:


  • push_back:在链表尾部插入一个节点
  • push_front:在链表开头插入一个节点
  • pop_back:从链表尾部弹出一个节点
  • pop_front:从链表头部弹出一个节点
  • reverse:反转链表
  • max:输出链表的最大值

输入:第一行输入n,表示接下来有n行命令;剩下n行输入每行的命令
输入示例:
5
pop_back
push_back 2
push_front 1
reverse
pop_front
输出:
Error
2 1
2 (表示把2 pop出来)
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;

// Get the second parameter if the command is push_back or push_front
int getSecondParam(const string& str){
    istringstream iss(str);  // To split the str by ' '
    string s;
    string val;
    int flag = 1;
    while (iss >> s)
    {
        if (flag == 2)  // Get the second parameter
        {
            val = s;
            break;
        }
        flag++;
    }
    unsigned int j;
    int data = 0;
    for (j = 0; j < val.size(); j++){
        int num = 1;
        for (unsigned int k = 1; k < val.size()-j; k++)
            num *= 10;
        data += (val[j]-48) * num;
    }
    return data;
}

// create a linear list class
class linear_List{
public:
    linear_List() {create_List(); }  // constractor
    ~linear_List() {clear_List(); }  // destractor
    void create_List();
    void push_front(const int& d);
    void push_back(const int& d);
    void pop_front();
    void pop_back();
    void reverse();
    int max();
private:
    struct ListNode{
        int data;
        ListNode* next;
        ListNode(const int& d) : data(d), next(NULL) {}
    };
    ListNode* head;
    void clear_List(){
        ListNode* p = head;
        while (p){
            ListNode* q = p->next;
            delete p;
            p = q;
        }
    }
};

// Create a List
void linear_List::create_List(){
    head = NULL;
}

// insert node in the front of the list
void linear_List::push_front(const int& d){
    ListNode* p = new ListNode(d);
    p->next = head;
    head = p;
}

// insert node at the end of the list
void linear_List::push_back(const int& d){
    if (head == NULL)
    {
        head = new ListNode(d);
        return ;
    }
    ListNode* p = new ListNode(d);
    ListNode* q = head;
    while(q->next)
        q = q->next;
    q->next = p;
}

// delete the head node from the list
void linear_List::pop_front(){
    if (head == NULL){
        cout << "Error" << endl;
        return ;
    }
    ListNode* p = head;
    cout << head->data << endl;
    head = head->next;
    delete p;
    p = NULL;
}

// delete the tail node from the list
void linear_List::pop_back(){
    if (head == NULL){
        cout << "Error" << endl;
        return ;
    }
    ListNode* p = head;
    if (p->next == NULL){
        cout << head->data << endl;
        head = NULL;
        delete p;
        p = NULL;
        return ;
    }
    while (p->next->next)
        p = p->next;
    ListNode* q = p;
    p = p->next;
    q->next = NULL;
    cout << p->data << endl;
    delete p;
    p = NULL;
    delete q;
    q = NULL;
}

// Reverst the linear list and print it
void linear_List::reverse(){
    if(head == NULL || head->next == NULL)
        return ;
    ListNode* p = head;
    ListNode* q = head->next;
    ListNode* r;
    head->next = NULL;
    while(q){
        r = q->next;
        q->next = p;
        p = q;
        q = r;
    }
    head = p;
    while (p)
    {
        cout << p->data << " ";
        p = p->next;
    }
    cout << endl;
    delete p;
    p = NULL;
    delete q;
    q = NULL;
    delete r;
    r = NULL;
}

// Get the max value in the list
int linear_List::max(){
    if (head == NULL){
        return 0;
    }
    int max_val = head->data;
    ListNode* p = head->next;
    while(p){
        if (p->data > max_val)
            max_val = p->data;
        p = p->next;
    }
    return max_val;
}

int main(){
    int n;
    cin >> n;
    while (cin.get() != '\n')
        continue;
    vector<string> str(n);
    for (int i = 0; i < n; i++){
        getline(cin, str[i]);
    }
    linear_List iList;
    for (int i = 0; i < n; i++){
        if (str[i] == "pop_front")
            iList.pop_front();
        else if (str[i] == "pop_back")
            iList.pop_back();
        else if (str[i] == "reverse")
            iList.reverse();
        else if (str[i] == "max")
            cout << iList.max() << endl;
        else if (str[i].find("push_front") != string::npos){

            iList.push_front(getSecondParam(str[i]));
        }
        else if (str[i].find("push_back") != string::npos){
            iList.push_back(getSecondParam(str[i]));
        }
        else
            cout << "No such function." << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值