C语言上机报告链表顺序表堆栈队列二叉树以及几种常见排序算法.docx
《C语言上机报告链表顺序表堆栈队列二叉树以及几种常见排序算法.docx》由会员分享,可在线阅读,更多相关《C语言上机报告链表顺序表堆栈队列二叉树以及几种常见排序算法.docx(26页珍藏版)》请在冰豆网上搜索。
C语言上机报告链表顺序表堆栈队列二叉树以及几种常见排序算法
上机报告
上机实验一:
顺序表相关操作。
程序:
#include
typedefstructlist_type
{
intdata[20];
intnum;
}list;
voidinsertl(list*l,inti,intx)
{
intj;
for(j=l->num-1;j>=i-1;j--)
{
l->data[j+1]=l->data[j];
}
l->data[i]=x;
l->num++;
for(j=0;jnum;j++)
{
printf("%d",l->data[j]);
printf("\n");
}
}
voiddelate(list*L,intk)
{
inti;
if(k<0||k>L->num-1)printf("Error!
\n");
for(i=k+1;inum;i++)
L->data[i-1]=L->data[i];
L->num--;
for(i=0;inum;i++)
printf("%d",L->data[i]);
printf("\n");
}
voiddelte_negative(list*L)
{
inti,j;
intn;
for(i=0;inum-1;i++)
{
if(L->data[i]<0)
{
n=L->num;
j=i;
while(i!
=n)
{
L->data[j]=L->data[j+1];
j++;
n--;
}
i--;
L->num--;
}
}
for(i=0;inum;i++)
printf("%d",L->data[i]);
printf("\n");
}
voidmain()
{
lista;
inti,n,k,c;
for(i=0;i<20;i++)
{
scanf("%d",&a.data[i]);
if(a.data[i]==-1)
{
break;
}
}
a.num=i;
printf("请输入要插入的位置");
scanf("%d",&n);
printf("请输入要插入的数值");
scanf("%d",&k);
insertl(&a,n,k);
printf("请输入要删除的位置");
scanf("%d",&c);
delate(&a,c);
delte_negative(&a);
}
典型测试数据(输入):
数据:
9632-5-652367865-1;
插入位置:
4;
插入数:
23;
删除位置:
5;
应输出:
1:
9632-5-62352367865;
2:
9632-5-623367865;
3:
963223367865;
上机时遇到的问题:
a.num与a->num区别,以及何时用a.num何时用a->num。
解决方法:
问同学。
实验输出:
1:
9632-5-62352367865;
2:
9632-5-623367865;
3:
963223367865;
上机实验二:
链表的相关操作:
程序:
#include
#include
typedefstructnode_type
{
intnumber;
structnode_type*next;
}node_type;
typedefstructlist_type
{
node_type*head;
node_type*tail;
intlength;
}list_type;
voidcreate_list(list_type*p)
{
printf("请创建一个链表:
\n");
node_type*p1,*p2;
p->length=0;
p1=(node_type*)malloc(sizeof(node_type));
printf("请输入第1个元素:
\n");
scanf("%d",&p1->number);
while(p1->number!
=0)
{
p->length++;
if(p->length==1)
p->head=p1;
else
p2->next=p1;
p->tail=p1;
p2=p1;
p1=(node_type*)malloc(sizeof(node_type));
printf("请输入第%d个元素:
\n",p->length+1);
scanf("%d",&p1->number);
}
}
voidinsert_list(list_type*p,intinsert_location,node_type*insert_node)
{
node_type*p1;
p1=p->head;
inti;
if(insert_location==1)
{
insert_node->next=p1;
p->head=insert_node;
}
else
if(insert_location==p->length+1)
{
p->tail->next=insert_node;
p->tail=insert_node;
}
else
{
for(i=1;ip1=p1->next;
insert_node->next=p1->next;
p1->next=insert_node;
}
p->length++;
}
voiddelete_list(list_type*p,intdelete_location)
{
node_type*p1;
p1=p->head;
inti;
if(delete_location==1)
p->head=p1->next;
else
{
for(i=1;ip1=p1->next;
p1->next=p1->next->next;
}
p->length--;
}
voidoutput_list(list_type*p)
{
printf("输出链表:
\n");
node_type*p1;
p1=p->head;
inti;
for(i=1;i<=p->length;i++)
{
printf("第%d个元素:
\n",i);
printf("%d\n",p1->number);
p1=p1->next;
}
}
voidmain()
{
list_typel;
create_list(&l);
node_typeinsert_node;
printf("请输入要插入的元素:
\n");
scanf("%d",&insert_node.number);
intinsert_location;
printf("请输入要插入的位置:
\n");
scanf("%d",&insert_location);
while(insert_location<1||insert_location>l.length+1)
{
printf("输入出错,请重新输入:
\n");
scanf("%d",&insert_location);
}
insert_list(&l,insert_location,&insert_node);
printf("%d\n",l.length);
output_list(&l);
intdelete_location;
printf("请输入要删除的位置:
\n");
scanf("%d",&delete_location);
while(delete_location<1||delete_location>l.length);
{
printf("输入出错,请重新输入:
\n");
scanf("%d",&delete_location);
}
delete_list(&l,delete_location);
output_list(&l);
}
典型测试数据(输入):
数据:
9632-5-652;
插入位置:
2;
插入数:
23;
删除位置:
4;
查询数:
23;
应输出:
1:
9632-5-652;
2:
962332-5-652;
3:
962332-652;
4:
9632-652;
上机时遇到的问题:
对链表的用法不清楚。
解决方法:
问同学。
实验输出:
1:
9632-5-652;
2:
962332-5-652;
3:
962332-652;
4:
9632-652;
上机实验三:
栈的相关操作:
程序:
#include
typedefstructstack_type
{
intstack[20];
inttop;
}stacktype;
intpushs(stacktype*s)
{inti=0,x;
while
(1)
{
scanf("%d",&x);
if(!
x)
break;
else
{
s->stack[s->top]=x;
s->top++;
i++;
}
}
return(i);
}
intpop(stacktype*s)
{
s->top--;
return(s->stack[s->top+1]);
}
voidput(stacktype*s)
{
intn,i=0;
n=s->top;
while(n>0)
{
printf("%d\n",s->stack[i]);
n--;
i++;
}
}
voidmain()
{
stacktypes;
intk;
s.top=0;
k=pushs(&s);
printf("输出进栈元素");
put(&s);
pop(&s);
pop(&s);
printf("输出调用两次出栈函数之后的元素");
put(&s);
}
典型测试数据(输入):
数据:
369850;
应输出:
1:
369850;
2:
369;
上机时遇到的问题:
没有给栈顶初值,导致实验结果一直不正确。
解决方法:
问同学。
实验输出:
1:
369850;
2:
369;
上机实验四:
队列的相关操作:
程序:
#include
#definem100
typedefstructqeue_type
{
intqe[m+1];
intfront;
intrear;
intn;
}qeuetype;
intenter(qeuetype*q,intx)
{
if(((q->rear)+1)%m==q->front)
return0;
else
{
q->qe[q->rear]=x;
q->rear=(q->rear+1)%m;
return1;
}
}
intel(qeuetype*q)
{
if(q->rear==q->front)
return0;
else
{
q->front=(q->front+1)%m;
return(q->qe[q->front]);
}
}
voidaa(qeuetype*q)
{
intl;
l=q->n;
while(l>0)
{
if(q->qe[q->front]>0)
enter(q,q->qe[q->front]);
else
(q->n)--;
el(q);
l--;
}
}
voidpush(qeuetype*a)
{
while
(1)
{
if(a->n>0)
{
printf("%d\n",a->qe[a->front]);
a->front=a->front+1;
(a->n)--;
}
else
break;
}
}
voidmain()
{
qeuetypea;
a.front=0;
a.rear=0;
inti;
ints[10]={2,3,-4,6,-5,8,-9,7,-10,20};
a.n=10;
for(i=0;i<10;i++)
{
enter(&a,s[i]);
}
printf("调用aa函数之前\n");
push(&a);
a.front=0;
a.n=10;
aa(&a);
printf("调用aa函数之后\n");
push(&a);
}
应输出:
1:
23-46-58-97-1020;
2:
2368720;
上机时遇到的问题:
无。
实验输出:
1:
23-46-58-97-1020;
2:
2368720;
上机实验五:
二叉树相关操作:
程序:
#include
#include
typedefstructbtree
{
intdata;
structbtree*l;
structbtree*r;
}bnode;
voidcreat(bnode**s)
{
bnode*p,*q;
intk;
inti,n;
*s=NULL;
printf("inputn:
");
scanf("%d",&n);
for(i=0;i{
p=(bnode*)malloc(sizeof(bnode));
p->l=NULL;
p->r=NULL;
printf("inputk:
");
scanf("%d",&k);
p->data=k;
if(*s==NULL)
*s=p;
else
{
q=*s;
while(q!
=NULL)
{
if(q->data>k)
{
if(q->l!
=NULL)
q=q->l;
else
{
q->l=p;
q=NULL;
}
}
else
if(q->r!
=NULL)
q=q->r;
else
{
q->r=p;
q=NULL;
}
}
}
}
}
voidxianxun(bnode*bt)
{
if(bt==NULL)
printf("error");
else
{
printf("%d\n",bt->data);
if(bt->l!
=NULL)
xianxun(bt->l);
if(bt->r!
=NULL)
xianxun(bt->r);
}
}
voidzhongxun(bnode*bt)
{
if(bt==NULL)
printf("error");
else
{
if(bt->l!
=NULL)
zhongxun(bt->l);
printf("%d\n",bt->data);
if(bt->r!
=NULL)
zhongxun(bt->r);
}
}
voidhouxun(bnode*bt)
{
if(bt==NULL)
printf("error");
else
{
if(bt->r!
=NULL)
houxun(bt->r);
if(bt->l!
=NULL)
houxun(bt->l);
printf("%d\n",bt->data);
}
}
voidmain()
{
bnode*st;
creat(&st);
printf("xianxunbianlijieguo\n");
xianxun(st);
printf("zhongxunbianlijieguo\n");
zhongxun(st);
printf("houxunbianlijieguo\n");
houxun(st);
}
典型测试数据(输入):
数据:
n=6;
数值:
569875352163;
应输出:
先序遍历结果:
563521987563;
中序遍历结果:
213556637598;
后序遍历结果:
637598213556;
上机时遇到的问题:
无;
实验输出:
先序遍历结果:
563521987563;
中序遍历结果:
213556637598;
后序遍历结果:
637598213556;
上机实验六:
查找相关操作:
程序:
#include
#definem8
intshunxun(int*a,intk)
{
inti;
for(i=0;i{
if(a[i]==k)
{
returni+1;
break;
}
}
printf("can'tfindtheword\n");
return-1;
}
interfen(int*a,intk)
{
intlow,high;
intmid;
low=0;
high=m-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==k)
returnmid+1;
elseif(a[mid]low=mid+1;
else
high=high-1;
}
printf("can'tfindtheword\n");
return-1;
}
voidmain()
{
intk;
inta[m]={3,10,13,17,40,43,50,70};
ints;
scanf("%d",&k);
printf("顺序查找结果\n");
s=shunxun(a,k);
printf("%d\n",s);
printf("二分查找查找结果\n");
s=erfen(a,k);
printf("%d\n",s);
}
实验数据:
预计结果:
第一次输入:
43
输出结果:
6;
第二次输入:
5;
输出结果:
can'tfindtheword
-1;
上机时遇到的问题:
对二分查找不是很熟悉;
解决办法:
查书;
实验输出:
第一次输入:
43
输出结果:
6;
第二次输入:
5;
输出结果:
can'tfindtheword
-1;
上机实验七:
排序相关算法;
程序:
#include
#definen10
voidpush(int*a)
{
inti;
for(i=0;i{
printf("%4d",a[i]);
}
}
voidxuanze(int*a)
{
inti,j,p,temp;
for(i=0;i{
p=i;
for(j=i+1;j{
if(a[p]{
p=j;
}
}
if(p!
=i)
{
temp=a[p];
a[p]=a[i];
a[i]=temp;
}
}
push(a);
printf("\n");
}
voidmaopao(int*a)
{
inti,j,temp;
for(i=0;i{
for(j=0;j{
if(a[j+1]>a[j])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
push(a);
printf("\n");
}
voidinsertSort(int*a)
{
inttemp;
for(inti=1;i{
for(intj=0;j
{
if(a[j]>a[i])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
push(a);
printf("\n");
}
voidmain()
{
inta[n]={513,87,512,61,908,170,897,275,653,462};
intb[n]={513,87,512,61,908,170,897,275,653,462};
intc[n]={513,87,512,61,908,170,897,275,653,462};
xuanze(a);
maopao(b);
insertSort(c);
}
预计输出结果:
选择排序输出结果:
9088976535135124622751708761;
冒泡排序输出结果:
9088976535135124622751708761;
直接插入排序输出结果:
6187170275462512513653897908;
上机时遇到的问题:
对直接排序不够了解;
解决办法:
上网查资料:
实验输出结果:
选择排序输出结果:
9088976535135124622751708761;
冒泡排序输出结果:
9088976535135124622751708761;
直接插入排序输出结果:
6187170275462512