zoukankan      html  css  js  c++  java
  • 【模板】双向链表

    #include <bitsstdc++.h> 
    using namespace std;
    typedef long long ll;
    
     
    struct Node{
        int key;
        Node *next,*prev;
    };
    
    Node *nil;
    
    void init(){
        nil = (Node *) malloc(sizeof(Node));
        nil->next = nil;
        nil->prev = nil;
    }
    
    void insert(int key){
        Node *x = (Node *) malloc(sizeof(Node));
        x->key = key;
        //在头结点后添加元素 
        x->next = nil->next;
        nil->next->prev = x;
        nil->next = x;
        x->prev = nil;
    }
    
    Node *listSearch(int key){
        Node *cur = nil->next;  // 从头结点后面的元素开始访问 
        while(cur != nil && cur->key != key){
            cur = cur->next;
        }
        return cur;
    }
    
    void deleteNode(Node *t){
        if(t == nil) return;  // t为头结点时不作处理
        t->prev->next = t->next;
        t->next->prev = t->prev; 
        free(t);
    }
    
    void deleteFirst(){
        deleteNode(nil->next);
    }
    
    void deleteLast(){
        deleteNode(nil->prev);
    }
    
    void deleteKey(int key){
        //删除搜索到的结点
        deleteNode(listSearch(key)); 
    }
    
    void printList(){
        Node *cur = nil->next;
        int isf = 0;
        while(true){
            if(cur == nil) break;
            if(isf++ > 0) printf(" ");
            printf("%d",cur->key);
            cur = cur->next;
        }
        printf("
    ");
    }
    
    int main() {
    
    
      return 0;
    }
    //  writen by zhangjiuding 
  • 相关阅读:
    golang gc
    set password to qcow2
    golang reflect struct
    Mac 自启动管理
    shell exec
    shell 管道 与 mkfifo
    shell 读取文件
    shell 函数
    shell read 命令
    ubuntu 快速安装和设置 mysql
  • 原文地址:https://www.cnblogs.com/zhangjiuding/p/7637961.html
Copyright © 2011-2022 走看看