单向链表

tags:C++

单向链表

本人学院: http://sdcs.sysu.edu.cn/

by –catherine

  • 单向链表
        • 本人学院 httpsdcssysueducn
      • 单向链表的简单操作c
          • 注排序是最难理解的也是最难想的

单向链表的简单操作(c++)

注:排序是最难理解的,也是最难想的
#include<iostream>
#include<string>
#include<sstream>
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();
  list(const list& other);
  void clear();
  string toString();
  list& operator=(const list& other);
  void insert(int position, const int& n);
  void erase(int position);
  int getsize(void);
  bool isempty() const;
  list& sort();
};
//缺省构造函数
list::list() {
  size = 0;
  head = NULL;
}
//拷贝构造函数
list::list(const list& other) {
  head = NULL;
  size = 0;
  *(this) = other;
}
//将输出内容转换为字符串
string list::toString() {
    node *p = this->head;
    string result;
    stringstream ss;
    while (p != NULL) {
      ss << p->data;
      string t;
      ss >> t;
      result += t + "->";
      ss.clear();
      p = p->next;
    }
    result += "NULL";
    return result;
}
//清空链表
void list::clear() {
  if (this->head != NULL) {
    node *p = this->head;
    while (p != NULL) {
      node *t = p;
      p = p->next;
      delete t;
    }
    this->head = NULL;
  }
  this->size = 0;
}
//析构函数
list::~list() {
  this->clear();
}
//运算符重载
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;
}
//在position处插入节点(data为n)
void list::insert(int position, const int& n) {
  if (position > this->size||position < 0) {
    return;
  } else if (position == 0) {
    node *t = new node(n, this->head);
    this->head = t;
  } else {
    node *p = this->head;
    int count = 1;
    while (count != position) {
      p = p->next;
      count++;
    }
    node* t = new node(n, p->next);
    p->next = t;
  }
  this->size++;
}
//将第position个节点删除掉
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--;
}
//对链表进行排序
//时间复杂度是1
//优于数组排序算法
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;
}
//返回链表节点数
int list::getsize(void) {
  return size;
}
//判断链表是否为空
bool list::isempty() const {
  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;
}

输入
5
0 0 0 1 0 2 0 3 0 4
4 0 1 2 3
输出
4->3->2->1->0->NULL size:5
4->3->2->1->0->NULL size:5
4->3->2->1->0->NULL size:5
4->3->2->1->0->NULL size:5
3->1->NULL
1->3->NULL
0->1->2->3->4->NULL
0->1->2->3->4->NULL

#

花了接近两天的时间思考整道题的思路
学到了很多东西
接下来要学习双向链表了

你可能感兴趣的:(C++)