您的位置:首页 > 理论基础 > 数据结构算法

ACM经典算法之数据结构

2015-08-18 22:54 579 查看
转自: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 a empty sequeue\n");

sqinit(head);

printf("please input the sequeue length:\n");

scanf("%d",&n);

for (i=0;i<n;i++)

{

printf("please input a 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 1 **** enqueue() \n");

printf("select 2 **** dequeue() \n");

printf("select 3 **** empty () \n");

printf("select 4 **** gethead() \n");

printf("select 5 **** display() \n");

printf("please select (1--5):");

scanf("%d",&select);

switch(select)

{

case 1:

{

printf("please input a 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 m 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 a empty stack!\n");

init(p);

printf("input a stack length:\n");

scanf("%d",&n);

for(i=0;i<n;i++)

{

printf("input a 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 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);

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 0

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 1 求长度 length()\n");

printf("select 2 取结点 get()\n");

printf("select 3 求值查找 locate()\n");

printf("select 4 删除结点 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));

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: