单链表的新建查找 链表的实现 链表的实现需要定义结点的结构体类型,例如:
1 2 3 4 struct node { int data; struct node *next ; };
然后需要创建一个头指针,用于指向链表的第一个结点,例如:
1 struct node *head = NULL ;
接下来,可以通过动态分配内存的方式,创建新的结点,并将它们链接起来,例如:
1 2 3 4 struct node *p = (struct node *)malloc (sizeof (struct node)); p->data = 10 ; p->next = NULL ; head = p;
这样就实现了一个只有一个结点的链表。如果要添加更多的结点,可以重复上述过程,并将新结点的地址赋给前一个结点的指针域,例如:
1 2 3 4 struct node *q = (struct node *)malloc (sizeof (struct node)); q->data = 20 ; q->next = NULL ; p->next = q;
链表的删除-插入-查找 例如,如果要删除链表中的第三个结点,可以用以下代码实现:
1 2 3 4 5 6 struct node *temp = head; temp = temp->next; struct node *del = temp->next; temp->next = del->next; free (del);
例如,如果要在链表中的第三个结点后插入一个新的结点,可以用以下代码实现:
1 2 3 4 5 6 struct node *new = (struct node *)malloc (sizeof (struct node)); new->data = x; struct node *temp = head; temp = temp->next->next; new->next = temp->next; temp->next = new;
例如,如果要在链表中查找第一个值为 x 的结点,可以用以下代码实现:
1 2 3 4 5 6 7 8 9 10 11 struct node *p = head->next; int i = 1 ; while (p != NULL ) { if (p->data == x) { return i; } p = p->next; i++; } return -1 ;
实现顺序表 插入 删除 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> #define MaxSize 100 typedef int ElemType; typedef struct { ElemType data[MaxSize]; int length; }SqList; void Put_arry (SqList &L,ElemType e) { int i = L.data[1 ]; int j; for (j=L.length;j>1 ;j--){ L.data[j]=L.data[j-1 ]; } L.data[i]=e; L.length++; } void print_arr (SqList L) { int i; for (i=0 ;i<L.length;i++){ printf ("%3d " ,L.data[i]); } printf ("\n" ); } void Delet_arry (SqList &L,ElemType e) { int i; for (i=e;i<L.length;i++){ L.data[i-1 ]=L.data[i]; } L.length--; } int main () { SqList L; L.data[0 ]=1 ; L.data[1 ]=2 ; L.data[2 ]=3 ; L.length=3 ; ElemType e; scanf ("%d" ,&e); Put_arry(L,e); print_arr(L); ElemType del; scanf ("%d" ,&del); Delet_arry(L,del); print_arr(L); return 0 ; }
头插法新建链表 这段代码,实现头插法新建一个链表,每次新建一个结点,都是放到第一个结点(注意头结点不是第一个结点,头结点指向第一个结点),该链表存储int类型数据,通过scanf输入建立对应的存储结点,直到输入9999,结束创建,并且不会包含9999
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 #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct LNode { ElemType data; struct LNode *next ; }LNode,*LinkList; void list_head_insert (LNode* &L) { L= (LinkList)malloc (sizeof (LNode)); L->next=NULL ; ElemType x; LinkList s; scanf ("%d" ,&x); while (x!=9999 ){ s=(LinkList) malloc (sizeof (LNode)); s->data=x; s->next=L->next; L->next=s; scanf ("%d" ,&x); } } void print_LinKlist (LinkList L) { L=L->next; while (L!=NULL ){ printf ("%3d" ,L->data); L=L->next; } } int main () { LinkList L; list_head_insert(L); print_LinKlist(L); return 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 36 37 38 39 40 41 #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct LNode { ElemType data; struct LNode *next ; }LNode,*LinkList; void list_tail_insert (LinkList &L) { L=(LinkList) malloc (sizeof (LNode)); L->next=NULL ; ElemType x; LNode *s,*r=L; scanf ("%d" ,&x); while (x!=9999 ){ s=(LinkList) malloc (sizeof (LNode)); s->data=x; r->next=s; r=s; scanf ("%d" ,&x); } r->next=NULL ; } void print_LinKlist (LinkList L) { L=L->next; while (L!=NULL ){ printf ("%3d" ,L->data); L=L->next; } } int main () { LinkList L; list_tail_insert(L); print_LinKlist(L); return 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 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 #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct LNode { ElemType data; struct LNode *next ; }LNode,*LinkList; void list_tail_insert (LinkList &L) { L=(LinkList) malloc (sizeof (LNode)); L->next=NULL ; ElemType x; LNode *s,*r=L; scanf ("%d" ,&x); while (x!=9999 ){ s=(LinkList) malloc (sizeof (LNode)); s->data=x; r->next=s; r=s; scanf ("%d" ,&x); } r->next=NULL ; } void print_LinKlist (LinkList L) { L=L->next; while (L!=NULL ){ printf ("%3d" ,L->data); L=L->next; } } LinkList GetEle_by_pos (LinkList L,int pos) { if (pos<1 ){ return NULL ; } if (pos==0 ){ return L; } int i=0 ; while (L&&i<pos){ L=L->next; i++; } return L; } LinkList GetEle_by_value (LinkList L,int value) { while (L){ if (L->data!=value){ L=L->next; }if (L->data==value){ return L; } } return NULL ; } int main () { LinkList L; list_tail_insert(L); print_LinKlist(L); LinkList pNode1 = GetEle_by_pos(L,3 ); if (pNode1!=NULL ){ printf ("\nsearch by postion success\n" ); printf ("the value is %d\n" ,pNode1->data); } LinkList pNode2 = GetEle_by_value(L, 8 ); if (pNode2!=NULL ){ printf ("search by value success\n" ); printf ("the value is %d\n" ,pNode2->data); } return 0 ; }
链表 往第i 个位置插入元素 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 <stdlib.h> typedef int ElemType; typedef struct LNode { ElemType data; struct LNode *next ; }LNode,*LinkList; void list_tail_insert (LinkList &L) { L=(LinkList) malloc (sizeof (LNode)); L->next=NULL ; ElemType x; LNode *s,*r=L; scanf ("%d" ,&x); while (x!=9999 ){ s=(LinkList) malloc (sizeof (LNode)); s->data=x; r->next=s; r=s; scanf ("%d" ,&x); } r->next=NULL ; } void print_LinKlist (LinkList L) { L=L->next; while (L!=NULL ){ printf ("%3d" ,L->data); L=L->next; } } LinkList GetEle_by_pos (LinkList L,int pos) { if (pos<1 ){ return NULL ; } if (pos==0 ){ return L; } int i=0 ; while (i<pos){ L=L->next; i++; } return L; } bool ListFrontInsert (LinkList L,int i,int insert) { LinkList p = GetEle_by_pos(L, i-1 ); if (p==NULL ){ return false ; } LinkList q; q = (LinkList) malloc (sizeof (LNode)); q->next=p->next; q->data=insert; p->next=q; return true ; } int main () { LinkList L; list_tail_insert(L); print_LinKlist(L); printf ("\n" ); ListFrontInsert(L,2 ,80 ); print_LinKlist(L); return 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 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 #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct LNode { ElemType data; struct LNode *next ; }LNode,*LinkList; void list_tail_insert (LinkList &L) { L=(LinkList) malloc (sizeof (LNode)); L->next=NULL ; ElemType x; LNode *s,*r=L; scanf ("%d" ,&x); while (x!=9999 ){ s=(LinkList) malloc (sizeof (LNode)); s->data=x; r->next=s; r=s; scanf ("%d" ,&x); } r->next=NULL ; } LinkList GetEle_by_pos (LinkList L,int pos) { if (pos<1 ){ return NULL ; } if (pos==0 ){ return L; } int i=0 ; while (i<pos){ L=L->next; i++; } return L; } void print_LinKlist (LinkList L) { L=L->next; while (L!=NULL ){ printf ("%3d" ,L->data); L=L->next; } printf ("\n" ); } bool delet_list (LinkList L,int pos) { if (pos>1 ){ LinkList p = GetEle_by_pos(L,pos - 1 ); LinkList q=p->next; if (q!=NULL ){ p->next=q->next; free (q); return true ; } } return false ; } int main () { LinkList L; list_tail_insert(L); print_LinKlist(L); delet_list(L,2 ); print_LinKlist(L); return 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 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 #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct LNode { ElemType data; struct LNode *next ; }LNode,*LinkList; void list_tail_insert (LinkList &L) { L=(LinkList) malloc (sizeof (LNode)); L->next=NULL ; ElemType x; LNode *s,*r=L; scanf ("%d" ,&x); while (x!=9999 ){ s=(LinkList) malloc (sizeof (LNode)); s->data=x; r->next=s; r=s; scanf ("%d" ,&x); } r->next=NULL ; } void print_LinKlist (LinkList L) { L=L->next; while (L!=NULL ){ printf ("%3d" ,L->data); L=L->next; } printf ("\n" ); } void find_middle (LinkList L,LinkList &L2) { L2=(LinkList) malloc (sizeof (LNode)); LinkList p,pp; p=pp=L->next; while (pp){ pp=pp->next; if (pp==NULL ){ break ; } pp=pp->next; if (pp==NULL ){ break ; } p=p->next; } L2->next=p->next; p->next=NULL ; } void reverse (LinkList L2) { LinkList r,s,t; r=L2->next; if (r==NULL ){ return ; } s=r->next; if (s==NULL ){ return ; } t=s->next; while (t){ s->next=r; r=s; s=t; t=t->next; } s->next=r; L2->next->next=NULL ; L2->next=s; } void merge (LinkList L,LinkList L2) { LinkList pcur,p,q; pcur=L->next; p=pcur->next; q=L2->next; while (p!=NULL &&q!=NULL ){ pcur->next=q; q=q->next; pcur=pcur->next; pcur->next=p; p=p->next; pcur=pcur->next; } } int main () { LinkList L; LinkList L2; list_tail_insert(L); print_LinKlist(L); find_middle(L,L2); print_LinKlist(L); print_LinKlist(L2); reverse(L2); print_LinKlist(L2); merge(L,L2); free (L2); print_LinKlist(L); return 0 ; }
OJ测试 作业说明
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 #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct LNode { ElemType data; struct LNode * next ; }LNOde,*Linklist; void list_tail_insert (Linklist &L) { L=(Linklist) malloc (sizeof (LNode)); L->next=NULL ; Linklist s,r=L; int x; scanf ("%d" ,&x); while (x!=9999 ){ s=(Linklist) malloc (sizeof (LNode)); s->data=x; r->next=s; r=s; scanf ("%d" ,&x); } s->next=NULL ; } Linklist getEle_by_pos (Linklist L,int pos) { if (pos==0 ){ return L; } if (pos<1 ){ return NULL ; } int i=0 ; while (L&&i<pos){ L=L->next; i++; } return L; } void list_insert (Linklist L,int i,int insert) { Linklist p = getEle_by_pos(L,i - 1 ); if (p==NULL ){ return ; } Linklist s; s=(Linklist) malloc (sizeof (LNode)); s->next=p->next; s->data=insert; p->next=s; } void print_list (Linklist L) { while (L){ L=L->next; if (L==NULL ){ break ; } printf ("%3d" ,L->data); } printf ("\n" ); } int main () { Linklist L; list_tail_insert(L); print_list(L); list_insert(L,2 ,99 ); print_list(L); return 0 ; }
栈与队列 栈原理解析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 typrdef struct { Elemtype data[50 ]; int top; }SqStack; S.top=-1 ; S.data[++S.top]
初始化栈-入栈-出栈 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 #include <stdio.h> #define Maxsize 50 typedef int ElemType; typedef struct { ElemType data[Maxsize]; int top; }SqStack; void innit (SqStack &S) { S.top=-1 ; } bool Push (SqStack &S,ElemType e) { if (S.top==Maxsize-1 ){ return false ; } S.data[++S.top]=e; return true ;} void print_satck (SqStack S) { for (int i=0 ;i<S.top+1 ;i++){ printf ("%3d" ,S.data[i]); } printf ("\n" ); } bool isEmpty (SqStack S) { if (S.top==-1 ){ return true ; } return false ; } bool Pop (SqStack &S,ElemType &e) { if (isEmpty(S)){ return false ; } e=S.data[S.top--]; return true ; } int main () { SqStack S; innit(S); Push(S,79 ); Push(S,2 ); Push(S,3 ); print_satck(S); ElemType e; bool flag; flag=Pop(S,e); if (flag){ print_satck(S); } return 0 ; }
队列循环队列原理解析 队列
1 2 3 队列实现有两种常见方式:数组、链表 特点:允许头部删除,尾部增加
循环队列
链式队列
循环队列数组实现 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 #include <stdio.h> #define Maxsize 5 typedef int ElemType; typedef struct { ElemType data[Maxsize]; int front,rear; }SqQueue; void Innit (SqQueue &Q) { Q.rear=Q.front=0 ; } bool EnQueue (SqQueue &Q,ElemType e) { if ((Q.rear+1 )%Maxsize==Q.front){ return false ; } Q.data[Q.rear]=e; Q.rear=(Q.rear+1 )%Maxsize; return true ; } bool OutQueue (SqQueue &Q,ElemType &x) { if (Q.rear==Q.front){ return false ; } x=Q.data[Q.front]; Q.front=(Q.front+1 )%Maxsize; return true ;} void print_queue (SqQueue Q) { int i; for (i=Q.front;i<Q.rear;i++){ printf ("%3d" ,Q.data[i]); } printf ("\n" ); } int main () { SqQueue Q; bool flag; ElemType e; Innit(Q); EnQueue(Q,29 ); EnQueue(Q,39 ); EnQueue(Q,9 ); print_queue(Q); flag=OutQueue(Q,e); if (flag){ printf ("Pop success ,the pop value is %d\n" ,e); } print_queue(Q); return 0 ; }
队列链表实现-1 这是 带有头结点 的方式实现
1 2 3 4 5 一般队列的头结点是否有数据取决于队列的实现方式: - 如果队列是用数组实现的,那么头结点就是数组的第一个元素,它有数据; - 如果队列是用链表实现的,那么头结点可以是一个空结点,也可以是链表的第一个元素,这取决于是否使用带头结点的链表。如果使用带头结点的链表,那么头结点没有数据,只是一个指针;如果使用不带头结点的链表,那么头结点就是链表的第一个元素,它有数据。
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 <stdlib.h> typedef int Elemype; typedef struct LinkNode { Elemype data; struct LinkNode * next ; }LinkNode,*LinkList; typedef struct { LinkList front,rear; }LinkQueue; void innit (LinkQueue &Q) { Q.rear=Q.front=(LinkList) malloc (sizeof (LinkNode)); Q.rear->next =NULL ; } bool EnQueue (LinkQueue &Q,Elemype x) { LinkList s; s=(LinkList) malloc (sizeof (LinkNode)); s->data=x; Q.rear->next=s; Q.rear=s; s->next=NULL ; return true ;} bool OutQueue (LinkQueue &Q) { if (Q.front->next==NULL ){ return false ; } LinkList s=Q.front->next; Q.front->next=s->next; if (Q.rear==s){ Q.rear=Q.front; } free (s); return true ;} int main () { LinkQueue Q; innit(Q); EnQueue(Q,20 ); EnQueue(Q,20 ); EnQueue(Q,20 ); EnQueue(Q,20 ); bool ret; ret= OutQueue(Q); if (ret){ printf ("Pop success" ); }else { printf ("Pop fail" ); } return 0 ; }
队列链表的实现-2 带头结点的队列,队列的头指针和尾指针都指向一个空的头结点,头结点的next指针指向第一个元素,尾结点的next指针为NULL。 不带头结点的队列,队列的头指针指向第一个元素,尾指针指向最后一个元素,尾结点的 不带头结点
不带头结点的链表
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 #include <stdio.h> #include <stdlib.h> typedef int ElemType;typedef bool Status;typedef struct LinkNode { ElemType data; struct LinkNode * next ; }LinkNode; typedef struct { LinkNode* front, * rear; }LinkQueue; void InitQueue (LinkQueue& Q) { Q.front = NULL ; Q.rear = NULL ; } Status IsEmpty (LinkQueue Q) { if (Q.front == NULL ) return true ; else return false ; } Status EnQueue (LinkQueue& Q, ElemType x) { LinkNode* s = (LinkNode*)malloc (sizeof (LinkNode)); if (s == NULL ) return false ; s->data = x; s->next = NULL ; if (Q.front == NULL ) { Q.front = s; Q.rear = s; } else { Q.rear->next = s; Q.rear = s; } return true ; } Status DeQueue (LinkQueue& Q, ElemType& x) { if (Q.front == NULL ) return false ; LinkNode* p = Q.front; x = p->data; Q.front = p->next; if (Q.rear == p) { Q.front = NULL ; Q.rear = NULL ; } free (p); return true ; } int main () { LinkQueue Q; ElemType x = -1 ; InitQueue(Q); EnQueue(Q, 3 ); EnQueue(Q, 6 ); EnQueue(Q, 8 ); EnQueue(Q, 9 ); EnQueue(Q, 7 ); EnQueue(Q, 5 ); for (int i = 1 ; i < 6 ; i++) { DeQueue(Q, x);
OJ测试
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 #include <stdio.h> #include <stdlib.h> #define MaxSize 5 typedef int ElemType; typedef struct { ElemType data[MaxSize]; int top; }SqStack; typedef struct { ElemType data[MaxSize]; int front,rear; }SqQueue; void initStack (SqStack &S) { S.top=-1 ; } bool Push (SqStack &S,ElemType e) { if (S.top==MaxSize-1 ){ return false ; } S.data[++S.top]=e; return true ;} bool Pop (SqStack &S,ElemType &e) { if (S.top==-1 ){ return false ; } e=S.data[S.top--]; return true ;} void print_stack (SqStack S) { int i; for (i=0 ;i<S.top+1 ;i++){ printf ("%3d" ,S.data[i]); } printf ("\n" ); } void initQueue (SqQueue &Q) { Q.front=Q.rear=0 ; } bool EnQueue (SqQueue &Q,ElemType e) { if ((Q.rear+1 )%MaxSize==Q.front){ return false ; } Q.data[Q.rear]=e; Q.rear=(Q.rear+1 )%MaxSize; return true ;} bool OutQueue (SqQueue &Q,ElemType &e) { if (Q.rear==Q.front){ return false ; } e=Q.data[Q.front]; Q.front=(Q.front+1 )%MaxSize; return true ;} void print_queue (SqQueue Q) { int i; for (i=Q.front;i<Q.rear;i++){ printf ("%3d" ,Q.data[i]); } } int main () { SqStack S; initStack(S); int i,num; for (i=0 ;i<3 ;i++){ scanf ("%d" ,&num); Push(S,num); } print_stack(S); ElemType m; for (i=0 ;i<3 ;i++){ Pop(S,m); printf ("the ride num is %d\n" ,m); } SqQueue Q; ElemType e; initQueue(Q); int i2,num2; bool flag; for (i2=0 ;i2<5 ;i2++){ scanf ("%d" ,&num2); flag=EnQueue(Q,num2); if (!flag){ printf ("flase Queue is full\n" ); } } for (i2=0 ;i2<4 ;i2++){ OutQueue(Q,e); printf ("%3d" ,e); } return 0 ; }
二叉树的建树和遍历 树与二叉树原理解析 树原理解析
二叉树原理 1 2 3 1、满二叉树:每一层都放满了 2、完全二叉树:除了最后一层,前面层数全部放满,最后一层从左往右,只能是右侧有空
二叉树的层次建树
1 2 3 4 上面层次建树的实现通过辅助队列实现 每加一个元素,辅助队列就往队尾加一个元素 而pcur指针判断结点的左右两侧都放满了时,才往后移动 这样就实现了层次建树
1 2 3 4 5 6 7 8 9 10 以下代码实现了,输入abcdefgj一串连续字符,然后按层次建树存储 这里的辅助队列有四个指针:front-rear-listpnew-pcur 起初都赋值为NULL,就是没有头结点得方式创建队列 front指向第一个结点 rear指向末尾结点 listpnew指向新加元素 pcur指向当前结点 这里的二叉树有一个指针:pnew指向树的最新结点
头文件 function.h 代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <stdio.h> #include <stdlib.h> typedef char ElenType; typedef struct TNode { ElenType c; struct TNode *lift ,*right ; }BiTNode,*BiTree; typedef struct tag { BiTree p; struct tag *pnext ; }tag,*ptag;
主文件 main.cpp 代码如下
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 #include "function.h" int main () { BiTree pnew; BiTree root=NULL ; char c; ptag front=NULL ,rear=NULL ,listpnew=NULL ,pcur=NULL ; while (scanf ("%c" ,&c)){ if (c=='\n' ){ break ; } pnew=(BiTree) calloc (1 ,sizeof (BiTNode)); pnew->c=c; listpnew= (ptag)calloc (1 ,sizeof (tag)); listpnew->p=pnew; if (root==NULL ){ root=pnew; rear=front=listpnew; pcur=listpnew; }else { rear->pnext=listpnew; rear=listpnew; if (pcur->p->lift==NULL ){ pcur->p->lift=pnew; }else if (pcur->p->right==NULL ){ pcur->p->right=pnew; pcur=pcur->pnext; } } } return 0 ; }
二叉树的-前中后-序遍历 递归思想
1前序遍历:PreOrder
前序遍历也叫深度优先遍历
2中序遍历:InOrder
3后序遍历:PostOrder
function.h代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <stdio.h> #include <stdlib.h> typedef char ElenType; typedef struct TNode { ElenType c; struct TNode *lift ,*right ; }BiTNode,*BiTree; typedef struct tag { BiTree p; struct tag *pnext ; }tag,*ptag;
main.cpp代码如下
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 #include "function.h" void PreOrder (BiTree p) { if (p!=NULL ) { printf ("%c" , p->c); PreOrder(p->lift); PreOrder(p->right); } } void InOrder (BiTree p) { if (p!=NULL ) { InOrder(p->lift); printf ("%c" , p->c); InOrder(p->right); } } void PostOrder (BiTree p) { if (p!=NULL ){ PostOrder(p->lift); PostOrder(p->right); printf ("%c" ,p->c); } } int main () { BiTree pnew; BiTree root=NULL ; char c; ptag front=NULL ,rear=NULL ,listpnew=NULL ,pcur; while (scanf ("%c" ,&c)){ if (c=='\n' ){ break ; } pnew=(BiTree) calloc (1 ,sizeof (BiTNode)); pnew->c=c; listpnew= (ptag)calloc (1 ,sizeof (tag)); listpnew->p=pnew; if (root==NULL ){ root=pnew; rear=front=listpnew; pcur=listpnew; }else { rear->pnext=listpnew; rear=listpnew; if (pcur->p->lift==NULL ){ pcur->p->lift=pnew; }else if (pcur->p->right==NULL ){ pcur->p->right=pnew; pcur=pcur->pnext; } } } printf ("=================\n" ); PreOrder(root); printf ("\n" ); InOrder(root); printf ("\n" ); PreOrder(root); return 0 ; }
二叉树的层次遍历 也称广度优先遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [二叉树的广度优先遍历是一种按层次遍历二叉树的方法,它的顺序是从上到下,从左到右,依次访问每一层的节点][。广度优先遍历的原则就是对每一层的节点依次访问,一层访问结束后,进入下一层,直到最后一个节点,每个节点都只访问一次] [二叉树的广度优先遍历的实现需要借助队列(Queue)这种数据结构,它的特点是先进先出。首先将根节点入队,然后循环执行以下操作,直到队列为空:从队列中取出一个节点,访问它,然后将它的左右子节点(如果有的话)入队]。 例如,对于下图的二叉树,广度优先遍历的结果是 {1,2,3,4,5,6,7,8,9,10},遍历过程如下: 1. 将根节点1入队,队列为【1】 2. 取出节点1,访问它,将它的左右子节点2和3入队,队列为【2,3】 3. 取出节点2,访问它,将它的左右子节点4和5入队,队列为【3,4,5】 4. 取出节点3,访问它,将它的右子节点6入队,队列为【4,5,6】 5. 取出节点4,访问它,将它的左右子节点8和9入队,队列为【5,6,8,9】 6. 取出节点5,访问它,将它的右子节点10入队,队列为【6,8,9,10】 7. 取出节点6,访问它,没有子节点,队列为【8,9,10】 8. 取出节点8,访问它,没有子节点,队列为【9,10】 9. 取出节点9,访问它,没有子节点,队列为【10】 10. 取出节点10,访问它,没有子节点,队列为空,遍历结束
代码采用3给文件
function.h代码如下
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 #ifndef INC_1_TREE_FUNCTION_H #define INC_1_TREE_FUNCTION_H #include <stdio.h> #include <stdlib.h> typedef char BiElemType; typedef struct BiTNode { BiElemType c; struct BiTNode *lift ; struct BiTNode *right ; }BiTNode,*BiTree; typedef struct tag { BiTree p; struct tag *pnext ; }tag,*ptag; typedef BiTree ElemType; typedef struct LinkNode { ElemType data; struct LinkNode * next ; }LinkNode,*LinkList; typedef struct { LinkList front,rear; }LinkQueue; void innit (LinkQueue &Q) ; bool EnQueue (LinkQueue &Q,ElemType x) ; bool OutQueue (LinkQueue &Q,ElemType &x) ; bool IsEmpty (LinkQueue Q) ; #endif
Queue.cpp代码如下
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 #include "function.h" void innit (LinkQueue &Q) { Q.rear=Q.front=(LinkList) malloc (sizeof (LinkNode)); Q.rear->next=NULL ; } bool EnQueue (LinkQueue &Q,ElemType x) { LinkList s; s=(LinkList) malloc (sizeof (LinkNode)); s->data=x; Q.rear->next=s; Q.rear=s; s->next=NULL ; return true ;} bool OutQueue (LinkQueue &Q,ElemType &x) { if (Q.front->next==NULL ){ return false ; } LinkList s=Q.front->next; x=s->data; Q.front->next=s->next; if (Q.rear==s){ Q.rear=Q.front; } free (s); return true ;} bool IsEmpty (LinkQueue Q) { if (Q.rear==Q.front){ return true ; } return false ; }
main.cpp代码如下
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 #include "function.h" void PreOrder (BiTree p) { if (p!=NULL ) { printf ("%c" , p->c); PreOrder(p->lift); PreOrder(p->right); } } void InOrder (BiTree p) { if (p!=NULL ) { InOrder(p->lift); printf ("%c" , p->c); InOrder(p->right); } } void PostOrder (BiTree p) { if (p!=NULL ){ PostOrder(p->lift); PostOrder(p->right); printf ("%c" ,p->c); } } void LevelOrder (BiTree T) { LinkQueue Q; innit(Q); BiTree p; EnQueue(Q,T); while (!IsEmpty(Q)){ OutQueue(Q,p); putchar (p->c); if (p->lift!=NULL ){ EnQueue(Q,p->lift); } if (p->right!=NULL ){ EnQueue(Q,p->right); } } } int main () { BiTree pnew; BiTree root=NULL ; char c; ptag front=NULL ,rear=NULL ,listpnew=NULL ,pcur; while (scanf ("%c" ,&c)){ if (c=='\n' ){ break ; } pnew=(BiTree) calloc (1 ,sizeof (BiTNode)); pnew->c=c; listpnew= (ptag)calloc (1 ,sizeof (tag)); listpnew->p=pnew; if (root==NULL ){ root=pnew; rear=front=listpnew; pcur=listpnew; }else { rear->pnext=listpnew; rear=listpnew; if (pcur->p->lift==NULL ){ pcur->p->lift=pnew; }else if (pcur->p->right==NULL ){ pcur->p->right=pnew; pcur=pcur->pnext; } } } printf ("=================\n" ); PreOrder(root); printf ("\n" ); InOrder(root); printf ("\n" ); PreOrder(root); printf ("\n" ); LevelOrder(root); return 0 ; }
真题-求带权路径长度
1 由于与深度有关,可采用深度优先遍历方式(可以获取深度),最后返回wpl
function.h代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <stdio.h> #include <stdlib.h> typedef char ElenType; typedef struct TNode { ElenType c; struct TNode *lift ,*right ; }BiTNode,*BiTree; typedef struct tag { BiTree p; struct tag *pnext ; }tag,*ptag;
main.cpp代码如下
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 #include "function.h" int PreOrder (BiTree p,int deep) { static int wpl=0 ; if (p!=NULL ) { if (p->lift==NULL &&p->right==NULL ){ wpl+=p->c*deep; } printf ("ele%c---deep%d\n" , p->c,deep); PreOrder(p->lift,deep+1 ); PreOrder(p->right,deep+1 ); } return wpl; } int main () { BiTree pnew; BiTree root=NULL ; char c; ptag front=NULL ,rear=NULL ,listpnew=NULL ,pcur; while (scanf ("%c" ,&c)){ if (c=='\n' ){ break ; } pnew=(BiTree) calloc (1 ,sizeof (BiTNode)); pnew->c=c; listpnew= (ptag)calloc (1 ,sizeof (tag)); listpnew->p=pnew; if (root==NULL ){ root=pnew; rear=front=listpnew; pcur=listpnew; }else { rear->pnext=listpnew; rear=listpnew; if (pcur->p->lift==NULL ){ pcur->p->lift=pnew; }else if (pcur->p->right==NULL ){ pcur->p->right=pnew; pcur=pcur->pnext; } } } printf ("=================\n" ); printf ("wpl=%d" ,PreOrder(root,0 )); return 0 ; }
OJ测试
代码实现
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 #include <stdio.h> #include <stdlib.h> typedef char ElemType; typedef struct TNode { ElemType data; struct TNode *lift ; struct TNode *right ; }TNode,*BiTree; typedef struct QNode { BiTree p; struct QNode *next ; }QNode,*Queue; void PreOreder (BiTree T) { if (T!=NULL ){ printf ("%c" ,T->data); PreOreder(T->lift); PreOreder(T->right); } } int main () { ElemType c; BiTree root=NULL ; BiTree pnew; Queue front=NULL ,rear=NULL ,listpnew=NULL ,pcur=NULL ; while (scanf ("%c" ,&c)){ if (c=='\n' ){ break ; } pnew=(BiTree) calloc (1 ,sizeof (TNode)); pnew->data=c; listpnew=(Queue) calloc (1 , sizeof (QNode)); listpnew->p=pnew; if (root==NULL ){ root=pnew; front=rear=pcur=listpnew; }else { rear->next=listpnew; rear=listpnew; if (pcur->p->lift==NULL ){ pcur->p->lift=pnew; }else if (pcur->p->right==NULL ){ pcur->p->right=pnew; pcur=pcur->next; } } } PreOreder(root); return 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 #include <stdlib.h> #include <stdio.h> #include <time.h> typedef int ELemType; typedef struct { ELemType* elem; int TableLen; }SSTable; void st_init (SSTable &ST,int len) { ST.TableLen=len+1 ; ST.elem= (ELemType*)malloc (sizeof (ELemType)*ST.TableLen); int i; srand(time(NULL )); for (i=1 ;i<ST.TableLen;i++){ ST.elem[i]=rand() % 100 ; } } void st_print (SSTable ST) { int i; for (i=1 ;i<ST.TableLen;i++){ printf ("%3d" ,ST.elem[i]); } printf ("\n" ); } int Search_Seq (SSTable ST,ELemType e) { int i; for (i=ST.TableLen-1 ;ST.elem[i]!=e;--i){ } return i; } int main () { SSTable ST; st_init(ST,9 ); st_print(ST); ELemType key; int pos; printf ("please input search by\n" ); scanf ("%d" ,&key); pos=Search_Seq(ST,key); printf ("the postion is %d" ,pos); return 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 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 #include <stdio.h> #include <stdlib.h> #include <time.h> typedef int ELemType; typedef struct { ELemType* elem; int Tablelen; }SSTable; void st_init (SSTable &ST,int len) { ST.Tablelen=len; ST.elem=(ELemType*) malloc (sizeof (ELemType)*ST.Tablelen); int i; srand(time(NULL )); for (i=0 ;i<ST.Tablelen;i++){ ST.elem[i]=rand() % 100 ; } } void st_print (SSTable ST) { int i; for (i=0 ;i<ST.Tablelen;i++){ printf ("%3d" ,ST.elem[i]); } printf ("\n" ); } int binary_search (SSTable ST,int key) { int begin=0 ; int mid; int end=ST.Tablelen-1 ; while (begin<=end){ mid=(begin+end)/2 ; if (key<ST.elem[mid]){ end=mid-1 ; }else if (key>ST.elem[mid]){ begin=mid+1 ; }else { return mid; } } return -1 ; } int compare (const void *left,const void *right) { return *(int *)left-*(int *)right; } int main () { SSTable ST; int key; st_init(ST,10 ); st_print(ST); qsort(ST.elem,ST.Tablelen,sizeof (ELemType),compare); st_print(ST); int pos; printf ("please input the number that you want to search\n" ); scanf ("%d" ,&key); pos=binary_search(ST,key); if (pos==-1 ){ printf ("false,the number is not exit" ); }else { printf ("the pos is %3d" ,pos); } return 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 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 #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct BSNode { ElemType key; struct BSNode *lift ; struct BSNode *right ; }BSNode,*BiTree; void BST_insert (BiTree &T,ElemType k) { if (T==NULL ){ T= (BiTree)calloc (1 ,sizeof (BSNode)); T->key=k; } BiTree p=T; BiTree parent; while (p){ parent=p; if (k==p->key){ return ; } else if (k<p->key){ p=p->lift; } else { p=p->right; } } BiTree pnew; pnew=(BiTree) calloc (1 ,sizeof (BSNode)); pnew->key=k; if (k<parent->key){ parent->lift=pnew; }else { parent->right=pnew; } } void creat_bstree (BiTree &T,ElemType str[],int n) { T=NULL ; int i; for (i=0 ;i<n;i++){ BST_insert(T,str[i]); } } void InOrder (BiTree T) { if (T!=NULL ) { InOrder(T->lift); printf ("%3d" , T->key); InOrder(T->right); } } int main () { BiTree T; ElemType str[9 ]={12 ,34 ,6 ,7 ,3 ,6 ,9 ,0 ,12 }; creat_bstree(T,str,9 ); InOrder(T); return 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 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 #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct BSNode { ElemType key; struct BSNode *left ,*righr ; }BSNode,*BiTree; void BST_insert (BiTree &T,ElemType k) { if (T==NULL ){ T=(BiTree) calloc (1 ,sizeof (BSNode)); T->key=k; } BiTree p=T; BiTree parent; while (p){ parent=p; if (k==p->key){ return ; }else if (k<p->key){ p=p->left; }else { p=p->righr; } } BiTree pnew; pnew=(BiTree) calloc (1 ,sizeof (BSNode)); pnew->key=k; if (k<parent->key){ parent->left=pnew; }else { parent->righr=pnew; } } void creat_BSTree (BiTree &T,ElemType str[],int n) { T=NULL ; int i; for (i=0 ;i<n;i++){ BST_insert(T,str[i]); } } void InOrder (BiTree T) { if (T!=NULL ){ InOrder(T->left); printf ("%3d" ,T->key); InOrder(T->righr); } } BiTree BS_Search (BiTree T,ElemType key) { while (T!=NULL &&key!=T->key){ if (key<T->key){ T=T->left; }else { T=T->righr; } } return T; } void delete_BSNode (BiTree &root,ElemType x) { if (root==NULL ){ return ; } if (x<root->key){ delete_BSNode(root->left,x); }else if (x>root->key){ delete_BSNode(root->righr,x); }else { if (root->left==NULL ){ BiTree tempNode=root; root=root->righr; free (tempNode); }else if (root->righr==NULL ){ BiTree tempNode=root; root=root->left; free (tempNode); }else { BiTree p=root->left; while (p->righr!=NULL ){ p=p->righr; } root->key=p->key; delete_BSNode(root->left,p->key); } } } int main () { BiTree T; ElemType str[8 ]={13 ,4 ,56 ,7 ,23 ,7 ,95 ,48 }; ElemType key; creat_BSTree(T,str,8 ); InOrder(T); ElemType x; printf ("\ndelet 23\n" ); delete_BSNode(T,23 ); InOrder(T); return 0 ; }
OJ测试
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 #include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct BSNode { ElemType key; struct BSNode *lift ,*right ; }BSNode,*Bitree; void BST_insert (Bitree &T,ElemType k) { if (T==NULL ){ T=(Bitree) calloc (1 ,sizeof (BSNode)); T->key=k; } Bitree p; p=T; Bitree parent; while (p){ parent=p; if (k==p->key){ return ; }else if (k<p->key){ p=p->lift; }else { p=p->right; } } Bitree pnew; pnew=(Bitree) calloc (1 ,sizeof (BSNode)); pnew->key=k; if (k<parent->key){ parent->lift=pnew; }else { parent->right=pnew; } } void creat_bstree (Bitree &T,ElemType *str,int n) { T=NULL ; int i; for (i=0 ;i<n;i++){ BST_insert(T,str[i]); } } ElemType* InOrder (Bitree T) { static ElemType str2[10 ]; static int i=0 ; if (T!=NULL ) { InOrder(T->lift); printf ("%3d" , T->key); str2[i]=T->key; i++; InOrder(T->right); } return str2;} int binary_search (ElemType str[],int x) { int begin=0 ,mid,end=10 ; while (begin<=end){ mid=(begin+end)/2 ; if (x==str[mid]){ return mid; } else if (x>str[mid]){ begin=mid; }else { end=mid; } } return -1 ; } int main () { Bitree root; ElemType str[10 ]; int k; int i; for (i=0 ;i<10 ;i++){ scanf ("%d" ,&k); str[i]=k; } creat_bstree(root,str,10 ); ElemType *pInt = InOrder(root); int pos; pos=binary_search(pInt,6 ); printf ("\n%d" ,pos); return 0 ; }
运行结果
排序算法 排序算法分类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 排序算法分为 交换类排序、插入类排序、选择类排序、归并类排序 交换类排序 冒泡排序 初冒泡排序,一般靠选择题,考大题几率小 快速排序 更重要,考大题 插入类排序 直接插入 折半插入 希尔排序,以上三种插入算法,一般考选择题,考大题概率低 选择排序 简单选择排序 堆排序(重要) 很有可能考大题
冒泡排序原理及实战
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 #include <stdio.h> #include <stdlib.h> #include <time.h> typedef int ElemType; typedef struct { ElemType *str; int len; }SSTable; void init_table (SSTable &SST,int len) { srand(time(NULL )); SST.len=len; SST.str=(ElemType*)malloc (sizeof (ElemType)*SST.len); int i; for (i=0 ;i<SST.len;i++){ SST.str[i]=rand()%100 ; } } void print_table (SSTable SST) { int i; for (i=0 ;i<SST.len;i++){ printf ("%3d" ,SST.str[i]); } } void swap (ElemType &a,ElemType &b) { ElemType temp; temp=a; a=b; b=temp; } void bubble_sort (ElemType arr[],int n) { int i,j; bool flag; for (i=0 ;i<n-1 ;i++){ flag=false ; for (j=n-1 ;j>i;j--){ if (arr[j]<arr[j-1 ]){ swap(arr[j],arr[j-1 ]); flag=true ; } } if (flag==false ){ return ; } } } int main () { SSTable T; int len=9 ; init_table(T,len); print_table(T); printf ("\n" ); bubble_sort(T.str,T.len); print_table(T); return 0 ; }
快速排序原理及实战
1 2 3 4 5 6 7 8 一个顺序表 以第一个数为基准 定义两个指针i j i从左往右找比3大的数,i停止 j从右往左找比3小的数,j停止 然后i j 对应的数完成一次交换,i j 继续前进 循环..直到ij相遇
代码实战
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 #include <stdio.h> #include <stdlib.h> #include <time.h> typedef int ElemType; typedef struct { ElemType *str; int len; }SSTable; void init_table (SSTable &SST,int len) { srand(time(NULL )); SST.len=len; SST.str=(ElemType*)malloc (sizeof (ElemType)*SST.len); int i; for (i=0 ;i<SST.len;i++){ SST.str[i]=rand()%100 ; } } void print_table (SSTable SST) { int i; for (i=0 ;i<SST.len;i++){ printf ("%3d" ,SST.str[i]); } } int partition (ElemType *str,int low,int high) { int temp; temp=str[low]; while (low<high){ while (low<high&&str[high]>=temp){ high--; } str[low]=str[high]; while (low<high&&str[low]<=temp){ low++; } str[high]=str[low]; } str[low]=temp; return low; } void quick_sort (ElemType *str,int low,int high) { if (low<high) { int postion = partition(str, low, high); quick_sort(str, low, postion - 1 ); quick_sort(str, postion + 1 , high); } } int main () { SSTable T; int len=9 ; init_table(T,len); print_table(T); printf ("\n" ); quick_sort(T.str,0 ,T.len-1 ); print_table(T); return 0 ; }
插入排序
1 2 3 4 5 6 7 8 9 10 11 12 void insert_sort (ElemType *str,int n) { int i,j,temp; for (i=1 ;i<n;i++){ temp=str[i]; for (j=i-1 ;j>=0 &&str[j]>temp;j--){ str[j+1 ]=str[j]; } str[j+1 ]=temp; } }
OJ测试
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 #include <stdio.h> #include <stdlib.h> #include <time.h> typedef int ElemType; typedef struct { ElemType *str; int len; }SSTable; void init_table (SSTable &T) { int i; int x; T.str=(ElemType*) malloc (sizeof (ElemType)*T.len); for (i=0 ;i<T.len;i++){ scanf ("%d" ,&x); T.str[i]=x; } } void print_table (SSTable SST) { int i; for (i=0 ;i<SST.len;i++){ printf ("%3d" ,SST.str[i]); } printf ("\n" ); } void swap (int &a,int &b) { int temp; temp=a; a=b; b=temp; } void bubble_sort (SSTable &T) { int i,j; for (i=0 ;i<T.len-1 ;i++){ for (j=T.len-1 ;j>i;j--){ if (T.str[j]>T.str[j-1 ]){ swap(T.str[j],T.str[j-1 ]); } } } } int split (SSTable &T,int low,int high) { int temp; temp=T.str[low]; while (low<high){ while (low<high&&T.str[high]>=temp){ high--; } T.str[low]=T.str[high]; while (low<high&&T.str[low]<=temp){ low++; } T.str[high]=T.str[low]; } T.str[low]=temp; return low; } void quick_sort (SSTable &T,int low,int high) { if (low<high){ int pos; pos= split(T,low,high); quick_sort(T,low,pos-1 ); quick_sort(T,pos+1 ,high); } } void insert_sort (SSTable &T) { int i,j,temp; for (i=1 ;i<T.len;i++){ temp=T.str[i]; for (j=i-1 ;j>=0 &&T.str[j]>temp;j--){ T.str[j+1 ]=T.str[j]; } T.str[j+1 ]=temp; } } int main () { SSTable SST; SST.len=10 ; init_table(SST); print_table(SST); insert_sort(SST); print_table(SST); return 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include <stdio.h> #include <stdlib.h> #include <time.h> typedef int ElemType; typedef struct { ElemType *str; int len; }SSTable; void init_table (SSTable &SST,int len) { srand(time(NULL )); SST.len=len; SST.str=(ElemType*)malloc (sizeof (ElemType)*SST.len); int i; for (i=0 ;i<SST.len;i++){ SST.str[i]=rand()%100 ; } } void print_table (SSTable SST) { int i; for (i=0 ;i<SST.len;i++){ printf ("%3d" ,SST.str[i]); } printf ("\n" ); } void swap (ElemType &a,ElemType &b) { ElemType temp; temp=a; a=b; b=temp; } void select_sort (ElemType *A,int n) { int min,i,j; for (i=0 ;i<n-1 ;i++){ min=i; for (j=min+1 ;j<n;j++){ if (A[j]<A[min]){ min=j; } } swap(A[i],A[min]); } } int main () { SSTable T; init_table(T,10 ); print_table(T); select_sort(T.str,10 ); print_table(T); return 0 ; }
堆排序原理及实战 1 2 实际上就是一个数组,并不是真正意义的树 就是把数组,想象成一个树
1 若父结点的值恒大于等于子结点的值,则该堆称为最大堆(max heap)。堆中最顶端的那个结点称为根结点(root node),根结点本身没有父结点(parent node)。平时在工作中,我们将最小堆称为小根堆或小顶堆,把最大堆称为大根堆或大顶堆
1 2 3 4 堆排序步骤: 堆排序的步骤是首先把堆调整为大根堆,然后我们交换根部元素也就是A[0],和最后一个元素,这 样最大的元素就放到了数组最后,接着我们将剩余 9 个元素继续调整为大根堆,然后交换 A[0]和 9 个元素的最后一个,循环往复,直到有序
代码实现
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 #include <stdio.h> #include <stdlib.h> #include <time.h> typedef int ElemType; typedef struct { ElemType *str; int len; }SSTable; void init_table (SSTable &SST,int len) { srand(time(NULL )); SST.len=len; SST.str=(ElemType*)malloc (sizeof (ElemType)*SST.len); int i; for (i=0 ;i<SST.len;i++){ SST.str[i]=rand()%100 ; } } void print_table (SSTable SST) { int i; for (i=0 ;i<SST.len;i++){ printf ("%3d" ,SST.str[i]); } printf ("\n" ); } void swap (ElemType &a,ElemType &b) { ElemType temp; temp=a; a=b; b=temp; } void adjust_down1 (ElemType A[],int k,int len) { int dad=k; int son=2 *dad+1 ; while (son<len){ if (son+1 <len&&A[son]<A[son+1 ]){ son++; } if (A[dad]<A[son]){ swap(A[son],A[dad]); dad=son; son=2 *dad+1 ; } else { break ; } } } void heap_sort (ElemType *A,int len) { int i; for (i=len/2 -1 ;i>=0 ;i--){ adjust_down1(A,i,len); } swap(A[0 ],A[len-1 ]); for (i=len-1 ;i>1 ;i--){ adjust_down1(A,0 ,i); swap(A[0 ],A[i-1 ]); } } int main () { SSTable T; init_table(T,10 ); print_table(T); heap_sort(T.str,10 ); print_table(T); return 0 ; }
归并排序原理及实战 1 2 归并排序是我们不断进行二分,最终各自剩余 1 个元素,自然有序,然后先将每两个元 素进行合并,变为有序,然后再将两个小组合并,变为有序,循环往复,直到整个数组有序
1 递归到最底部后,开始层层merge,low,high,mid任然对应分组时的位置
1 2 3 4 5 6 7 merge的时候,需要三个指针,i指向A的有序的前半段,j指向A有序的后半段,k指向放入的数组的当前位置 1、将A[] 元素全部复制放到B[] ,i ,j分别对应low\mid+1起始位置,k=i. 2、i j 先比较,小的放入A[k],i++,j不动,k++ 3、直到有剩余,i到头了,j还没到头;或者j到头了,i还没到头。while循环将剩余的全部放入,k随着++
代码如下
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 #include <stdio.h> #include <stdlib.h> #include <time.h> typedef int ElemType; typedef struct { ElemType *str; int len; }SSTable; void init_table (SSTable &SST,int len) { srand(time(NULL )); SST.len=len; SST.str=(ElemType*)malloc (sizeof (ElemType)*SST.len); int i; for (i=0 ;i<SST.len;i++){ SST.str[i]=rand()%100 ; } } void print_table (SSTable SST) { int i; for (i=0 ;i<SST.len;i++){ printf ("%3d" ,SST.str[i]); } printf ("\n" ); } void swap (ElemType &a,ElemType &b) { ElemType temp; temp=a; a=b; b=temp; } void merge (ElemType A[],int low,int mid,int high) { static ElemType B[10 ]; int i,j,k; for (k=low;k<=high;k++){ B[k]=A[k]; } for (i=low,j=mid+1 ,k=i;i<=mid&&j<=high;k++) { if (B[i] <= B[j]) { A[k] = B[i]; i++; } else { A[k] = B[j]; j++; } } while (i <= mid) { A[k] = B[i]; k++; i++; } while (j <= high) { A[k] = B[j]; k++; j++; } } void merge_sort (ElemType A[],int low,int high) { if (low<high) { int mid = (low + high) / 2 ; merge_sort(A, low, mid); merge_sort(A, mid+1 ,high); merge(A,low,mid,high); } } int main () { SSTable T; init_table(T,10 ); print_table(T); merge_sort(T.str,0 ,9 ); print_table(T); return 0 ; }
1 2 3 4 MergeSort 函数的递归 次数是 log 2 n, Merge 函数的循环了 n 次, 因此时间复杂度是 O(nlog 2 n)。 归并排序最好、最坏、平均时间复杂度都是 O(nlog 2 n)。 归并排序的空间复杂度是 O(n),因为使用了数组 B,它的大小与 A 一样,占用 n 个元素的 空间。
所有算法空间时间复杂度