数据结构概论
A data structure is a way
to store and organize data
in a computer, so that it
can be used efficiently.
We talk about data structures as:
1)Mathematical/Logical models OR Abstract data types 2)Implementation
Abstract data types(ADTS)
List as abstract data type
List -Store a given number of elments of a given data-type
链表数据结构
Array VS linked list
时间复杂度
插入/删除元素的时间复杂度
使用简单程度 数组yyds
C/C++关于链表的实现
Linked List: Inserting a node at beginning
void InsertHead(int x) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node*));
temp->data = x;
temp->next = NULL;
/*if (head != NULL) {
temp->next = head;
}*/
temp->next = head;
head = temp;
}
Linked List: Inserting a node at random
#include<stdio.h>
#include<malloc.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head;
void Insert (int data, int n) {
struct Node* temp1 = (struct Node*)malloc(sizeof(struct Node));
temp1->data = data;
temp1->next = NULL;
if (n == 1) {
temp1->next = head;
head = temp1;
return;
}
struct Node* temp2 = head;
for (int i = 0; i < n - 2; i++) {
temp2 = temp2->next;
}
temp1->next = temp2->next;
temp2->next = temp1;
}
void Print() {
struct Node* temp = head;
while (head != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
head = NULL;
Insert(2, 1);//List:2
Insert(3, 2);//List:2,3
Insert(4, 1);//List:4,2,3
Insert(5, 2);//List:4,5,2,3
Print();
}
LinkedList:Delete a node at ramdom position
void Delete(struct Node* head, int n) {
struct Node* temp1 = head;
if (n == 1) {
head = temp1->next;
free(temp1);
return;
}
for (int i = 0; i < n - 2; i++) {
temp1 = temp1->next;
}
struct Node* temp2 = temp1->next;
temp1->next = temp2->next;
free(temp2);
}
Reverse a linked list
void ReverseList() {
struct Node* temp= head ,*pre= NULL;
while (temp != NULL) {
head = head->next;
temp->next = pre;
pre = temp;
temp = head;
}
head = pre;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head;
void Insert(int data,int n) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node*));
temp->data = data;
temp->next = NULL;
if (n==1) {
temp->next = head;
head = temp;
return;
}
struct Node* temp1 = head;
for (int i = 0; i < n - 2; i++) {
temp1 = temp1->next;
}
temp->next = temp1->next;
temp1->next = temp;
}
void InsertHead(int data) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
//temp->next = NULL;
///*if (head != NULL) {
// temp->next = head;
//}*/
temp->next = head;
head = temp;
}
void Delete(int n) {
struct Node* temp = head;
if (n == 1) {
head = temp->next;
free(temp);
return;
}
for (int i = 0; i < n - 2; i++) {
temp = temp->next;
}
struct Node* temp2 = temp->next;
temp->next = temp2->next;
free(temp2);
}
void ReverseList() {
struct Node* temp= head ,*pre= NULL;
while (temp != NULL) {
head = head->next;
temp->next = pre;
pre = temp;
temp = head;
}
head = pre;
}
void Print() {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
head = NULL;
int n;
InsertHead(2);
/*InsertHead(4);
InsertHead(6);
InsertHead(7);*/
Print();/*
printf("Press the position want delete:\n");
scanf("%d", &n);
Delete(n);*/
printf("reverse list:\n");
ReverseList();
Print();
return 0;
}
Print linked list using recursion
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Node {
int data;
struct Node* next;
};
struct Node* Insert(struct Node* head, int data) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if (head == NULL) {
head = temp;
}
else {
struct Node* temp1 = head;
while (temp1->next!= NULL) {
temp1 = temp1->next;
}
temp1->next = temp;
}
return head;
}
void Print(struct Node* p) {
if (p == NULL) {
return;
}
Print(p->next);
printf("%d ", p->data);
}
int main() {
struct Node* head = NULL;
head = Insert(head, 2);
head = Insert(head, 4);
head = Insert(head, 7);
head = Insert(head, 8);
Print(head);
return 0;
}
正向输出结果的逻辑图
反向输出结果的逻辑图
Doubly Linked List
Doubly Linked List Implements
牛逼!!!看视频之前,自己写出来了!!!!!!
#include<stdio.h>
#include<malloc.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* head;
void InsertAtHead(int data) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
temp->prev = NULL;
if (head == NULL) {
head = temp;
return;
}
struct Node* temp1 = head;
temp->next = temp1;
temp1->prev = temp;
head = temp;
}
void InsertAtTail(int data) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
temp->prev = NULL;
if (head == NULL) {
head = temp;
return;
}
struct Node* temp1 = head;
while (temp1->next != NULL) {
temp1 = temp1->next;
}
temp1->next = temp;
temp->prev = temp1;
}
void Delete() {
}
void Print() {
if (head == NULL) {
return;
}
struct Node* temp = head;
while (temp != NULL) {
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
head = NULL;
InsertAtHead(7);
InsertAtHead(8);
InsertAtHead(1);
InsertAtHead(2);
InsertAtTail(1);
InsertAtTail(8);
InsertAtTail(6);
InsertAtTail(7);
Print();
}
反向输出
Introduction to Stack
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 101
int A[MAX_SIZE];
int top = -1;
void Push(int x) {
if (top == MAX_SIZE - 1) {
printf("Error:stack overflow\n");
return;
}
A[++top] = x;
}
void Pop() {
if (top == -1) {
printf("Error:stack is empty\n");
return;
}
top--;
}
int Top() {
return A[top];
}
void Print() {
int i;
printf("Stack list is: ");
for (i = 0; i <= top; i++) {
printf("%d ", A[i]);
}
printf("\n");
}
int main() {
Push(2);Push(4);Push(7);Push(9);Push(8);Print();
Pop();Print();
Pop(); Print();
Pop(); Print();
Pop(); Print();
Pop(); Print();
}
Stack-链表表示方式
耶!又是自己写的,但是好像压栈写的复杂了......
#include<stdio.h>
#include<malloc.h>
#include<stdbool.h>
struct Node {
int data;
struct Node* next;
};
struct Node* top=NULL;
void Push(int x) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = x;
temp->next = NULL;
if (top == NULL) {
top = temp;
return;
}
struct Node* temp2 = top;
temp->next = temp2;
top = temp;
}
void Pop() {
struct Node* temp = top;
if (temp == NULL) {
printf("Error:Empty stack no more pop\n");
return;
}
top = temp->next;
free(temp);
}
int GetTop() {
if (top == NULL) {
printf("Stack is empty, no top element");
return;
}
return top->data;
}
bool IsEmpty() {
if (top == NULL) {
return true;
}
else {
return false;
}
}
void Print() {
if (top == NULL) {
printf("stack is empty\n");
return;
}
struct Node* temp = top;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
Push(2); Push(6); Push(7); Print();
Pop(); Print(); GetTop();
Pop(); Print();
Pop(); Print();
Pop(); Print();
bool empty = IsEmpty();
if (empty) {
printf("stack is empty\n");
}
else {
printf("stack is not empty\n");
}
return 0;
}
Using Stack to reverse
Reverse a string
#include<iostream>
#include<stack>
#include<cstring>
#include<stdio.h>
using namespace std;
void ReverseString(char C[],int n) {
stack<char> S;
for (int i = 0; i < n; i++) {
S.push(C[i]);
}
for (int i = 0; i < n; i++) {
C[i] = S.top();
S.pop();
}
}
int main() {
char C[51];
printf("press a string:");
gets_s(C);
ReverseString(C,strlen(C));
printf("Reverse stack is: %s", C);
return 0;
}
Reverse a linked list
void Reverse() {
if (head == NULL) {
return;
}
struct Node* temp = head;
stack<struct Node*> S;
while (temp != NULL) {
S.push(temp);
temp = temp->next;
}
temp = S.top();
head = temp;
S.pop();
while (!S.empty()) {
temp->next = S.top();
S.pop();
temp = temp->next;
}
temp->next = NULL;
}
括号匹配
前缀、中缀、后缀
前缀-波兰表达式 后缀-逆波兰表达式(求值的时间和内存的代价最小,最容易实现)
Infix to postfix
Introduction to Queue
链表的方式实现队列
树
#include<iostream>
using namespace std;
struct BstTree {
int data;
BstTree* left;
BstTree* right;
};
BstTree* CreateNode(int data){
BstTree* BstNode = new BstTree();
BstNode->data = data;
BstNode->left = BstNode->right = NULL;
return BstNode;
}
BstTree* InsertNode(BstTree* root, int data) {
if (root == NULL) {
root = CreateNode(data);
}
else if (data < root->data) {
root->left=InsertNode(root->left, data);
}
else {
root->right = InsertNode(root->right, data);
}
return root;
}
bool SearchNode(BstTree* root, int data) {
if (root == NULL) {
return false;
}else if (root->data == data) {
return true;
}else if (data < root->data) {
return SearchNode(root->left, data);
}
else {
return SearchNode(root->right, data);
}
}
int main() {
BstTree* root = NULL;
root = InsertNode(root, 15);
root = InsertNode(root, 10);
root = InsertNode(root, 20);
root = InsertNode(root, 25);
root = InsertNode(root, 8);
root = InsertNode(root, 12);
int number;
cin >> number;
if (SearchNode(root, number) == true) {
cout << "Found!"<<endl;
}
else {
cout << "Not Found" << endl;
}
return 0;
}
BST递归实现的过程
Find Min and Max element in a BST
//迭代的方式找最小/大元素
int FindMin(BstTree* root) {
if (root == NULL) {
cout << "empty tree" << endl;
return -1;
}
while (root->left != NULL) {
root = root->left;
}
return root->data;
}
int FindMax(BstTree* root) {
if (root == NULL) {
cout << "empty tree" << endl;
return -1;
}
while (root->right != NULL) {
root = root->right;
}
return root->data;
}
//递归的方式找最小/大的节点
int findMin(BstTree* root) {
if (root == NULL) {
cout << "empty tree" << endl;
return -1;
}
else if (root->left == NULL) {
return root->data;
}
return findMin(root->left);
}
int findMax(BstTree* root) {
if (root == NULL) {
cout << "empty tree" << endl;
return -1;
}
while (root->right == NULL) {
return root->data;
}
return findMax(root->right);
}
树的高度
//计算树的高度
int FindHeight(BstTree* root) {
if (root == NULL) {
return -1;
}
return max(FindHeight(root->left), FindHeight(root->right)) + 1;
}
二叉树遍历:深度遍历vs广度遍历
广度遍历:层序遍历
深度遍历:
先序遍历
中序遍历
后序遍历
//广度遍历:层序遍历
void LevelOrder(BstTree* root) {
if (root == NULL) {
return;
}
queue<BstTree*> Q;
Q.push(root);
while (!Q.empty()) {
BstTree* current = Q.front();
cout << current->data << " ";
if (current->left != NULL) Q.push(current->left);
if (current->right != NULL) Q.push(current->right);
Q.pop();
}
}
//深度遍历:前序遍历、中序遍历、后序遍历
void PreOrder(BstTree* root) {
if (root == NULL) {
return;
}
cout << root->data << " ";
PreOrder(root->left);
PreOrder(root->right);
}
void midOrder(BstTree* root) {
if (root == NULL) {
return;
}
midOrder(root->left);
cout << root->data << " ";
midOrder(root->right);
}
void laterOrder(BstTree* root) {
if (root == NULL) {
return;
}
laterOrder(root->left);
laterOrder(root->right);
cout << root->data << " ";
}