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

//这个程序是,没有头节点的
//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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值