• 动态链表学习——实践


        学习有些紧张(我的天,一开学就那么累),都没有时间写博客了,这两天学习了动态链表的知识,于是利用这个知识写了一个表(可以统计学生信息——如学号,姓名,成绩),没有时间说自己的思路了,只能直接放上代码作为学习记录(以后有时间会写更好的,并且把思路详细写出)。我这个程序比较低级,大佬勿喷。
    效果图:


    代码如下(编译环境:VC++):

    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    struct node {
        int number;
        string name;
        double score;
        node * next;
    };
    class list {
    private:
        node *head;
        int count;
        int selMode(); //选择模式
        void display();//显示
        void insert(); //插入操作
        void remove(); //删除操作
        void clear();  //释放所有动态数据
    
    public:
        list() {        //构造函数
            head = NULL;
            count = 0;
        } 
        ~list() {       //解析函数
            clear();
        }
        void play() {   //执行函数
            while (1) {
                int i = selMode();
                if (i == 1)
                    display();
                else if (i == 2)
                    insert();
                else
                    remove();
            }
        }
    };
    int list::selMode() {
        system("cls");
        cout << "1: 显示" << endl;
        cout << "2: 插入" << endl;
        cout << "3: 删除" << endl;
        cout << "4: 退出" << endl;;
        while (1) {
            int k;
            cin >> k;
            if (k == 4) {
                exit(1);
            }
            else if (k < 1 || k > 4) {
                cout << "非法输入,请重新输入!" << endl;
            }
            else
                return k;
        }
    }
    void list::display() {
        system("cls");       //清屏
        cout << "总学生数 :" << count << endl;
        cout << "学号   " << "姓名    " << "分数" << endl;
        //输出具体数据
        if (head == NULL)
            cout << "无数据" << endl;
        else {
            node *k = head; 
            while (k!= NULL) { 
                cout << k->number << "   " << k->name << "   " << k->score << endl;
                k = k->next;
            }
        }
        cout << "返回请输入一个键:";
        int k;
        cin >> k;
    }
    void list::insert() {
        system("cls");
        while (1) {
            node *p = new node;
            cout << "请输入该学生学号:";
            cin >> p->number;
            if (p->number <= 0)       //输入值小于等于0代表返回操作
                break;
    //具体插入操作(需要分类讨论)
            //链表为空
            if (head == NULL) {       
                p->next = NULL;
                head = p;
            }
            //链表不为空
            else if (head->number >= p->number) { //第一个节点的学号大于等于插入节点的学号
                if (head->number > p->number) {//大于
                    p->next = head;
                    head = p;
                }
                else {//等于
                    cout << "重复学号,请重新输入" << endl;
                    continue;
                }
            }
            else {                              //第一个节点的学号小于插入节点的学号
                node * a = head;
                node * b = a->next;
                while (b != NULL && b->number < p->number) {//找出特殊的节点
                    a = b;
                    b = b->next;
                }
                if (b == NULL || b->number > p->number) {
                    a->next = p;
                    p->next = b;
                }
                else {
                    cout << "重复学号,请重新输入" << endl;
                    continue;
                }
            }
            cout << "请输入该学生的姓名:";
            cin >> p->name;
            cout << "请输入该学生的分数:";
            cin >> p->score;
            cout << endl;
            count++;
        }
    }
    void list::remove() {
        system("cls");
        while (1) {
            cout << "请输入所要删除学生的学号:";
            int pos; //要删除的学生学号;
            cin >> pos;
            if (pos <= 0)      //小于等于0代表返回操作
                break;
            if (head == NULL) {
                cout << "列表为空,请输入任意键返回";
                int k;
                cin >> k;
                break;
            }
            else {
                if (head->number == pos) {
                    node *p = head;
                    head = p->next;
                    delete p;
                    count--;
                }
                else {
                    node *k = head;
                    while (k->next != NULL && (k->next)->number != pos) {
                        k = k->next;
                    }
                    if (k->next == NULL)
                        cout << "该学号不在此列表中,请重新输入" << endl;
                    else {
                        node *p = k->next;
                        k->next = p->next;
                        delete p;
                        count--;
                    }
                }
            }
        }
    }
    void list::clear() {
        if (head != NULL) {
            node *p = head;
            while (p != NULL) {
                delete p;
                p = p->next;
            }
        }
    }
    int main() {
        list stu;
        stu.play();
        return 0;
    }
  • 相关阅读:
    poj 3348 Cows 凸包 求多边形面积 计算几何 难度:0 Source:CCC207
    uva 12356 Army Buddies 树状数组解法 树状数组求加和恰为k的最小项号 难度:1
    cf 290F. Treeland Tour 最长上升子序列 + 树的回溯 难度:1
    poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0
    poj 1379 Run Away 模拟退火 难度:1
    模拟退火算法
    快速切题 sgu 112. a^b-b^a 大数 次方 难度:0 非java:1
    快速切题 poj 1003 hangover 数学观察 难度:0
    #Leetcode# 404. Sum of Left Leaves
    PAT 甲级 1012 The Best Rank
  • 原文地址:https://www.cnblogs.com/Serenaxy/p/10471872.html
Copyright © 2020-2023  润新知