单向链表理解

这道链表的题很久之前就做了,上学期也做过类似的链表的题目,相比上学期的完全不懂,到上次做的懵懵懂懂,这几天忙着复习高数期中,但心中一直挂念着这道链表的题还没有搞懂,花了几天的时间,吃饭时候想,起床时候想,总算是把思路理清晰了,很感动!

1. 下面先贴上代码:
#include<iostream>
#include<sstream>
#include<cstring>
using namespace std;

typedef struct node{
  int data;
  struct node* next;
  node(int data = 0, struct node* next = NULL):data(data), next(next) {}
}node;

class list {
private:
  int size;
  node* head;
public:
  list();
  list(const list& other);
  ~list();
  list& operator=(const list& other);
  string toString();
  void clear();
  void insert(int position, const int& num);
  void erase(int position);
  list& sort();
  int getsize();
  bool empty();
};

list::list() {
  size = 0;
  head = NULL;
}
list& list::operator=(const list& other) {
  if (this != &other) {
    this->clear();
    if (other.head != NULL) {
      this->head = new node(other.head->data);
      node* p = other.head->next;
      node* q = this->head;
      while (p != NULL) {
        q->next = new node(p->data);
        p = p->next;
        q = q->next;
      }
      q->next = NULL;
    }
    this->size = other.size;
  }
  return *this;
}

list::list(const list& other) {
  size = 0;
  head = NULL;
  *this = other;
}
void list::clear() {
  if (this->head != NULL) {
    node* p = this->head;
    while (p != NULL) {
      node* t = p;
      p = p->next;
      delete t;
    }
  }
  this->size = 0;
}

list::~list() {
  this->clear();
}

void list::insert(int position, const int& num) {
  if (position > size||position < 0) {
    return;
  } else if (position == 0) {
    node* t = new node(num, this->head);
    this->head = t;
  } else {
    node* p = this->head;
    int count = 1;
    while (count != position) {
      p = p->next;
      count++;
    }
    node* t = new node(num, p->next);
    p->next = t;
  }
  this->size++;
}
void list::erase(int position) {
  if (position >= size||position < 0) {
    return;
  } else if (position == 0) {
    node* t = this->head;
    this->head = this->head->next;
    delete t;
  } else {
    node* p = this->head;
    int count = 0;
    while (count != position - 1) {
      p = p->next;
      count++;
    }
    node* t = p->next;
    p->next = t->next;
    delete t;
  }
  this->size--;
}
list& list::sort() {
  if (this->head != NULL&&this->head->next != NULL) {
    node* fast = this->head->next;
    node* slow = this->head;
    while (fast != NULL) {
      if (fast->data >= slow->data) {
        fast = fast->next;
        slow = slow->next;
      } else {
        node* pre = this->head;
        if (this->head->data > fast->data) {
          slow->next = fast->next;
          fast->next = this->head;
          this->head = fast;
        } else {
          while (pre->next->data <= fast->data) {
            pre = pre->next;
          }
          slow->next = fast->next;
          fast->next = pre->next;
          pre->next = fast;
        }
        fast = slow->next;
      }
    }
  }
  return *this;
}
string list::toString() {
  string result;
  node* p = this->head;
  while (p != NULL) {
    stringstream strCat;
    strCat << p->data << "->";
    result += strCat.str();
    p = p->next;
  }
  result += "NULL";
  return result;
}
int list::getsize() {
  return size;
}
bool list::empty() {
  return size == 0;
}

int main() {
  list li;

  int n;
  cin >> n;

  for (int i = 0, data, pos; i < n; i++) {
    cin >> pos >> data;
    li.insert(pos, data);
  }

  cout << li.toString() << " size: " << li.getsize() << endl;

  list li2(li);
  list li3;

  li = li3 = li2 = li;

  cout << li.toString() << " size: " << li.getsize() << endl;
  cout << li2.toString() << " size: " << li2.getsize() << endl;
  cout << li3.toString() << " size: " << li3.getsize() << endl;

  int m;
  cin >> m;

  for (int i = 0, pos; i < m; i++) {
    cin >> pos;
    li.erase(pos);
  }

  cout << li.toString() << endl;

  cout << li.sort().toString() << endl;
  cout << li2.sort().toString() << endl;
  cout << li3.sort().toString() << endl;

  return 0;
}

node(int data = 0, struct node* next = NULL):data(data), next(next) {}

2.struct 其实也是一种类,struct的成员默认为public, 而class的成员默认为private; 所以这行代码用了初始化列表的形式

list::list()

缺省构造函数,也称为默认构造函数

list& operator=(const list& other)

3.拷贝构造函数,在拷贝构造函数中用到了new,动态声明内存; 并且返回的是*this, 所以list l1, list l2, 这其中l1 = l2的话,首先l2已经运用insert构造出来,l2中已经有动态声明的内存,这时候l1 = l2,则通过调用拷贝构造函数, l1中也动态声明了内存,所以到最后的时候delete l1, l2,就是各自delete自己的内存, 主要注意在拷贝构造函数中其实是调用了赋值运算符重载函数,而不是我们简单理解的赋值,所以也就不会说最后delete的时候删掉的是同一块内存

node* t = new node(this->head)

创建一个新的node的同时让 t>next = this->head, 等价于

node* t = new node(this->head->data, this->head);

也等价于

node* t = new node(this->head->data);
t->next = this->head;

注意几个地方:

(1) this->clear();

(2) p->next = NULL;

4.insert函数中:

(1)首先是插入位置不当的情况,直接退出,不执行任何操作

position > size||position < 0; // unvalid position

(2)其次是position == 0的情况,直接将数插在链表头的位置

node* t = new node(num, this->head);
this->head = t;

创建 t 的同时让 t->next = this->head, 简化了代码

(3)最后比较复杂的是position合法而有随机的情况

node* p = this->head;
int count = 1;
while (p != NULL) {
   p  = p->next;
   count++;
}
node* t = new node(num, p->next);
p->next = t;

这里可以简单的看position == 1的情况,即创建了一个node,并且node的下一个指向了p->next,即为this->head, 从而实现了将num插在第一个位置的情况,这里的position可以为0,所以position == 1 实际上就是插在了第二个node上
主要要注意

int count = 1;
while (count != position)

5.erase函数:
(1)首先也是position 位置是否合法的判断

position >= size||position < 0; // unvalid position

(2)删除的是第一个节点,即为头节点
(3)删除的节点随机的情况

int count = 0;
while (count != position - 1) {
   p = p->next;
   count++;
}
node* t = p->next;
p->next = t->next;
delete t;

这里同样可以简单考虑position == 1的情况

6.sort函数,这个函数是最难理解的,来来回回看了好多遍都没有完全理解,尽早醒来的时候突然脑洞大开,下面是自己的理解,不知是否百分之百正确。

this->head == NULL||(this->head == NULL&&this->head->next == NULL)

在只有一个节点或者没有节点的情况下不需要进行排序

(1) 这种情况比较简单

fast->data > slow->data

直接

fast = fast->next;
slow = slow->next;

(2)else的话

if (this->head->data > fast->data) {
  slow->next = fast->next;
  fast->next = this->head;
  this->head = fast;
}

当这个时候fast->data 比前面的小,要将fast插到前面去
所以从this->head开始进行比较
如果满足this->head->data > fast->data
将fast插到this->head的前面
(3)不满足的话,将fast插到满足顺序的前面

pre = this->head;
while (pre != NULL) {
   pre = pre->next;
}
slow->next = fast->next;
fast->next = pre->next;
pre->next = fast;
//  将fast插到最后一个比它小的那一个的后面

注意后面要将fast重置为slow->next, 然后继续进行比较

toString函数,主要注意一下头文件

#include<cstring>
#include<sstream>

还有是p = p->next绝对不能粗心漏掉,会导致死循环

总结

难理解的题出现的频率最高,不管怎样,都要一步一个脚印,把应该掌握的,能够掌握的,都一点一点啃下来。
成功的路上注定是孤独的,既然选择了远方,便只顾风雨兼程,就算留给世界的只有背影,那也是我做的不后悔的选择。
接下来要认认真真的看书,而不是为了求速度看得一知半解。

做自己想做的事,人生不可复制,既然决定了,就努力去建造属于自己的不可复制的人生!

最重要的是,跟自己说:

“每个人都有自己的选择,不要用身边不努力的人的生活状态来麻痹自己”,最后只是自欺欺人!

最后一句话:

不管是为了自己的人生观,价值观,还是为了那些在意的人,没有理由不努力。没有必要去追求那些不适合自己的东西,做自己!只要不放弃,最后一定可以找到同路人!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值