数据结构上机考核试题及答案Word格式文档下载.docx
- 文档编号:17302090
- 上传时间:2022-12-01
- 格式:DOCX
- 页数:27
- 大小:19.86KB
数据结构上机考核试题及答案Word格式文档下载.docx
《数据结构上机考核试题及答案Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《数据结构上机考核试题及答案Word格式文档下载.docx(27页珍藏版)》请在冰豆网上搜索。
a'
)insertlist(L);
elsedeletelist(L);
当前顺序表中的数据是:
length;
%3d"
L->
\n请再选择:
}
voidinsertlist(seqlist*L)
{intx,i,j;
\n请输入要插入的整数:
"
\n%d"
x);
\n在下面序列中插入%d\n"
x);
i=0;
while(i<
length&
x<
data[i])i++;
if(i<
0||i>
length+1)
Error("
\n插入位置错误!
elseif(L->
length>
=ListSize)Error("
\n表溢出,无法插入!
elseif(x==L->
data[i])printf("
\n重复插入,不允许!
else{printf("
\n将数据%d插入到第%d的位置上\n"
x,i);
for(j=L->
length-1;
j>
=i;
j--)
/*=========空白处1===========*/
data[j+1]=l->
data[j];
data[i]=x;
length++;
/*======================*/
}}
voiddeletelist(seqlist*L)
{intx,i,j,num;
\n请输入要删除的整数:
\n在下面序列中删除%d\n"
num=0;
if(x!
=L->
data[i])Error("
\n没找到要删除的整数!
else
{
num++;
while(L->
data[i+1]==x&
length-1)
{i++;
\n删除原表中从第%d个位置以后的%d个数据%d\n"
i-num+1,num,x);
for(j=i+1;
j<
j++)
/*=====请在下面填入相应的语句======*/
data[j-num]=L->
length=L->
length-num;
/*=====================*/}}_
2\单链表的插入与删除*/
#include<
stdio.h>
stdlib.h>
string.h>
#definen5
typedefstructnode{
intdata;
structnode*next;
}ListNode;
typedefListNode*LinkList;
LinkListCreatelinklist(void);
voidInsertlinklist(LinkListhead);
voidDeletelinklist(LinkListhead);
voidOutputlinklist(LinkListhead);
LinkListhead;
head=Createlinklist();
请选择:
A--------------插入-----------------\n"
B--------------删除-----------------\n"
C--------------退出-----------------\n"
if(c=='
)Insertlinklist(head);
elseDeletelinklist(head);
Outputlinklist(head);
/*****************************/
LinkListCreatelinklist()
intx;
LinkListhead,s,r;
head=(ListNode*)malloc(sizeof(ListNode));
r=head;
r->
next=NULL;
请按递减顺序输入整数(输0结束):
while(x!
=0)
{s=(ListNode*)malloc(sizeof(ListNode));
s->
data=x;
next=r->
next;
next=s;
r=s;
returnhead;
voidInsertlinklist(LinkListhead)
ListNode*p,*s;
inti,j;
请输入要插入的整数:
p=head;
j=0;
while(p->
next&
p->
next->
data)
j++;
p=p->
if(x==p->
重复插入,不允许!
{s=(ListNode*)malloc(sizeof(ListNode));
/*=====请在下面填入相应的语句===*/
S->
next=P->
P->
next=S;
/*====================*/}}
voidDeletelinklist(LinkListhead)
ListNode*p,*r;
请输入要删除的整数:
r=head->
while(r&
{p=r;
r=r->
if(r==NULL||x!
=r->
data)Error("
没找到要删除的整数.\n"
else{
/*==========空白处2=========*/
free(r);
/*====================*/}}
voidOutputlinklist(LinkListhead)
{ListNode*p;
p=head->
当前链表中数据为:
while(p)
{printf("
%6d"
p->
data);
}}_
/*====数据结构上机考核试题3======*/
/*栈的操作#defineStackSize10
typedefstruct{
DataTypedata[StackSize];
inttop;
}SeqStack;
voidInitStack(SeqStack*s)
{s->
top=0;
}
intStackEmpty(SeqStack*S)
{if(S->
top==0)return1;
elsereturn0;
intStackFull(SeqStack*S)
{returnS->
top==StackSize;
voidPush(SeqStack*S,DataTypex)
{if(StackFull(S))
栈溢出!
/*==========空白处1============*/
ElseS->
data[++(S->
top)]=x;
/*=====================*/
DataTypePop(SeqStack*S)
/*========空白处2=========*/
If(StackEmpty(S))
Error(“Stackunderflow”);
ElsereturnS->
data[--(S->
top)];
/*=========*/
voidconversion(intN,intB);
{intN,B;
charch;
进行数值转换请输入Y,退出请输入N:
ch);
while(ch=='
Y'
||ch=='
y'
请输入需要转换的十进制数:
N);
\n请输入想要转换的进制数(2,8or16):
B);
conversion(N,B);
继续转换请输入Y,退出请输入N:
}}
voidconversion(intN,intB)
{DataTypei;
SeqStack*S;
InitStack(S);
while(N)
{Push(S,N%B);
N=N/B;
转换的结果为:
while(!
StackEmpty(S))
{i=Pop(S);
switch(i)
{case10:
%c"
'
break;
case11:
b'
case12:
case13:
d'
case14:
e'
case15:
f'
default:
i);
}_
/*==========数据结构上机考核试题4======*/
/*队列的操作*/
#defineQueueSize100
typedefcharDataType;
intfront;
intrear;
intcount;
DataTypedata[QueueSize];
}CirQueue;
voidInitQueue(CirQueue*Q)
{Q->
front=Q->
rear=0;
Q->
count=0;
intQueueEmpty(CirQueue*Q)
{returnQ->
count==0;
intQueueFull(CirQueue*Q)
count==QueueSize;
voidEnQueue(CirQueue*Q,DataTypex)
{if(QueueFull(Q))
Error("
队列溢出!
else
{/*==========空白处1===========*/
Q->
count++;
data[Q->
rear]=x;
rear=(Q->
rear+1)%QueueSize;
/*=================*/}}
DataTypeDeQueue(CirQueue*Q)
{DataTypetemp;
if(QueueEmpty(Q))
队列下溢!
else{temp=Q->
front];
/*============空白处2===========*/
count--;
front=(q->
front+1)%QueueSize;
/*==============*/
returntemp;
voidInputch(CirQueue*Q);
voidOutputch(CirQueue*Q);
{CirQueue*Q=0;
charch;
printf("
\n继续进行请按Y,退出请按N:
scanf("
while(ch=='
{InitQueue(Q);
Inputch(Q);
Outputch(Q);
voidInputch(CirQueue*Q)
{charch;
\n请输入字符串并以$为结束符:
while(ch!
$'
{EnQueue(Q,ch);
voidOutputch(CirQueue*Q)
你输入的字符串是:
while(!
QueueEmpty(Q))
{ch=DeQueue(Q);
ch);
/*========数据结构上机考核试题5=====*/
/*二叉树的遍历*/
DataTypedata;
structnode*lchild,*rchild;
}BinTNode;
typedefBinTNode*BinTree;
voidCreateBinTree(BinTree*T);
voidLevelorder(BinTreeT);
{BinTreeT;
charch1,ch2;
ch1='
;
while(ch1=='
||ch1=='
\nA------------------二叉树建立-------------"
\nB------------------层次遍历---------------"
\nC------------------退出-------------------\n"
ch2);
switch(ch2){case'
:
case'
请按先序输入建立二叉树存储的结点序列:
CreateBinTree(&
T);
B'
该二叉树的层次遍历序列为:
Levelorder(T);
n'
}}}
voidCreateBinTree(BinTree*T)
{charch;
if(ch=='
0'
)*T=NULL;
else{*T=(BinTNode*)malloc(sizeof(BinTNode));
(*T)->
data=ch;
lchild);
rchild);
voidLevelorder(BinTreeT)
{inti,j;
BinTNode*q[20],*p;
p=T;
if(p!
=NULL){i=1;
q[i]=p;
j=2;
while(i!
=j)
{p=q[i];
%3c"
/*==========空白处1==============*/
if(p->
lchild!
=NULL){q[j]=p->
lchild;
j++;
rchild!
rchild;
i++;
/*===========*/}}
/*======数据结构上机考核试题6========*/
/*求二叉树叶子结点个数*/
voidLeafnum(BinTreeT);
BinTreeT;
\nB------------------求叶子结点个数---------"
switch(ch2)
{case'
请按先序输入二叉树存储的结点序列:
Leafnum(T);
该二叉树有%d个叶子.\n"
count);
}}}
if(ch=='
else{*T=(BinTNode*)malloc(sizeof(BinTNode));
(*T)->
CreateBinTree(&
voidLeafnum(BinTreeT)
{if(T)
{if(T->
lchild==NULL&
T->
rchild==NULL)
/*=============空白处1==============*/
Leafnum(T->
/*==================*/}}
/*========数据结构上机考核试题7======*/
/*二分查找*/
#definen10
{intR[n],i,k,low,mid,high,m;
请按递增顺序输入10个整数:
R[i]);
需要查找请输入Y,否则输入N:
while(ch=='
请输入要查找的整数:
k);
low=0;
high=n-1;
m=0;
while(low<
=high)
{/*========空白处1===========*/
/*=======请在下面填入相应的语句========*/
mid=(low+high)/2;
m++;
if(R[mid]>
k)high=mid-1;
elseif(R[mid]<
k)low=mid+1;
elsebreak;
/*=============*/}
if(low>
high)
{printf
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 上机 考核 试题 答案