ACM经典算法之数据结构


转自:http://blog.sina.com.cn/s/blog_93d2ceba010145f0.html


一、(顺序队列)

#define maxsize 100
typedef struct
{
    int data[maxsize];
    int front;
    int rear;
sqqueue; 

int sqinit(sqqueue *p) //队列初始化
{
    p->front=0;
    p->rear=0;
    return 1;
}

int enqueue(sqqueue *q, int e) //入队
{
    if((q->rear+1)%maxsize==q->front)
        return 0;
    else
        q->data[q->rear]=e;
    q->rear=(q->rear+1)%maxsize;
    return 1;
}

int dequeue(sqqueue *q) //出队
{
    int e;
    if (q->front==q->rear)
        return 0;
    e=q->data[q->front];
    q->front=(q->front+1)%maxsize;
    return e;
}

int empty(sqqueue *q)  //判空
{
    int v;
    if (q->front==q->rear)
        v=1;
    else
        v=0;
     return v; 
}

int gethead(sqqueue *q)  //取得头元素
{
    int e;
    if (q->front==q->rear) 
        e=-1;
    else
        e=q->data[q->front];
    return e;
}

void display(sqqueue *q) //显示所有元素
{
    int s;
    s=q->front;
    printf("the sequeue is display:\n");
    if (q->front==q->rear)
        printf("the sequeue is empty!");
    else
        {
        while(s<q->rear)
            {
            printf("->%d", q->data[s]);
            s=(s+1)%maxsize;
            
    printf("\n");
}
}

main(sqqueue *head)  //函数使用样例
{
    int n,i,m,x,y,select,xq;
    printf("create empty sequeue\n");
    sqinit(head);
    printf("please input the sequeue length:\n"); 
    scanf("%d",&n);
    for (i=0;i<n;i++)
        {
        printf("please input sequeue value:\n");
        scanf("%d",&m);
        enqueue(head,m);
       }
    printf("head->rear:%d\n",head->rear);
    printf("head->front:%d\n",head->front);
    display(head);
    printf("select **** enqueue() \n");
    printf("select **** dequeue() \n");
    printf("select **** empty () \n");
    printf("select **** gethead() \n");
    printf("select **** display() \n");
    printf("please select (1--5):");
    scanf("%d",&select);
    switch(select)
        {
        case 1:
            
            printf("please input value :\n ");
            scanf("%d",&x);
            enqueue(head,x);
            display(head);
            break;
            }
        case 2:
            {
            dequeue(head);
            display(head);
            break;
            }
        case 3:
            {
        if(empty(head))
            printf("the sequeue is empty");
        else
            printf("the sequeue is full");
            }
        case 4:
            {
            y=gethead(head);
            printf("output head value:%d\n",y);
            break;
            }
        case 5:
            {
            display(head);
            break;
            }
        }
    }


二、(顺序栈)

#define 100
typedef struct
{
    int stack[m];
    int top;
stackstru; 

init(stackstru *s) 
{
    s->top=0;
    return 1;
}

int push(stackstru *s,int x) 
{
    if (s->top==m)
        printf("the stack is overflow!\n");
    else
        {
        s->top=s->top+1;
        s->stack[s->top]=x;
        }
}

void display(stackstru *s) 
{
    if(s->top==0)
        printf("the stack is empty!\n");
    else
        {
        while(s->top!=0)
            {
            printf("%d->",s->stack[s->top]);
            s->top=s->top-1;
            }
        }
}

int pop(stackstru *s) 
{
    int y;
    if(s->top==0)
        printf("the stack is empty!\n");
    else
        {
        y=s->stack[s->top];
        s->top=s->top-1;
        return y;
        }
}

int gettop(stackstru *s) 

    int e;
    if(s->top==0)
        return 0;
    else 
        e=s->stack[s->top];
    return e;
}

main(stackstru *p) //函数使用演示
{
    int n,i,k,h,x1,x2,select;
    printf("create empty stack!\n");
    init(p);
    printf("input stack length:\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
        {
        printf("input stack value:\n");
        scanf("%d",&k);
        push(p,k);
        }
    printf("select 1:display()\n");
    printf("select 2:push()\n");
    printf("select 3:pop()\n");
    printf("select 4:gettop()\n");
    printf("input your select(1-4):\n");
    scanf("%d",&select);
    switch(select)
        {
        case 1:
            {
            display(p);
            break;
            }
        case 2:
            {
            printf("input push value:\n");
            scanf("%d",&h);
            push(p,h);
            display(p);
            break;
            }
        case 3:
            {
            x1=pop(p);
            printf("x1->%d\n",x1);
            display(p);
            break;
            }
        case 4:
            {
            x2=gettop(p);
            printf("x2->%d",x2);
            break;
            }
        }
}


三、(链表)

define null 

typedef char ElemType;  

typedef struct LNode
{
    ElemType data;
    struct LNode *next;
};

setnull(struct LNode **p);
int length (struct LNode **p);
ElemType get(struct LNode **p,int i);
void insert(struct LNode **p,ElemType x,int i);
int delete(struct LNode **p,int i);
void display(struct LNode **p); 

main()
{
    struct LNode *head,*q; 
    int select,x1,x2,x3,x4;
    int i,n; 
    int m,g;
    char e,y; 

    head=setnull(&head); 
    printf("请输入数据长度: ");
    scanf("%d",&n);
    for(i=1;i<n;i++);
        {
        printf("将数据插入到单链表中: ");
        scanf("%d",&y);
        insert(&head,y,i);} 
        display(&head); 

        printf("select 求长度 length()\n");
        printf("select 取结点 get()\n");
        printf("select 求值查找 locate()\n");
        printf("select 删除结点 delete()\n");
        printf("input your select: ");
        scanf("%d",&select); 
        switch(select)
            {
            case 1:
                {
                x1=length(&head);
                printf("输出单链表的长度%d ",x1);
                display(&head);
                }break;
            case 2:
                {
                printf("请输入要取得结点: ");
                scanf("%d",&m);
                x2=get(&head,m);
                printf(x2);
                display(&head);
                }break;
         case 3:
                {
                printf("请输入要查找的数据: ");
                scanf("%d",&e);
                x3=locate(&head,e);
                printf(x3);
                display(&head);
                }break;
         case 4:
                {
                printf("请输入要删除的结点: ");
                scanf("%d",&g);
                x4=delete(&head,g);
                printf(x4);
                display(&head);
                }break;
            }
        }
}


setnull(struct LNode **p)
{
    *p=null;
}

int length (struct LNode **p)
{
    int n=0;
    struct LNode *q=*p;
    while (q!=null)
        {
        n++;
        q=q->next;
        }
    return(n);
}

ElemType get(struct LNode **p,int i)
{
    int j=1;
    struct LNode *q=*p;
    while (j<i&&q!=null)
        {
        q=q->next;
        j++;
        }
        if(q!=null)
            return(q->data);
        else
            printf("位置参数不正确!\n");
}

int locate(struct LNode **p,ElemType x)
    {
    int n=0;
    struct LNode *q=*p;
    while (q!=null&&q->data!=x)
        {
        q=q->next;
        n++;
        }
    if(q==null)
        return(-1);
    else
        return(n+1);
}

void insert(struct LNode **p,ElemType x,int i)
    {
    int j=1;
    struct LNode *s,*q;
    s=(struct LNode *)malloc(sizeof(struct LNode));
    s->data=x;
    q=*p;
    if(i==1)
        {
        s->next=q;
        p=s;
        }
    else
        {
        while(j<i-1&&q->next!=null)
            {
            q=q->next;
            j++;
            }
        if(j==i-1)
            {
            s->next=q->next;
            q->next=s;
            }
        else 
            printf("位置参数不正确!\n");
        
}

int delete(struct LNode **p,int i)
{
    int j=1;
    struct LNode *q=*p,*t;
    if(i==1)
        {
        t=q;
        *p=q->next;
        }
    else
        {
        while(j<i-1&&q->next!=null)
            {
            q=q->next;
            j++;
            }
        if(q->next!=null&&j==i-1)
            {
            t=q->next;
            q->next=t->next;
            }
        else 
            printf("位置参数不正确!\n");
        }
    if(t=null) 
    free(t);
}

void display(struct LNode **p)
    
    struct LNode *q;
    q=*p;
    printf("单链表显示: ");
    if(q==null)
        printf("链表为空!");
    else if (q->next==null)
        printf("%c\n",q->data);
    else
        {
        while(q->next!=null)
            {
            printf("%c->",q->data);
            q=q->next;
            }
        printf("%c",q->data);
    }
    printf("\n");
}


四、(链栈)

# define null 0 

typedef struct stacknode
{
int data;
struct stacknode *next;
} stacklink;
typedef struct
{
stacklink *top;
int stacksize;
}stackk;

initlink(stackk *s)
{
s->top=(stacklink *)malloc(sizeof(stacklink));
s->top->data=0;
s->top->next=null;
} 

int poplink(stackk *s)
{
stackk *p;int v;
if(s->top->next==null) printf("the stackis empty\n");
else
{
v=s->top->next->data;
p=s->top->next;
s->top=s->top->next;
} 
free(p);
return v;
}
}

int pushlink(stackk *s,int x)
{
stackk *p;
p=(stacklink *)malloc(sizeof(stacklink));
p->data=x;
p->next=s->top->next;
s->top->next=p;
}

int gettop(stackk *s)
{
int e;
if(s==null) printf("the stack is empty!\n");
e=s->top->next->data;
return e;
}

display(stackk *s)
{
stackk *p;
p=s->top->next;
printf("display the stacklink:\n");
if (s->top=null) printf("the stacklink is empty!\n");
else
{
while(p)
{
printf("->%d",p->data);
p=p->next;
}
}
}

main(stacklink *p)
{
int n,k,i,select,h,x1,x2;
printf("create a empty stacklink!\n");
initlink(p);
printf("input a stacklink length:\n");
scanf("%d",&n);
for (i=1;i<=n;i++)
{printf("input a stacklink value:\n");
scanf("%d",&k);
pushlink(p,k);
}
printf("select 1:display()\n");
printf("select 2:pushlink()\n");
printf("select 3:poplink()\n");
printf("select 4:gettop()\n");
printf("input a your select(1-4):\n");
scanf("%d",&select);
switch(select)
{case 1:
{display(p);break;}
case 2:
{printf("input a push a value :\n");
scanf("%d",&h);
pushlink(p,h);
display(p);
break;}
case 3:
{x1=poplink(p);printf("x1->%d\n",x1);
display(p);
break;}
case 4:
{x2=gettop(p);printf("x2->%d",x2);
break;}
}
}



五、(二叉树)

typedef struct bitnode
{
char data;
struct bitnode *lchild, *rchild;
}bitnode, *bitree;

void createbitree(t,n)
bitnode ** t;
int *n;
{
char x;
bitnode *q;
*n=*n+1;
printf("\n Input %d DATA:",*n);
x=getchar();
if(x!='\n') getchar();
if (x=='\n')
return;
q=(bitnode*)malloc(sizeof(bitnode));
q->data=x;
q->lchild=NULL;
q->rchild=NULL;
*t=q;
printf(" This Address is: %o, Data is: %c,\n Left Pointer is: %o, Right Pointer is: %o",q,q->data,q->lchild,q->rchild);
createbitree(&q->lchild,n);
createbitree(&q->rchild,n);
return;
}

void visit(e)
bitnode *e;
{
printf(" Address: %o, Data: %c, Left Pointer: %o, Right Pointer: %o\n",e,e->data,e->lchild,e->rchild);
}

void preordertraverse(t)
bitnode *t;
{
if(t)
{
visit(t);
preordertraverse(t->lchild);
preordertraverse(t->rchild);
return ;
}
else
return ;
}

void countleaf(t,c)
bitnode *t;
int *c;
{
if(t!=NULL)
{
if (t->lchild==NULL && t->rchild==NULL)
{*c=*c+1;
}
countleaf(t->lchild,c);
countleaf(t->rchild,c);
}
return;
}

int treehigh(t)
bitnode *t;
{
int lh,rh,h;
if(t==NULL)
h=0;
else
{
lh=treehigh(t->lchild);
rh=treehigh(t->rchild);
h=(lh>rh ? lh:rh)+1;
}
return h;
}

main()
{
bitnode *t; int count=0;
int n=0;
printf("\n Please input TREE Data:\n");
createbitree(&t,&n);
printf("\n This is TREE struct: \n");
preordertraverse(t);
countleaf(t,&count);
printf("\n This TREE has %d leaves ",count);
printf(" , High of The TREE is: %d\n",treehigh(t));
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值