线性表
1.顺序表
需求分析
/*创建顺序表具体功能:初始化顺序表销毁顺序表获取顺序表元素个数输出顺序表中的内容自动扩容增 --- 插入数据(包含了尾部添加功能)删 --- 删除数据(包含了尾部删除功能)改 --- 修改数据查 --- 查找数据尾部追加尾部删除*/
C语言实现
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int dataType;
typedef struct SeqList {dataType* arr; int capacity; int size;
}SeqList;
bool initList(SeqList&, int);
void destroyList(SeqList&);
int sizeList(SeqList&);
void printList(SeqList&);
bool expandList(SeqList&, int);
bool insertList(SeqList&, dataType, int);
bool deleteList(SeqList&, int);
bool modifyList(SeqList&, dataType, int);
int findList(SeqList&, dataType);
bool appendList(SeqList&, dataType);
bool popList(SeqList&);
int main()
{SeqList myList;bool res = initList(myList, 5); (res == true) ? printf("顺序表初始化成功!\n") : printf("顺序表初始化失败!\n");int size = sizeList(myList);printf("顺序表中元素的个数:%d\n", size);printList(myList);insertList(myList, 10, 0);insertList(myList, 20, 0);insertList(myList, 30, 0);insertList(myList, 40, 0);printList(myList);deleteList(myList, 2); printList(myList);modifyList(myList, 66, 2); printList(myList);int searchTarget = 40;int index = findList(myList, searchTarget);printf("元素%d的下标:%d\n", searchTarget, index);appendList(myList, 50);printList(myList);popList(myList);printList(myList);destroyList(myList);char justPause = getchar(); return 0;
}
bool initList(SeqList& sl, int capacity)
{sl.arr = (dataType*)malloc(sizeof(dataType) * capacity);if (sl.arr == NULL) return false; sl.capacity = capacity; sl.size = 0; return true;
}
void destroyList(SeqList& sl)
{free(sl.arr); sl.arr = NULL;sl.size = 0;sl.capacity = 0;
}
int sizeList(SeqList& sl)
{return sl.size;
}
void printList(SeqList& sl)
{printf("[ ");for (int i = 0; i < sl.size; i++){printf("%d, ", sl.arr[i]);}printf("]\n");
}
bool expandList(SeqList& sl, int size)
{dataType* p = (dataType*)malloc(sizeof(dataType) * (sl.capacity + size));if (sl.arr == NULL) return false; for (int i = 0; i < sl.size; i++){p[i] = sl.arr[i];}free(sl.arr);sl.capacity += size;sl.arr = p;p = NULL;return true;
}
bool insertList(SeqList& sl, dataType element, int index)
{if (index < 0 || index > sl.size) return false;if (sl.size == sl.capacity){if (expandList(sl, 10) == false) return false; }for (int i = sl.size; i > index; --i){sl.arr[i] = sl.arr[i - 1];}sl.arr[index] = element;sl.size++;return true;
}
bool deleteList(SeqList& sl, int index)
{if (sl.size == 0) return false;if (index < 0 || index > sl.size - 1) return false;for (int i = index; i < sl.size - 1; i++){sl.arr[i] = sl.arr[i + 1];}sl.size--; return true;
}
bool modifyList(SeqList& sl, dataType element, int index)
{if (index < 0 || index > sl.size - 1) return false;sl.arr[index] = element;return true;
}
int findList(SeqList& sl, dataType element)
{for (int i = 0; i < sl.size; i++){if (sl.arr[i] == element) return i;}return -1;
}
bool appendList(SeqList& sl, dataType element)
{return insertList(sl, element, sl.size);
}
bool popList(SeqList& sl)
{return deleteList(sl, sl.size - 1);
}
C++实现
#include <iostream>
template<class T>
class SeqList
{
private:T* arr; int capacity; int size; public:SeqList(); ~SeqList(); int sizeList(); void printList(); bool expandList(); bool insertList(T, int); bool deleteList(int); bool modifyList(T, int); int findList(T); bool appendList(T); bool popList();
};int main()
{SeqList<int> sl;int size = sl.sizeList();printf("顺序表中元素的个数:%d\n", size);sl.printList();sl.insertList(10, 0);sl.insertList(20, 0);sl.insertList(30, 0);sl.insertList(40, 0);sl.printList();sl.deleteList(2); sl.printList();sl.modifyList(66, 2); sl.printList();int searchTarget = 40;int index = sl.findList(searchTarget);printf("元素%d的下标:%d\n", searchTarget, index);sl.appendList(50);sl.printList();sl.popList();sl.printList();system("pause"); return 0;
}
template<class T>
SeqList<T>::SeqList()
{this->arr = new T[capacity]; this->capacity = capacity; this->size = 0;
}
template<class T>
SeqList<T>::~SeqList()
{delete this->arr; this->capacity = 0; this->size = 0;
}
template<class T>
int SeqList<T>::sizeList()
{return this->size;
}
template<class T>
void SeqList<T>::printList()
{std::cout << "[ ";for (int i = 0; i < this->size; i++){std::cout << this->arr[i] << ", ";}std::cout << "]" << std::endl;
}
template<class T>
bool SeqList<T>::expandList()
{T* temp = new T[this->capacity + 10]; if (temp == nullptr) return false;for (int i = 0; i < this->size; i++){temp[i] = this->arr[i];}delete this->arr;this->arr = temp;this->capacity += size;temp = nullptr;return true;
}
template<class T>
bool SeqList<T>::insertList(T element, int index)
{if (index < 0 || index > this->size) return false;if (this->size == this->capacity) expandList();for (int i = size; i > index; i--){this->arr[i] = this->arr[i - 1];}this->arr[index] = element;this->size++;return true;
}
template<class T>
bool SeqList<T>::deleteList(int index)
{if (index < 0 || index > this->size - 1) return false;for (int i = index; i < this->size - 1; i++){this->arr[i] = this->arr[i + 1];}this->size--;return true;
}
template<class T>
bool SeqList<T>::modifyList(T element, int index)
{if (index < 0 || index > this->size - 1) return false;this->arr[index] = element;return true;
}
template<class T>
int SeqList<T>::findList(T element)
{for (int i = 0; i < this->size; i++){if (this->arr[i] == element) return i;}return -1;
}
template<class T>
bool SeqList<T>::appendList(T element)
{return insertList(element, this->size);
}
template<class T>
bool SeqList<T>::popList()
{return deleteList(this->size - 1);
}
2.链表
需求分析
C语言实现
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int dataType;
typedef struct Node
{dataType data; Node* next;
}Node;
void init(Node*);
int size(Node*);
void print(Node*);
bool insert(Node*, dataType, int);
bool remove(Node*, int);
bool modify(Node*, dataType, int);
int find(Node*, dataType);
bool append(Node*, dataType);
bool pop(Node*);
void destroy(Node*);int main()
{Node headNode;init(&headNode);printf("链表元素的个数:%d\n", size(&headNode));print(&headNode);insert(&headNode, 66, 1);insert(&headNode, 77, 1);insert(&headNode, 88, 1);print(&headNode);remove(&headNode, 1);print(&headNode);modify(&headNode, 99, 0);print(&headNode);int index = find(&headNode, 66);(index == -1) ? printf("查找不到此数据!\n") : printf("查到此数据的下标:%d\n", index);append(&headNode, 6666);print(&headNode);pop(&headNode);print(&headNode);destroy(&headNode);char justPause = getchar(); return 0;
}
void init(Node* node)
{node->next = NULL;
}
int size(Node* node)
{int size = 1;while (node->next){size++;node = node->next;}return size;
}
void print(Node* node)
{printf("(*) -> ");while (node->next){node = node->next;printf("%d -> ", node->data);}printf("\n");
}
bool insert(Node* node, dataType element, int index)
{if (index < 1) return false;while (--index){node = node->next;if (node == NULL) return false;}Node* temp = (Node*)malloc(sizeof(Node)); if (temp == NULL) return false; temp->data = element; temp->next = node->next;node->next = temp;return true;
}
bool remove(Node* node, int index)
{if (index < 1) return false;while (--index){node = node->next;if (node == NULL) return false;}Node* temp = node->next; node->next = node->next->next; free(temp); return true;
}
bool modify(Node* node, dataType element, int index)
{if (index < 1) return false;while (--index){node = node->next;if (node == NULL) return false;}node->next->data = element;return true;
}
int find(Node* node, dataType element)
{int index = 0; while (node->next){node = node->next;index++;if (node->data == element) return index; }return -1;
}
bool append(Node* node, dataType element)
{while (node->next){node = node->next;}Node* temp = (Node*)malloc(sizeof(Node)); if (temp == NULL) return false; temp->data = element;temp->next = NULL;node->next = temp;return true;
}
bool pop(Node* node)
{if (node->next == NULL) return false;Node* prev = NULL;while (node->next){prev = node; node = node->next; }free(node);prev->next = NULL;return true;
}
void destroy(Node* node)
{Node* temp = NULL;while (node->next){temp = node->next; node->next = node->next->next;free(temp);}
}
C++实现
#include<iostream>
template <class T>
class Node
{
public:T data; Node<T>* next; Node(){this->next = nullptr;}~Node(){this->next = nullptr;}
};
template<class T = int>
class Link
{
private:Node<T>* head; int size; public:Link();~Link();int getSize();void printLink();bool insert(T, int);bool remove(int);bool modify(T, int);int find(T);bool append(T);bool pop();
};int main()
{Link<> myLink;printf("链表元素的个数:%d\n", myLink.getSize());myLink.printLink();myLink.insert(66, 1);myLink.insert(77, 1);myLink.insert(88, 1);myLink.insert(99, 1);myLink.printLink();myLink.remove(1);myLink.printLink();myLink.modify(100, 2);myLink.printLink();int index = myLink.find(66);(index == -1) ? printf("查找不到此数据!\n") : printf("查到此数据的下标:%d\n", index);myLink.append(1024);myLink.printLink();myLink.pop();myLink.printLink();system("pause"); return 0;
}
template<class T>
Link<T>::Link()
{this->head = new Node<T>; this->size = 1;
}
template<class T>
Link<T>::~Link()
{Node<T>* temp = nullptr;while (this->head->next) {temp = this->head->next; this->head->next = this->head->next->next; delete temp; }delete this->head;this->head = nullptr;this->size = 0;
}
template<class T>
int Link<T>::getSize()
{return this->size;
}
template<class T>
void Link<T>::printLink()
{Node<T>* temp = this->head->next; std::cout << "(*) -> ";while (temp) {std::cout << temp->data << " -> ";temp = temp->next;}std::cout << std::endl;
}
template<class T>
bool Link<T>::insert(T data, int index)
{if (index < 1 || index > this->size) return false;Node<T>* targetNodePre = this->head;for (int i = 0; i < index - 1; i++){targetNodePre = targetNodePre->next;}Node<T>* newNode = new Node<T>;newNode->data = data;newNode->next = targetNodePre->next;targetNodePre->next = newNode;this->size++;return true;
}
template<class T>
bool Link<T>::remove(int index)
{if (index < 1 || index > this->size) return false;Node<T>* targetNodePre = this->head;for (int i = 0; i < index - 1; i++){targetNodePre = targetNodePre->next;}Node<T>* temp = targetNodePre->next; targetNodePre->next = targetNodePre->next->next; delete temp; this->size--;return true;
}
template<class T>
bool Link<T>::modify(T data, int index)
{if (index < 1 || index > this->size) return false;Node<T>* targetNodePre = this->head;for (int i = 0; i < index - 1; i++){targetNodePre = targetNodePre->next;}targetNodePre->next->data = data;return true;
}
template<class T>
int Link<T>::find(T data)
{int index = 0; Node<T>* targetNodePre = this->head;while (targetNodePre->next) {targetNodePre = targetNodePre->next;index++;if (targetNodePre->data == data) return index; }return -1;
}
template<class T>
bool Link<T>::append(T data)
{return insert(data, this->size);
}
template<class T>
bool Link<T>::pop()
{return remove(this->size - 1);
}
3.扩展
双向链表
#include<iostream>
template<class T>
class Node
{
public:T data;Node* next;Node* prev;Node(){this->next = nullptr;this->prev = nullptr;}~Node(){this->next = nullptr;this->prev = nullptr;}
};
template<class T = int>
class Link
{
private:Node<T>* head;int size;public:Link(){this->head = new Node<T>;this->size = 1;}~Link(){Node<T>* temp = nullptr;while (this->head->next){temp = this->head->next;this->head->next = this->head->next->next;delete temp;}delete this->head;this->head = nullptr;this->size = 0;}bool insert(T data, int index){if (index < 1 || index > this->size) return false;Node<T>* targetNodePre = this->head;for (int i = 1; i < index; i++){targetNodePre = targetNodePre->next;}Node<T>* newNode = new Node<T>;newNode->data = data;newNode->next = targetNodePre->next;newNode->prev = targetNodePre;if (targetNodePre->next != nullptr) targetNodePre->next->prev = newNode;targetNodePre->next = newNode;this->size++;return true;}bool remove(int index){if (index < 1 || index > this->size - 1) return false;Node<T>* targetNode = this->head;for (int i = 1; i <= index; i++){targetNode = targetNode->next;}if (targetNode->next != nullptr) targetNode->next->prev = targetNode->prev;targetNode->prev->next = targetNode->next;delete targetNode;this->size--;return true;}void printLink(){std::cout << "(*) <==> ";Node<T>* temp = this->head;while (temp->next){temp = temp->next;std::cout << temp->data << " <==> ";}std::cout << std::endl;}
};
int main()
{Link<int> myLink;myLink.printLink();myLink.insert(10, 1);myLink.printLink();myLink.insert(20, 2);myLink.printLink();myLink.insert(30, 3);myLink.printLink();myLink.remove(1);myLink.printLink();myLink.remove(1);myLink.printLink();myLink.remove(1);myLink.printLink();system("pause");return 0;
}
循环链表
特殊线性表
1.栈
通过顺序表实现
#include<iostream>
#include<string>
using namespace std;template<class T>
class Stack
{
public:T* arr; int capacity; int top; Stack(){this->arr = new T[10]; this->capacity = 10;this->top = -1;}~Stack(){delete[] this->arr; this->capacity = 0;this->top = -1;}Stack& push(T data){top++;this->arr[top] = data;return *this;}Stack& pop(){top--;return *this;}void show(){string symbol = "";for (int i = 0; i <= top; i++){symbol += "----";}cout << "*" << symbol << endl << "| ";for (int i = 0; i <= top; i++){std::cout << this->arr[i] << " ";}cout << endl << "*" << symbol << endl << endl;}
};int main()
{Stack<int> s;s.push(10);s.push(20);s.push(30).show();s.pop().show();s.pop().show();s.pop().show();system("pause");return 0;
}
通过链表实现
#include<iostream>
#include<string>
using namespace std;
template<class T>
class Node
{
public:T data;Node* next;Node(){this->next = nullptr;}
};
template<class T>
class Stack
{
public:Node<T>* head; int size; Stack(){this->head = new Node<T>; this->size = 0;}~Stack(){Node<T>* temp = nullptr;while (this->head->next){temp = this->head->next;this->head->next = this->head->next->next;delete temp;}delete this->head;this->head = nullptr;this->size = 0;}Stack& push(T data){Node<T>* newNode = new Node<T>;newNode->data = data;newNode->next = this->head->next;this->head->next = newNode;size++;return *this;}Stack& pop(){Node<T>* temp = nullptr;if (this->head->next){temp = this->head->next;this->head->next = this->head->next->next;delete temp;}size--;return *this;}void show(){string symbol = "";for (int i = 0; i < this->size; i++){symbol += "----";}cout << "*" << symbol << endl << "| ";if (this->size == 0){cout << endl << "*" << symbol << endl << endl;return;}Node<T>** arr = new Node<T>*[this->size];int index = 0;Node<T>* temp = this->head;while (temp->next){temp = temp->next;arr[index] = temp;index++;}for (int i = this->size - 1; i > -1; i--){std::cout << arr[i]->data << " ";}cout << endl << "*" << symbol << endl << endl;delete[] arr;}
};int main()
{Stack<int> s;s.push(10);s.push(20);s.push(30).show();s.pop().show();s.pop().show();s.pop().show();system("pause");return 0;
}
2.队列
通过顺序表实现
// C++#include<iostream>
#include<string>
using namespace std;// 队列所存储数据的类型
typedef int E;// 队列 (通过顺序表实现循环队列)
class Queue
{
public:E* arr; // 用于记录队列的内存地址int capacity; // 用于记录队列的容量int size; // 用于记录队列中元素的个数int front; // 用于记录队首位置int rear; // 用于记录队尾位置Queue(){this->capacity = 5;this->arr = new E[this->capacity];this->size = 0;this->front = this->rear = 0;}~Queue(){delete[] this->arr;this->arr = nullptr;this->capacity = 0;this->size = 0;this->front = this->rear = 0;}// 入队Queue& enqueue(E data){// 判断队列是否已满if (this->size == this->capacity) return *this;// 循环入队this->arr[rear] = data;this->size++;this->rear++;this->rear %= this->capacity; // 循环队列的核心算法return *this;}// 出队Queue& dequeue(){// 判断队列是否为空if (size == 0) return *this;// 循环出队this->front++;this->size--;this->front %= this->capacity; // 循环队列的核心算法return *this;}// 输出void show(){string symbol = "";for (int i = 0; i < this->size; i++){symbol += "<<<<";}if (this->size == 0) symbol = "<<<<";cout << symbol << endl;for (int i = this->front; i < this->front + this->size; i++){cout << this->arr[i % this->capacity] << " ";}cout << endl << symbol << endl << endl;}
};int main()
{Queue q; // 创建并初始化队列q.show(); // 输出队列里面的内容// 入队q.enqueue(10).show();q.enqueue(20).show();q.enqueue(30).show();// 出队q.dequeue().show();q.dequeue().show();q.dequeue().show();system("pause");return 0;
}
通过链表实现
#include<iostream>
#include<string>
using namespace std;
typedef int E;
class Node
{
public:E data;Node* next;Node(){this->next = nullptr;}
};
class Queue
{
public:Node* head; Node* tail; int size; Queue(){this->head = new Node;this->tail = this->head;this->size = 0;}~Queue(){Node* temp = nullptr;while (this->head->next){temp = this->head->next;this->head->next = this->head->next->next;delete temp;}delete this->head;this->head = this->tail = nullptr;this->size = 0;}Queue& enqueue(E data){Node* newNode = new Node;newNode->data = data;newNode->next = nullptr;this->tail->next = newNode;this->tail = newNode;this->size++;return *this;}Queue& dequeue(){Node* temp = nullptr;if (this->head->next){temp = this->head->next;this->head->next = this->head->next->next;delete temp;}this->size--;return *this;}void show(){string symbol = "";for (int i = 0; i < this->size; i++){symbol += "<<<<";}if (this->size == 0) symbol = "<<<<";cout << symbol << endl;Node* temp = this->head;while (temp->next){temp = temp->next;cout << temp->data << " ";}cout << endl << symbol << endl << endl;}
};int main()
{Queue q; q.show(); q.enqueue(10).show();q.enqueue(20).show();q.enqueue(30).show();q.dequeue().show();q.dequeue().show();q.dequeue().show();system("pause");return 0;
}