zoukankan      html  css  js  c++  java
  • 手写单链表(基础)

    手写了一个单链表,实现了一些简单的功能。

      1 #pragma once
      2 
      3 using namespace std;
      4 
      5 class Node
      6 {
      7 public:
      8     Node* next;
      9     int data;
     10 };
     11 
     12 class List
     13 {
     14 private:
     15     Node* head;
     16     int length;
     17 
     18 public:
     19     List();
     20     int size();
     21     bool empty();
     22     void push_front(int num);
     23     void push_back(int num);
     24     bool insert(int pos, int num);
     25     bool erase(int pos);
     26     bool find(int num);
     27     void clear();
     28     void show();
     29 };
     30 
     31 List::List()
     32 {
     33     head = new Node;
     34     head->next = nullptr;
     35     head->data = 0;
     36     length = 0;
     37 }
     38 
     39 int List::size()
     40 {
     41     return length;
     42 }
     43 
     44 bool List::empty()
     45 {
     46     if (length == 0) return true;
     47     else return false;
     48 }
     49 
     50 void List::push_front(int num)
     51 {
     52     Node* first = head->next;
     53     Node* pushNode = new Node;
     54     pushNode->data = num;
     55     pushNode->next = first;
     56     head->next = pushNode;
     57     length++;
     58 }
     59 
     60 void List::push_back(int num)
     61 {
     62     Node* temp = head;
     63     while (temp->next != nullptr)
     64     {
     65         temp = temp->next;
     66     }
     67     Node* pushNode = new Node;
     68     pushNode->data = num;
     69     pushNode->next = nullptr;
     70     temp->next = pushNode;
     71     length++;
     72 }
     73 
     74 bool List::insert(int pos, int num)
     75 {
     76     Node* temp = head;
     77     int i = 0;
     78     if (pos < 0 || pos > length) return false;
     79     while (i < pos && temp->next != nullptr)
     80     {
     81         i++;
     82         temp = temp->next;
     83     }
     84     Node* pushNode = new Node;
     85     pushNode->data = num;
     86     pushNode->next = temp->next;
     87     temp->next = pushNode;
     88     length++;
     89     return true;
     90 }
     91 
     92 bool List::erase(int pos)
     93 {
     94     Node* temp = head;
     95     int i = 0;
     96     if (pos <= 0 || pos > length || length == 0) return false;
     97     while (i < pos - 1 && temp->next != nullptr)
     98     {
     99         i++;
    100         temp = temp->next;
    101     }
    102     Node* q = temp->next;
    103     temp->next = q->next;
    104     free(q);
    105     length--;
    106     return true;
    107 }
    108 
    109 bool List::find(int num)
    110 {
    111     Node* temp = head->next;
    112     int i = 1;
    113     while (temp != nullptr && temp->data != num)
    114     {
    115         temp = temp->next;
    116         i++;
    117     }
    118     if (temp == nullptr)
    119         return -1;
    120     else 
    121         return i;
    122 }
    123 
    124 void List::clear()
    125 {
    126     Node* nextNode = head->next;
    127     while (nextNode != nullptr)
    128     {
    129         Node* temp = nextNode->next;
    130         delete nextNode;
    131         nextNode = temp;
    132     }
    133     length = 0;
    134     head->next = nullptr;
    135 }
    136 
    137 void List::show()
    138 {
    139     if (length == 0) return;
    140     else
    141     {
    142         Node* temp = head->next;
    143         while (temp->next != nullptr)
    144         {
    145             cout << temp->data << " ";
    146             temp = temp->next;
    147         }
    148         cout << temp->data << endl;
    149     }
    150 }
  • 相关阅读:
    html更改弹窗样式(原创,转载需声明)
    关于考研的反思
    Android之控件学习
    Android之LinearLayout布局下怎么让按钮固定在底部
    Android中控件属性详细总结(转载)
    毕业设计周记(第四篇)
    毕业设计周记(第三篇)
    毕业设计周记(第二篇)
    毕业设计周记(第一篇)
    Hadoop
  • 原文地址:https://www.cnblogs.com/zny0222/p/13672024.html
Copyright © 2011-2022 走看看