zoukankan      html  css  js  c++  java
  • 数据结构<一>单链表

    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    using namespace std;
    struct Node{
      int date;
      Node* next;
    };
    class List{
      private:
        Node *head;
      public:
        List() {
          head = new Node;
          head->date = 0;
          head->next = NULL; 
        }
        ~List() {
          delete head;
        }
        int Length();
        bool IsEmpty(); 
        void Insert(int x,int k); 
        void Delete(int k); 
        void ClearList(); 
        void Output();    
    };
    int List::Length() {
      Node *q = head->next;
      int k = 0;
      while (q) {
        k++;
        q = q->next;
      }
      return k;
    }
    bool List::IsEmpty(){
      return head->next == NULL;
    }
    void List::Insert(int x, int s) {
      if (x < 1) {
        cout<<"NO"<<endl;
        exit(1);
      }
      int k = 0;
      Node *q = head;
      while (q&& k < x-1) {
        k++;
        q = q->next;
      }
      if (!q) {
        cout<<"NO"<<endl;
        exit(1);
      }
      Node *p = new Node;
      p->date = s;
      p->next = q->next;
      q->next = p;
    }
    void List::Delete(int x) {
      if (x < 1) {
        cout<<"NO"<<endl;
        exit(1);
      }
      int k = 0;
      Node *q = head;
      while (q&& k < x-1) {
        k++;
        q = q->next;
      }
      if (q->next == NULL) {
        cout<<"NO"<<endl;
        exit(1);
      }
      Node *p = q->next;
      q->next = p->next; 
    } 
    void List::ClearList() {
      Node *q = head->next;
      while (q) {
        Node *p = q;
        q = q->next;
        delete p;
      }
      head->next = NULL;
    }
    void List::Output() {
      Node *q = head->next;
      while (q) {
        cout<<q->date<<" ";
        q = q->next;
      }
      cout<<endl;
    }
    int main() {
      List l;
      l.Insert(1,2);
      l.Insert(2,3);
      l.Insert(3,4);
      l.Insert(4,5);
      l.Output();
      l.Delete(2);
      l.Output();
      cout<<l.Length();
      return 0;
    } 
  • 相关阅读:
    Dll Hijacker
    PE文件格式学习之PE头移位
    远程线程注入shellcode笔记
    DLL注入之SHELLCODE数据转换
    vc libcurl 模拟上传文件
    Mysql uploader File
    vc 导出函数/调用
    windows 模拟用户会话创建进程
    综合一句话Shell破解
    义牛有灵舍命报恩 力拼强盗感人肺腑
  • 原文地址:https://www.cnblogs.com/a863886199/p/7553747.html
Copyright © 2011-2022 走看看