数据结构与算法(四)
发布日期:2021-05-09 09:32:20 浏览次数:30 分类:博客文章

本文共 19648 字,大约阅读时间需要 65 分钟。

���������������������������������������

  • ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
#include 
#include
#include
#define STACK_INIT_SIZE 20#define STACKINCREMENT 10typedef char ElemType;typedef struct{ ElemType *base; ElemType *top; int stackSize;}sqStack;void InitStack(sqStack *s){ s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); if( !s->base ) { exit(0); } s->top = s->base; s->stackSize = STACK_INIT_SIZE;}void Push(sqStack *s, ElemType e){ if( s->top - s->base >= s->stackSize ) { s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType) ); if( !s->base ) { exit(0); } } *(s->top) = e; s->top++;}void Pop(sqStack *s, ElemType *e){ if( s->top == s->base ) { return; } *e = *--(s->top);}int StackLen(sqStack s){ return (s.top - s.base);}int main(){ sqStack s; char c, e; InitStack( &s ); printf("������������������������������#��������������������� "); scanf("%c", &c); while(c != '#' ) { while( c >= '0' && c <= '9' ) { printf("%c", c); scanf("%c", &c); if( c < '0' || c > '9' ) { printf(" "); } } if ( ')' == c ) { Pop(&s, &e); while('(' != e ) { printf("%c ", e); Pop(&s, &e); } } else if( '+' == c || '-' == c ) { if( !StackLen(s) ) { Push(&s, c); } else { do { Pop(&s, &e); if( '(' == e ) { Push( &s, e ); } else { printf("%c ", e ); } }while( StackLen(s) && '(' != e ); Push(&s, c); } } else if( '*' == c || '/' == c || '(' == c ) { Push( &s, c ); } else if( '#' == c ) { break; } else { printf("\n���������������������������!\n"); return -1; } scanf("%c", &c); } while( StackLen(s) ) { Pop(&s, &e); printf("%c ", e); } return 0;}

������

  • ������(queue)������������������������������������������������������������������������������������������

  • ������������������������������������������(First In First Out,FIFO)���������������

  • ������������������������������������������������������������������������������������������������������������������������������

  • ���������������������������������������������������������������������������������������������������������������������������

    typedef struct QNode{    ElemType data;    struct QNode *next;}QNode, *QueuePrt;typedef struct {    QueuePrt front, rear;	//������������������} LinkQueue;
  • ���������������������������������������������������������������������������������������������������������������������������������������������������

������������������

  • ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    initQueue( LinkQueue *q ){    q->front = q->rear = ( QueuePtr )malloc( sizeof( QNode ) );    if( !q->front )        exit(0);    q->front->next = NULL;}

���������������

InsertQueue(LinkQueue *q, ElemType e){    QueuePtr p;    p = (QueuePtr)malloc(sizeof(QNode));    if( p == NULL )        exit(0);    p->data = e;    p->next = NULL;    q->rear->next = p;    q->rear = p;}

���������������

  • ���������������������������������������������������������������������������������������������������������next���������������

  • ���������������������

  • ������������������������������������������������������������������������������

DeleteQueue( LinkQueue *q, ElemType *e ){    QueuePtr p;    if( q->front == q->rear )        return;    p = q->front->next;    *e = p->data;    q->front->next = p->next;    if( q->rear == p )        q->rear = q->front;    free(p);}

������������������

  • ���������������������������������������������������������������������������������������������������������������������������������������������������
DestroyQueue( LinkQueue *q ){    while( q->front ){        q->rear = q->front->next;        free( q->front );        q->front = q->rear;    }}

������������

  • ���������������������������������������������������������������������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������
  • ������������������������������������������front���rear������������
  • ������������front���rear���������������1���������������������������������������������������������������������������������������������

������������������������

#define MAXSIZE 100typedef struct{    ElemType *base;	//���������������������������������������������������������������    int front;    int rear;}

���������������������������

initQueue(cycleQueue *q){    q->base = (ElemType *)malloc(MAXSIEZ * sizeof(ElemType));    if( !q->base )        exit(0);    q->front = q->next = 0;}

���������������

InsertQueue(cycleQueue *q, ElemType e){    if( (q->rear+1)%MAXSIZE == q->front )        return;		//������������    q->base[q->rear] = e;    q->rear = (q->rear+1) % MAXSIZE;}

���������������������

������������������������������������

#include 
int main(){ int i; int a[40]; a[0] = 0; a[1] = 1; printf("%d %d ", a[0], a[1]); for( i=2; i < 40; i++ ) { a[i] = a[i-1] + a[i-2]; printf("%d ", a[i]); } return 0;}

������������������������������������

���������������������������������������������

int Fib(int i){    if(i < 2 )        return i == 0? 0:1;    return Fib(i-1) + Fib(i-2);}

���������������������������������

void move(int n, char x, char y, char z){    if( 1 == n )    {        printf("%c---->%c\n", x, z);    }    else    {        move(n-1, x, z, y);	//���n-1������������x������z���������y���        printf("%c---->%c\n", x, z);		//������n������������x������z���        move(n-1, y, x, z);				//���n-1������������y������x������z���    }}

���������������

8*8���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

#include 
int count = 0;int notDanger( int row, int j, int (*chess)[8] ){ int i, k, flag1=0, flag2=0, flag3=0, flag4=0, flag5=0; // ��������������� for( i=0; i < 8; i++ ) { if( *(*(chess+i)+j) != 0 ) { flag1 = 1; break; } } // ��������������� for( i=row, k=j; i>=0 && k>=0; i--, k-- ) { if( *(*(chess+i)+k) != 0 ) { flag2 = 1; break; } } // ��������������� for( i=row, k=j; i<8 && k<8; i++, k++ ) { if( *(*(chess+i)+k) != 0 ) { flag3 = 1; break; } } // ��������������� for( i=row, k=j; i>=0 && k<8; i--, k++ ) { if( *(*(chess+i)+k) != 0 ) { flag4 = 1; break; } } // ��������������� for( i=row, k=j; i<8 && k>=0; i++, k-- ) { if( *(*(chess+i)+k) != 0 ) { flag5 = 1; break; } } if( flag1 || flag2 || flag3 || flag4 || flag5 ) { return 0; } else { return 1; }}// ������row: ���������������// ������n: ������������// ������(*chess)[8]: ������������������������������������void EightQueen( int row, int n, int (*chess)[8] ){ int chess2[8][8], i, j; for( i=0; i < 8; i++ ) { for( j=0; j < 8; j++ ) { chess2[i][j] = chess[i][j]; } } if( 8 == row ) { printf("��� %d ���\n", count+1); for( i=0; i < 8; i++ ) { for( j=0; j < 8; j++ ) { printf("%d ", *(*(chess2+i)+j)); } printf("\n"); } printf("\n"); count++; } else { for( j=0; j < n; j++ ) { if( notDanger( row, j, chess ) ) // ������������������ { for( i=0; i < 8; i++ ) { *(*(chess2+row)+i) = 0; } *(*(chess2+row)+j) = 1; EightQueen( row+1, n, chess2 ); } } }}int main(){ int chess[8][8], i, j; for( i=0; i < 8; i++ ) { for( j=0; j < 8; j++ ) { chess[i][j] = 0; } } EightQueen( 0, 8, chess ); printf("��������� %d ���������������!\n\n", count); return 0;}

���������

  • ������������(String)������������������������������������������������������������������������
  • ���������������������������������������

������������������������

  • ���������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������������������������������

BF������(Brute Force)

  • ���������������������������
  • ���������������������������������S���T������������N���M���������S[1]���T[1]���������������������������������S[2]���T[2]������������T[M]������������S[1]���T[1]������������T������������������������������������������������������������S������������T������������
  • ������������������������������������M(N-M+1)������������������������������O(M*N).
  • ���������������
/*���������������������*/int BF(char S[], char T[]){    int i, j;    i = j = 0;    while(S[i] != '\0' &&T[j] != '\0')    {        if(S[i] == T[i])        {            i++;            j++;        }        else        {            j = 0;            i = i-j+1;        }    }    if(T[j] == '\0')        return i-j+1;    else        return 0;}/*������String���������BF������*/int BFstring(string MotherStr, string SonStr){    int i =0, j =0;    for(;(i != MotherStr.size()) && (j != SonStr.size());){        if(MotherStr[i] == SonStr[j]){            i++, j++;        }        else{            i = i - j + 1;            j = 0;        }        if(j == SonStr.size()){            return i - j + 1;        }            }    return 0;}

KMP������

(���������������KMP������)

���

  • ���(Tree)���n(n>=0)���������������������������n=0������������������������������������������������

    • ������������������������������������(Root)������������
    • ���n>1���������������������������m(m>0)���������������������������T1���T2���...���Tm������������������������������������������������������������������������(SubTree)���
    • n>0���������������������������������������������������������������
    • m>0������������������������������������������������������������������������������������
    • ���������������������������������������������������������������������������������(Degree)���������������������������������������������������
      • ������0������������������������(Leaf)���������������
      • ���������0���������������������������������������������������������������������������������������������������
  • ���������������������

    • ������������������������������������������(Child)���������������������������������������������(Parent)���������������������������������������������(Sibling)���
    • ���������������������������������������������������������������������
  • ���������������

    • ������������������������������������������������������������������������������������

    • ���������������������������������������������

    • ���������������������������������������������(Depth)���������

    • ������������������������������������������������������������������������������������������������������������������������������������������

    • ������(Forest)���m(m>=0)���������������������������������������������������������������������������������������������������

  • ������������������

    • ������������������������������������������������������������������������������������

    • ������������������

      • ���������������������������������������������������������

      • ������������������������������������������������������������������������������������������������������������������������������������������

      • ���������

      • //���������������������������������������#define MAX_TREE_SIZE 100typedef int ElemType;typedef struct PTNode{    ElemType data;	//������������    int parent;		//������������}PTNode;typedef struct{    PTNode nodes[MAX_TREE_SIZE];    int r;			//������������    int n;			//������������}PTree;
    • ���������������������

    • #define MAX_TREE_SIZE 100typedef char ElemType;//������������typedef struct CTNode{    int child;		//���������������������    struct CTNode *next;	//������������������������������������} *ChildPtr;//������������typedef struct{    ElemType data;		//���������������������������������    int parent;			//���������������������    ChildPtr firstchild;	//������������������������������} CTBox;//���������typedef struct{    CTBox nodes[MAX_TREE_SIZE];		//������������    int r, n;}

���������

  • ���������(Binary Tree)���n(n>=0)���������������������������������������������������(������������)������������������������������������������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������2������������

  • ������������������������������������������������������������

  • ���������������������������������

������������

  • ���������

    • ���������������������������������
    • ������������������������������2
    • ������������������������������������������������������������������������������������������������������

    ���������������

  • ���������������n������������������������������������������������������i(1<=i<=n)���������������������������������������������������i������������������������������������������������������������������������

  • ���������

    • ���������������������������������������
    • ���������������������������������������������������
    • ���������������������������������������������������������������������
    • ������������������1������������������������������
    • ���������������������������������������������������������������
  • ���������������������������������������������������������������������������������������������

  • ���������

������������������

  • ���������������������������������������������������������������������������������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������

    • ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������
    • ���������������������������������������������������������������������������(���������������������������������)������������������������������������������������������������������������������������������������
    • ���������������������������������������������������������������������������������������������������������������������������������������������������
    • ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
  • ���������������������������������

    • #include 
      #include
      typedef char ElemType;typedef struct BiTNode{ char data; struct BiTNode *lchild, *rchild;} BiTNode, *BiTree;//���������������������,���������������������������������������������������CreateBiTree(BiTree *T){ char c; scanf("%c", &c); if( ' ' == c ) { *T = NULL; } else { *T = (BiTNode *)malloc(sizeof(BiTNode)); (*T)->data = c; CreateBiTree(&(*T)->lchild); CreateBiTree(&(*T)->rchild); }}//������������������������������������visit(char c, int level){ printf("%c ��������� %d���\n", c, level);}//���������������������PreOrderTraverse(BiTree T, int level){ if( T ) { visit(T->data, level); PreOrderTraverse(T->lchild, level+1); PreOrderTraverse(T->rchild, level+1); }}int main(){ int level = 1; BiTree T = NULL; CreateBiTree(&T); PreOrderTraverse(T, level);}

���������������

#include 
#include
typedef char ElemType;//���������������������//Link(0):���������������������������������//Thread(1):���������������������������������typedef enum{Link, Thread} PointerTag;typedef struct BiThrNode{ char data; struct BiThrNode *lchild, *rchild; PointerTag ltag; PointerTag rtag;} BiThrNode, *BiThrTree;//���������������������������������������������������BiThrTree pre;//���������������������������������������������������������������������������CreateBiThrTree( BiThrTree *T ){ char c; scanf("%c", &c); if( ' ' == c ) { *T = NULL; } else { *T = (BiThrNode *)malloc(sizeof(BiThrNode)); (*T)->data = c; (*T)->ltag = Link; (*T)->rtag = Link; CreateBiThrTree(&(*T)->lchild); CreateBiThrTree(&(*T)->rchild); }}//���������������������InThreading(BiThrTree T){ if( T ) { InThreading(T->lchild ); //������������������������ //������������ if( !T->lchild ) //���������������������������������������ltag���Thread���������lchild������������������������������ { T->ltag = Thread; T->lchild = pre; } if( !pre->rchild ) { pre->rtag = Thread; pre->rchild = T; } pre = T; InThreading( T->rchild ); //������������������������ }}InOrderThreding( BiThrTree *p, BiThrTree T ){ *p = (BiThrTree)malloc(sizeof(BiThrNode)); (*p)->ltag = Link; (*p)->rtag = Thread; (*p)->rchild = *p; if( !T ) { (*p)->lchild = *p; } else { (*p)->lchild = T; pre = *p; InThreading(T); pre->rchild = *p; pre->rtag = Thread; (*p)->rchild = pre; }} void visit( char c ){ printf("%c", c);}//���������������������������������void InOrderTraverse( BiThrTree T ){ BiThrTree p; p = T->lchild; while(p != T ) { while( p->ltag == Link ) { p = p->lchild; } visit(p->data); while(p->rtag == Thread && p->rchild != T ) { p = p->rchild; visit(p->data); } p = p->rchild; }}int main(){ BiThrTree P, T = NULL; CreateBiThrTree( &T ); InOrderThreading( &P, T ); printf("������������������������������"); InOrderTraverse( P ); printf("\n"); return 0;}

������������

���������������������������

  • ������������������������������������������������������
  • ���������������������������������������������������������������������������������������������������������������������������
  • ���������������������������������������������������������������������������������������������������������������������

������������������������

  • ������������������������������������
  • ������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������

  • ���������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������������������������������������������������������������

  • ������������������������������������������������������������������������������������������������������������������������������������������������������������

������������

上一篇:数据结构和算法(五)
下一篇:数据结构与算法(三)

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年04月18日 05时46分41秒