单链表的基本操作

作者: sugar_coated | 来源:发表于2016-06-11 15:18 被阅读487次

    按C语言代码编写

    • 节点
    typedef struct Student {
        char name[20];       //学生姓名
        int num;             //学号
        struct Student *next;
    }node;       //node为struct Student的别名
    
    • 链表的创建
    node *creat() {
        node *head = NULL, *tail = NULL;
        node *pNode = malloc(sizeof(node));
        scanf("%s%d", pNode->name, &pNode->num);
        while(pNode->num) {      //这里以学号为零结束创建
            if(!head) head = tail = pNode;
            tail->next = pNode;
            tail = pNode;
            pNode = malloc(sizeof(node));
            scanf("%s%d", pNode->name, &pNode->num);
        }
        if(head) tail->next = NULL;//注意,条件不可以省
        return head;
    }
    
    • 输出
    void output(node *head) {
        if(!head) printf("This list is empty!\n");
        node *pNode;
        for(pNode = head; pNode; pNode = pNode->next)
            printf("%s\t%d\n", pNode->name, pNode->num);
    }
    
    • 查找
    //通过姓名查找某位同学的具体信息
    node *find(node *head, const char *name) {
        node *pNode = NULL;
        for(pNode = head; pNode; pNode = pNode->next)
            if(!strcmp(pNode->name, name))
                return pNode;   
        return pNode;
    }
    
    • 修改
    //修改某位学生信息
    void revise(node *head, const char *name) {
        node *pNode = find(head, name);
        if(!pNode)
            printf("No such person!\n");
        else
            scanf("%s%d", pNode->name, &pNode->num);
    }
    
    • 删除
    //按姓名删除
    void Delete(node *head, const char *name) {
        if(!head || !head->next) return ;
        node *pNode = head;
        while (pNode->next) {
            if (!strcmp(pNode->next->name, name)) {
                node *dNode = pNode->next;
                pNode->next = dNode->next;
                free(dNode);
                break;
            }
            if(pNode->next)
                pNode = pNode->next;
        }
    }
    
    • 排序
    //选择排序(按num从小到大排)
    void mySort(node *head) {
        if(!head || !head->next) return ;
        node *pi, *pj, *tnode;
        node tNode;
        for (pi = head; pi->next; pi = pi->next) {
            for (pj = pi->next; pj; pj = pj->next) {
                if (pi->num > pj->num) {
                    tNode = *pi;
                    *pi = *pj;
                    *pj = tNode;
                    tnode = pi->next;
                    pi->next = pj->next;
                    pj->next = tnode;
                }       
            }
        }
    }
    
    • 测试代码
    void main() {
        node *head = NULL;
    //创建函数和输出
        head = creat();
        output(head);
    // 查找
        char name[20];
        scanf("%s", name);
        node *pNode = find(head, name);
        if(pNode)
            printf("%s\t%d\n", pNode->name, pNode->num);
        else
           printf("No such person!\n");
    //修改
        scanf("%s", name);
        revise(head, name);
        output(head);
    //删除
        scanf("%s", name);
        Delete(head, name);
        output(head);
    //排序
        mySort(head);
        output(head);
        return 0;   
    }
    

    C++代码

    #include <iostream>
    #include <string>
    using namespace std;
    struct node {
        string name;
        int num;
        node *next;
    };
    //单链表的创建
    node *creat() {
        node *head;
        node *pNode = new node;
        head = pNode;
        return head;
    }
    //头插法
    void push_front(node *head, string str, int val) {
        if(!head) return ;
        node *pNode = new node;
        pNode->name = str;
        pNode -> num = val;
        pNode->next = head->next;
        head->next = pNode;
    }
    //尾插法
    void push_back(node *head,string str, int val) {
        if(!head) return ;
        node *tail;
        for (tail = head; tail->next; tail = tail->next);
        node *pNode = new node;
        pNode->name = str;
        pNode->num = val;
        tail->next = pNode;
        pNode->next = nullptr;
    }
    //删除(此处按姓名删除)
    void remove(node *head, string str) {
        if(!head || !head->next) return ;
        node *pNode = head;
        while (pNode->next) {
            if (pNode->next->name == str) {
                node *flag = pNode->next;
                pNode->next = pNode->next->next;
                delete flag;
            }
            if(pNode->next)
                pNode = pNode->next;
        }
    }
    //计算链表的节点
    int n_node(node *head) {
        int count = 0;
        for (node *pNode = head; pNode; pNode = pNode->next)
            ++count;
        return count;
    }
    //选择排序(按num从小到大排)
    void mySort(node *head) {
        if(!head || !head->next) return ;
        for (node *pi = head; pi->next; pi = pi->next) {
            for (node *pj = pi->next; pj; pj = pj->next) {
                if (pi->num > pj->num) {
                    node tNode = *pi;
                    *pi = *pj;
                    *pj = tNode;
                    node *tnode = pi->next;
                    pi->next = pj->next;
                    pj->next = tnode;
                }       
            }
        }
    }
    //在有序的链表中插入
    void push(node *head, string str, int val) {
        if(!head) return ;
        push_front(head, str, val);
        mySort(head);
    }
    //链表的输出
    void output(node *head) {
        for (node *pNode = head; pNode; pNode = pNode->next)
            cout << pNode->name << ' ' << pNode->num << endl;
    }
    int main() {
        node *head = nullptr;
        head = creat();
        cin >> head->name;
        cin >> head->num;
        head->next = nullptr;
        string str;
        int val;
        for (int i = 0; i < 3; ++i) {
            cin >> str;
            cin >> val;
            push_front(head, str, val);
        }
        cout << "push_front:" << endl;
        output(head);
        for (int i = 0; i < 3; ++i) {
            cin >> str;
            cin >> val;
            push_back(head, str, val);
        }
        cout << "push_back:" << endl;
        output(head);
        cin >> str;
        remove(head, str);
        cout << "remove:" << endl;
        output(head);
        mySort(head);
        cout << "sort:" << endl;
        output(head);
        for (int i = 0; i < 3; ++i) {
            cin >> str;
            cin >> val;
            push(head, str, val);
        }
        cout << "push:" << endl;
        output(head);
        cout << "节点:" << n_node(head) << endl;
        return 0;
    }
    

    第一次写博客,有什么不好的地方,欢迎拍砖。

    相关文章

      网友评论

        本文标题:单链表的基本操作

        本文链接:https://www.haomeiwen.com/subject/vjvsdttx.html