计蒜客 数据结构 链表 ——应用筛选简历 C++

来源:互联网 发布:g3110扫描仪软件 编辑:程序博客网 时间:2024/06/15 04:50
//这个程序是,没有头节点的//2016/08/10#include<iostream>using namespace std;class Node {public:    int data;    Node* next;    Node(int _data) {        data = _data;        next = NULL;    }};class LinkList {private:    Node* head;public:    LinkList() {        head = NULL;    }    //插入函数    void insert(Node *node, int index) {        //插入的时候先检查是否是有这样的头节点,若是没有头结点的话建立一个头结点        if (head == NULL) {            head = node;           // head->next = head;            return;        }        //检查插入的位置,若是头部插入节点。若是插入的位置是0的话,头结点不是数据元素的结点(⊙o⊙)哦!!        if (index == 0) {        //node->next = head->next;           // head->next = node;           //还是使用循环链表,没有头节点           node->next = head;           head = node;           return;        }        //Node *current_node = head->next;        Node* current_node = head;        int count = 0;        //寻找插入位置        while (current_node->next != head && count < index - 1) {            current_node = current_node->next;            count++;        }        //讲节点插入,node指向插入节点        if (count == index - 1) {            node->next = current_node->next;            current_node->next = node;        }//        if (node == head->next) {//            head = node;//        }    }    //删除函数    void delete_node(int index){        //删除之前要检查头节点是否为空        if(head == NULL){            return;        }        Node* current_node = head;        int count = 0;        //检查删除位置,删除头节点的情况下,这个是没有头节点的情况        if(index == 0){            head = head->next;            delete current_node;            return;        }        //找到删除的前一个位置        while(current_node->next != NULL && count < index -1){            current_node = current_node->next;            count++;        }        //删除节点current_node指向删除节点的前一个位置,delete_node是指向删除节点        if(count == index - 1 && current_node->next != NULL){            Node* delete_node = current_node->next;            current_node->next = delete_node->next;            //用完的空间记得回收            delete delete_node;        }    }    //链表长度函数    int get_length(Node* head){        int count = 0;        Node* p = head->next;        while(p){            count = count +1;             p = p->next;        }        return count;    }    //输出函数    void output_node(){        int len = get_length(head);        int count = -1;        if(head == NULL){            return ;        }        Node* current_node = head;        while(current_node != NULL){            count = count + 1;            if(count == len/2){               //去掉结尾的换行符,就因为换行符就判断为错,计蒜客这点很不好。                cout<< current_node->data;            }            current_node = current_node->next;        }    }};int main() {    int n,m,num_id;    LinkList linklist;    cin >> n >> m ;    for(int i = 1; i <= n ;i++){        Node* node = new Node(i);        linklist.insert( node ,i - 1);    }    for(int j = 1 ; j <= m ;j++){        //这道题目是输入特殊不喜欢的数字就删除对应的序号的简历        //输入一个就删除一个        cin>>num_id;        linklist.delete_node(num_id - 1);    }    linklist.output_node();    return 0;}


0 0