数据结构与算法分析读书笔记1———链表、栈和队列
基于《数据结构与算法分析——c++语言描述》,原理可以参考这本书,本文是自己作为练习写的简单的程序,代码写的粗制滥造,存在很多bug,后续有时间再修改。
链表
链表作为常用的一种数据结构,可以分为单链表、双链表、循环链表和多重表。
单链表
struct Node {
int val;
struct Node *next;
Node() : next(nullptr) {}
Node(int val, Node *p) : val(val), next(p) {}
Node(int val) : val(val), next(nullptr) {}
};
class LinkList {
public:
LinkList() : head(new Node) { tail = head; }
~LinkList() {
Node *tmp = head->next;
while (true) {
delete head;
head = tmp;
if (head == nullptr) break;
tmp = tmp->next;
}
}
void append(int val) {
Node *tmp = new Node(val);
tail->next = tmp;
tail = tmp;
}
void insertVal(int i, int val) {
Node *p = head;
if (i < 0) return;
while (p != nullptr && --i) p = p->next;
if (p == nullptr) append(val);
Node *tmp = new Node(val);
tmp->next = p->next;
p->next = tmp;
}
void deleteVal(int val) {
Node *p = head;
while (p->next != nullptr) {
if (p->next->val == val) {
auto q = p->next;
p->next = p->next->next;
delete q;
}
p = p->next;
}
}
Node *searchVal(int val) {
Node *p = head->next;
while (p != nullptr && p->val != val) p = p->next;
return p;
}
Node *iterator() {
return head->next;
}
private:
Node *head;
Node *tail;
};
双链表
struct Node {
int data;
struct Node *prior;
struct Node *next;
Node() : prior(nullptr), next(nullptr) {}
Node(int val) : data(val), prior(nullptr), next(nullptr) {}
};
class DoubleLinkList {
public:
DoubleLinkList() : head(new Node) { tail = head; }
~DoubleLinkList() {
Node *tmp = head->next;
while(true) {
delete head;
head = tmp;
if (head == nullptr) break;
tmp = head->next;
}
}
void head_add(int val) {
Node *tmp = new Node(val);
tmp->next = head->next;
head->next->prior = tmp;
head->next = tmp;
tmp->prior = head;
}
void tail_add(int val) {
Node *tmp = new Node(val);
tail->next = tmp;
tmp->prior = tail;
tail = tmp;
}
void insert(int i, int val) {
Node *p = head;
while (p->next && --i) p = p->next;
if (p->next == nullptr) tail_add(val);
Node *tmp = new Node(val);
tmp->next = p->next;
p->next->prior = tmp;
p->next = tmp;
tmp->prior = p;
}
void deleteVal(int val) {
Node *p = head;
while (p->next) {
if (p->next->data == val) {
auto tmp = p->next;
p->next->next->prior = p;
p->next = p->next->next;
delete tmp;
}
p = p->next;
}
}
private:
Node *head;
Node *tail;
};
栈
struct Node {
int data;
struct Node *prior;
struct Node *next;
Node() : prior(nullptr), next(nullptr) {}
Node(int val) : data(val), prior(nullptr), next(nullptr) {}
};
class MyStack {
public:
MyStack() : backPointer(nullptr), topPointer(nullptr), len(0){}
~MyStack() {}
void push(int val) {
Node *tmp = new Node(val);
if (backPointer == nullptr) {
backPointer = tmp;
topPointer = tmp;
}
else {
topPointer->next = tmp;
tmp->prior = topPointer;
topPointer = tmp;
}
len++;
}
void pop() {
Node *tmp = topPointer->prior;
topPointer->prior->next = nullptr;
topPointer->prior = nullptr;
delete topPointer;
topPointer = tmp;
len--;
}
int top() {
if (topPointer)
return topPointer->data;
}
bool isEmpty() {
if (len == 0) return true;
return false;
}
private:
int len;
Node *backPointer;
Node *topPointer;
};
队列
struct Node {
int val;
struct Node *next;
Node() : next(nullptr) {}
Node(int val, Node *p) : val(val), next(p) {}
Node(int val) : val(val), next(nullptr) {}
};
class Queue {
public:
Queue() : back(nullptr), front(nullptr) {}
void enqueue(int val) {
Node *tmp = new Node(val);
if (front == nullptr) {
back = tmp;
front = tmp;
}
else {
back->next = tmp;
}
}
int dequeue() {
Node *tmp = front;
front = front->next;
return front->val;
}
private:
Node *back;
Node *front;
};