放上一些没头没尾的东西
数据结构
线性表
顺序表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include <stdio.h> #include <string.h> #include <stdlib.h>
#define IniSize 100 #define false 0
typedef int ElemType;
typedef struct { ElemType *data; int MaxSize,length; } SeqList;
void initList(SeqList *L) { L->data = (ElemType*)malloc(sizeof(ElemType)*IniSize); L->length = 0; L->MaxSize = IniSize; }
void ListAdd(SeqList *L, ElemType e) { if(L->length == IniSize) return false; L->data[L->length] = e; L->length++; }
void ListInsert(SeqList *L, int i, ElemType e) { if(i<1 || i>L->length || L->length >= IniSize) return false;
for(int j=L->length; j>=i; j--) L->data[j] = L->data[j-1];
L->data[i-1] = e; L->length++; }
void ListDelete(SeqList *L, int i, ElemType e) { if(i<1 || i>L->length) return false;
e = L->data[i-1];
for(int j=i; j<L->length; j++) L->data[j-1] = L->data[j]; L->length--; }
void LocatElem(SeqList *L, ElemType e) { int i; for(i=0; i<L->length; i++) { if(L->data[i] == e) return i+1; }
return false; }
void Reverse(SeqList *L) { ElemType t; for(int i=0; i<L->length/2; i++) { t = L->data[i]; L->data[i] = L->data[L->length-i-1]; L->data[L->length-i-1] = t; } }
|
单链表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| #include <stdio.h> #include <string.h> #include <stdlib.h>
typedef int ElemType;
typedef struct LNode { ElemType data; struct LNode *next; }LNode, *LinkList;
LinkList List_HeadInsert(LinkList L) { LNode *s; int x; L = (LinkList)malloc(sizeof(LNode)); L->next = NULL;
scanf("%d", &x);
while(x != 9999) { s = (LNode*)malloc(sizeof(LNode)); s->data = x; s->next = L->next; L->next = s; scanf("%d",&x); }
return L; }
LinkList List_TailInsert(LinkList L) { LNode *s; int x; L = (LinkList)malloc(sizeof(LNode)); LNode *r = L;
scanf("%d", &x);
while(x != 9999) { s = (LNode*)malloc(sizeof(LNode)); s->data = x; r->next = s; r = s; scanf("%d", &x); }
r->next = NULL; return L; }
LinkList GetElem(LinkList L, int i) { int j=1; LNode *p=L->next;
if(!i) return L; if(i<1) return NULL; while(p && j<i) { p = p->next; j++; }
return p; }
LinkList LocateElem(LinkList L, ElemType e) { LNode *p=L->next;
while(p!=NULL && p->data!=e) p = p->next;
return p; }
|
双链表
1 2 3 4 5 6 7 8 9 10 11
| #include <stdio.h> #include <string.h> #include <stdlib.h>
typedef int ElemType;
typedef struct DNode //与单链表区别在于插入与删除元素 插入删除元素均为o(1) { ElemType data; struct DNode *prior, *next; }DNode, *DLinkList;
|
顺序栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| #include <stdio.h> #include <string.h> #include <stdlib.h>
#define Maxsize 50 #define false 0 #define true 1
typedef char ElemType;
typedef struct //基础版 { ElemType data[Maxsize]; int top; } SeqStack;
void InitStack(SeqStack *S) { S->top = -1; }
int StackEmpty(SeqStack S) { if(S.top == -1) return true; else return false; }
void Push(SeqStack *S, ElemType x) { if(S->top == Maxsize-1) return false;
S->top++; S->data[S->top] = x;
return true; }
void Pop(SeqStack *S,ElemType *x) { if(S->top == -1) return false;
*x = S->data[S->top]; S->top--;
return true; }
ElemType Gettop(SeqStack S) { if(S.top == -1) return false;
return S.data[S.top]; }
void BracketsCheck(ElemType str[]) { SeqStack S; InitStack(&S); char ch; int i;
while(str[i] != '\0') { switch (str[i]) { case '{': case '[': case '(': Push(&S, str[i]); break; case '}': ch = Gettop(S); if(ch == '{') Pop(&S, &ch); else return false; break;
case ']': ch = Gettop(S); if(ch == '[') Pop(&S, &ch); else return false; break;
case ')': ch = Gettop(S); if(ch == '(') Pop(&S, &ch); else return false; break; }
i++; }
if(StackEmpty(S)) return true; else return false; }
|
链式队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #include <stdio.h> #include <string.h> #include <stdlib.h>
#define Maxsize 50 #define false 0 #define true 1
typedef int ElemType;
typedef struct LinkNode //链式队列结点 { ElemType data; struct LinkNode *next; } LinkNode;
typedef struct //链式队列 { LinkNode *front, *rear; } LinkQueue;
void InitQueue(LinkQueue *Q) { Q->front = Q->rear = (LinkNode*)malloc(sizeof(LinkNode)); Q->front->next = NULL; }
void isEmpty(LinkQueue Q) { if(Q.front == Q.rear) return true;
else return false; }
void EnQueue(LinkQueue *Q, ElemType x) { LinkNode *s = (LinkNode*)malloc(sizeof(LinkNode));
s->data = x; s->next = NULL;
Q->rear->next = s; Q->rear = s; }
void DeQueue(LinkQueue *Q, ElemType *x) { if(Q->front == Q->rear) return false; LinkNode *p = Q->front->next;
*x = p->data; Q->front->next = p->next;
if(Q->rear == p) Q->rear = Q->front; free(p); }
|
循环队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include <stdio.h> #include <string.h> #include <stdlib.h>
#define Maxsize 50 #define false 0 #define true 1
typedef int ElemType;
typedef struct { ElemType data[Maxsize]; int front, rear; } SeqQueue;
void InitQueue(SeqQueue *Q) { Q->rear = Q->front = 0; }
void isEmpty(SeqQueue Q) { if(Q.rear == Q.front) return true; else return false; }
void EnQueue(SeqQueue *Q, ElemType x) { if((Q->rear+1)%Maxsize == Q->front) return false;
Q->data[Q->rear] = x; Q->rear = (Q->rear)%Maxsize; }
void DeQueue(SeqQueue *Q, ElemType *x) { if(Q->rear == Q->front) return false;
*x = Q->data[Q->front]; Q->front = (Q->front)%Maxsize; }
|
串
next数组值求解和nextval数组求解
https://www.zhihu.com/question/62030859/answer/835271234
假如有这样一串字符串
1 2 3 4 5 6
a a a a a a
这可以说是一个字符串间规律最强的一个数组了吧,让我们来手动模拟一下。
首先默认第一位是0,第二位是1,从第3位开始求,比较第3-1位和next[3-1]的字符是否相同,若相同,则next[3]=next[2]+1
所以第3位的值就是2.那么依此类推就可以得到,这个字符串的next数组为
1 2 3 4 5 6
a a a a a a
0 1 2 3 4 5
总结来看就是相同就在他的next数组值加1.
那么有这样一个字符串那
1 2 3 4 5 6
a b c d e f
默认给出第1位为0 第二位为1 ,发现全都不一样,可以说毫无相关性了,所以next数组为
1 2 3 4 5 6
a b c d e f
0 1 1 1 1 1
这两种极端情况可以让大家初步了解一下计算next数组的方法,起码可以初步理解一下next数组的意义。但是这还不完善。
下面介绍一到北邮2016年考研真题
1 2 3 4 5 6 7 8
a b a a b c a c
0 1 1 2 2 3 1 2
这是一个考试常见的字符串,是如何计算的那?
第n位:next[n]的值来自于第n-1位的字符,通过跟第next[n-1]位字符比较,如果相同next[n]=next[n-1]+1,如果不相同,就跟第next[next[n-1]]位的字符比较,就这样迭代直到相同的时候,加上1,如果实在没有,就为1.
这一段话可能很难理解,逐位分析。
让我们从依次来看:
第3位:第2位和第1位比较,不相同 所以为1
第4位:第3位和第1位比较,相同,所以为2
第5位:第4位和第2位比较,不相同,和第1位比较,相同,所以为2
第6位:第5位和第2位比较, 相同,所以为3
第7位:第6位和第3位比较,不同,和第1位比较,不同,所以为1
第8位:第7位和第1位比较,相同,所以为2.
这就是next数组的手动计算方法。
接下来介绍如何根据next数组计算nextval数组
nextval是在next数组的基础上优化算法,避免不必要的浪费。其实我也不太理解nextval的具体原理,现只能介绍一下如何计算。
依旧用上面北邮的真题为例,其真题本身求的就是nextval数组
现在我们已经有了next数组:
1 2 3 4 5 6 7 8
a b a a b c a c
0 1 1 2 2 3 1 2
现在通过next数组计算nextval数组,nextval数组与next相反,是找不同,
1 2 3 4 5 6 7 8
a b a a b c a c
0 1 1 2 2 3 1 2
0 1 0 2 1 3 0 2
第1位:必为0
第2位:第2位next值为1,所以第2位和第1位比较,不同,为第2位的next 值1
第3位:第3位next值为1,所以第3位和第1位比较,相同,因为到第1位了,所以为0
第4位:第4位next值为2,所以第4位和第2位比较,不同,就为第4位next值2
第5位:第5位next值为2,所以第5位和第2位比较,相同,则继续,第2位和第1位不同,则为第2位的next值1
第6位:第6位next值为3,所以第6位和第3位比较,不同,就为第6位的next值3
第7位:第7位next值为1,所以第7位和第1位比较,相同,则为0
第8位:第8位next值为2,所以第8位和第2位比较,不同,则为第8位的next值2
【简而言之】第n位nextval数组值就是,让第n位字符和第next[n]位比较,不同,则nextval[n]=next[n],如果相同,则比较第next[next[n]]位和第next[n]位比较,如果不同,则nextVal[n]=next[next[n]].就是这样的计算方式。
树
递归二叉树
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <stdio.h> #include <string.h> #include <stdlib.h>
typedef char ElemType;
typedef struct BitNode //二叉树结点结构 { ElemType data; struct BitNode *lchild, *rchild; } BitTree;
BitTree* Creatbitree(BitTree *T) { char ch; scanf("%c", &ch);
if(ch == '#') return 0;
T = (BitTree*)malloc(sizeof(BitTree)); T->data = ch; T->lchild = Creatbitree(T->lchild); T->rchild = Creatbitree(T->rchild); }
void PreOrder(BitTree *T) { if(T) { printf("%c ", T->data); PreOrder(T->lchild); PreOrder(T->rchild); } }
void InOrder(BitTree *T) { if(T) { InOrder(T->lchild); printf("%c ", T->data); InOrder(T->rchild); } }
void PostOrder(BitTree *T) { if(T) { PostOrder(T->lchild); PostOrder(T->rchild); printf("%c ", T->data); } }
|
非递归二叉树(需借助栈和队列结构)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| #include <stdio.h> #include <string.h> #include <stdlib.h>
#define Maxsize 50 #define false 0
typedef char ElemType;
typedef struct BitNode //二叉树结点结构 { ElemType data; struct BitNode *lchild, *rchild; } BitTree;
typedef struct { BitTree *link; int flag; } SeqStack;
BitTree* Creatbitree(BitTree *T) { char ch; scanf("%c", &ch);
if(ch == '#') return 0;
T = (BitTree*)malloc(sizeof(BitTree)); T->data = ch; T->lchild = Creatbitree(T->lchild); T->rchild = Creatbitree(T->rchild); }
void PreOrder(BitTree *T) { BitTree *p,*stack[Maxsize]; int top;
top = 0; p = T;
if(!T) return 0; while(p && top) { while(p) { printf("%c ", p->data);
if(top <= Maxsize-1) stack[++top] = p; else return false;
p = p->lchild; }
if(top <= 0) return false; else { p = stack[top--]; p = p->rchild; } } }
void InOrder(BitTree *T) { BitTree *p,*stack[Maxsize]; int top;
top = 0; p = T;
if(!T) return 0;
while(p && top) { while(p) { printf("%c ", p->data);
if(top <= Maxsize-1) stack[++top] = p; else return false;
p = p->lchild; }
if(top <= 0) return false; else { p = stack[top--]; printf("%c ", p->data); p = p->rchild; } } }
void PostOrder(BitTree *T) { SeqStack stack[Maxsize]; BitTree *p; int top, sign;
top = -1; p = T;
if(!T) return false;
while(p && top!=-1) { if(p) { stack[++top].link = p; stack[top].flag = 1; p = p->lchild; }
else { sign = stack[top].flag; p = stack[top--].link;
if(sign == 1) { stack[++top].link = p; stack[top].flag = 2; p = p->rchild; }
else { printf("%c ", p->data); p = NULL; } } } }
void LevelOrder(BitTree *T) { BitTree *p, *queue[Maxsize]; int front, rear; front = rear = 0;
if(T) { queue[rear++] = T;
while(front != rear) { p = queue[front++]; printf("%c ", p->data);
if(p->lchild) queue[rear++] = p->lchild; if(p->rchild) queue[rear++] = p->rchild; } } }
|
排序
插入排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| #include <stdio.h> #include <string.h> #include <stdlib.h>
typedef int ElemType;
void InsertSort1(ElemType A[], int n) { int i,j; for(i=2; i<=n; i++) { if(A[i]<A[i-1]) { A[0] = A[i];
for(j=i-1; A[0]<A[j]; --j) A[j+1] = A[j];
A[j+1] = A[0]; } } }
void InsertSort2(ElemType A[], int n) { int i,j,low,high,mid;
for(i=2; i<=n; i++) { A[0] = A[i]; low = 1; high = i-1;
while(low<=high) { mid = (low+high)/2;
if(A[mid>A[0]]) high = mid-1; else low = mid+1; }
for(j=i-1; j>=high+1; --j) A[j+1] = A[j];
A[high+1] = A[0]; } }
void ShellSort(ElemType A[], int n) { int i,j,dk;
for(dk=n/2; dk>=1; dk=dk/2) { for(i=dk+1; i<=n; ++i) { if(A[i]<A[i-dk]) { A[0] = A[i];
for(j=i-dk; j>0&&A[0]<A[j]; j-=dk) A[j+dk] = A[j];
A[j+dk] = A[0]; } } } }
|
交换排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include <stdio.h> #include <string.h> #include <stdlib.h>
typedef int ElemType;
int Partition(ElemType A[], int low, int high) { ElemType pivot = A[low];
while(low<high) { while(low<high && A[high]>=pivot) --high; A[low] = A[high];
while(low<high && A[low]<=pivot) ++low; A[high] = A[low]; }
A[low] = pivot; return low; }
void QuickSort(ElemType A[], int low, int high) { if(low<high) { int pivotpos = Partition(A,low,high); QuickSort(A,low,pivotpos-1); QuickSort(A,pivotpos+1,high); } }
|
选择排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include <stdio.h> #include <string.h> #include <stdlib.h>
typedef int ElemType;
void AdjustMaxHeap(ElemType A[], int target, int n) { int i = target; int j = 2*i+1; int temp = A[i];
while(j<=n) { if(j<n && A[j]<A[j+1]) j++;
if(temp<A[j]) { A[i] = A[j]; i = j; j = 2*j+1; } else break; }
A[i] = temp; }
void HeadSort(ElemType A[], int n) { int i; int temp;
for(i=n/2-1; i>=0; i--) AdjustMaxHeap(A, i, n-1);
for(i=n-1; i>0; i--) { temp = A[0]; A[0] = A[i]; A[i] = temp; AdjustMaxHeap(A, 0, i-1); } }
|