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");
}
四、(链栈)
五、(二叉树)
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));
}
一、(顺序队列)
#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));
}
相关文章推荐
- MongoDB学习二--MongoDB 数据结构和查询
- 散列-分离链接法(数据结构与算法分析-C语言描述)
- 数据结构(Java语言)——AVLTree简单实现
- iOS开发——OC常用的数据结构一览。
- 数据结构基本概念
- 数据结构之---C语言实现最短路径之Dijkstra(迪杰斯特拉)算法
- 浅谈数据结构-树和二叉树之间关系
- 浅谈数据结构-二叉树
- POJ 3125 Printer Queue 数据结构 队列
- 2015-8-18数据结构-动态规划-矩阵乘法次数最少
- 数据结构—排序总结
- 数据结构实验之二叉树的建立与遍历
- POJ 2259 Team Queue 数据结构 队列
- 浅谈数据结构-树
- 数据结构实验之求二叉树后序遍历和层次遍历
- 传说中的数据结构 SDUT 2556
- 数据结构上机测试4.1:二叉树的遍历与应用1--知先序和中序求后序
- c语言实现数据结构中的链式表
- c语言实现数据结构中的顺序表
- c语言实现数据结构中的哈希表