数据结构考试试题与复习题.docx
《数据结构考试试题与复习题.docx》由会员分享,可在线阅读,更多相关《数据结构考试试题与复习题.docx(27页珍藏版)》请在冰豆网上搜索。
数据结构考试试题与复习题
#include
#include
#include
#defineLIST_INIT_SIZE100
#defineLISTINCREMENT20
#defineN9
#defineM9
typedefintElemType;
typedefstruct
{
int*elem;
intlength;
intlistsize;
}SqList;
intListInsert_Sq(SqList&L,inti,inte)
{
int*q,*p;
int*newbase;
if(i<1||i>L.length+1)return0;
if(L.length>=L.listsize)
{
newbase=(ElemType*)realloc(L.elem,
(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!
newbase)exit(0);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return1;
}
intListDelete_Sq(SqList&L,inti,inte)
{
int*q,*p;
if((i<1)||(i>L.length-1))return0;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--L.length;
return1;
}
main()
{
inti,e,x,y,a,array[N],arry[M];
printf("请输入原数据组:
");
for(i=0;iscanf("%d",&array[i]);
SqListL;
L.elem=array;
L.length=N;
L.listsize=100;
printf("请输入插入数的位置:
");
scanf("%d",&x);
printf("请输入要插入的数:
");
scanf("%d",&y);
ListInsert_Sq(L,x,y);
for(i=0;i<10;i++)
{
printf("%d",L.elem[i]);
}
printf("\n");
printf("请输入要删除的原数组:
");
for(i=0;iscanf("%d",&arry[i]);
printf("请输入要删除数的位置:
");
scanf("%d",&a);
L.elem=arry;
ListDelete_Sq(L,a,e);
for(i=0;i{
printf("%d",L.elem[i]);
}printf("\n");
}
#include
#include
#include
#defineLIST_INIT_SIZE100
#defineLISTINCREMENT20
#defineN9
#defineM9
typedefintElemType;
typedefstruct
{
int*elem;
intlength;
intlistsize;
}SqList;
intListInsert_Sq(SqList&L,inti,inte)
{
int*q,*p;
int*newbase;
if(i<1||i>L.length+1)return0;
if(L.length>=L.listsize)
{
newbase=(ElemType*)realloc(L.elem,
(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!
newbase)exit(0);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return1;
}
main()
{
inti,e,x,y,a,array[N],arry[M];
printf("请输入原数据组:
");
for(i=0;iscanf("%d",&array[i]);
SqListL;
L.elem=array;
L.length=N;
L.listsize=100;
printf("请输入插入数的位置:
");
scanf("%d",&x);
printf("请输入要插入的数:
");
scanf("%d",&y);
ListInsert_Sq(L,x,y);
for(i=0;i<10;i++)
{
printf("%d",L.elem[i]);
}
printf("\n");
}
2、单链表的删除
#defineOVERFLOW0
#defineOK1
#defineNULL0
#defineERROR0
#include
#include
#include
typedefintElemType;
typedefstructLNode
{
ElemTypedata;
structLNode*next;
}LNode,*LinkList;
/*********creat**********/
LNode*creatlist(intn)
{
intx,k;
LNode*head,*r,*p;
p=(LNode*)malloc(sizeof(LNode));
head=p;
p->next=NULL;
r=p;
printf("pleaseinputthedata:
\n");
for(k=1;k<=n;k++)
{
scanf("%d",&x);
p=(LNode*)malloc(sizeof(LNode));
p->data=x;
p->next=NULL;
r->next=p;
r=r->next;
}
return(head);
}
/*****************delete*****************/
LNode*ListDelete(LinkList&L,inti,ElemTypee)
{
LNode*p,*q;
intj=0;
p=L;
while(p->next&&j{
p=p->next;
++j;
}
if(!
(p->next)||j>i-1)
returnERROR;
q=p->next;
p->next=q->next;
e=q->data;
free(q);
returnL;
}
/************main**************/
voidmain()
{
LNode*head,*p,a;
intn,i,e,j;
printf("pleaseinputthelengthofthelist:
\n");
scanf("%d",&n);
head=&a;
head=creatlist(n);
p=head->next;
printf("pleaseinputtheplacei:
\n");
scanf("%d",&i);
printf("nowthedatais:
");
head=ListDelete(head,i,e);
p=head->next;
while(p)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
}
#defineOVERFLOW0
#defineOK1
#defineNULL0
#defineERROR0
#include
#include
#include
typedefintElemType;
typedefstructLNode
{
ElemTypedata;
structLNode*next;
}LNode,*LinkList;
/*********单链表的建立**********/
LNode*creatlist(intn)
{
intx,k;
LNode*head,*r,*p;
p=(LNode*)malloc(sizeof(LNode));
head=p;
p->next=NULL;
r=p;
printf("请输入数据:
\n");
for(k=1;k<=n;k++)
{
scanf("%d",&x);
p=(LNode*)malloc(sizeof(LNode));
p->data=x;
p->next=NULL;
r->next=p;
r=r->next;
}
return(head);
}
/*******************插入****************/
LNode*ListInsert(LinkList&L,inti,ElemTypee)
{
LNode*p,*s,*head,*r;
intj=0;
head=L;
r=L;
p=L;
while(p&&j{
p=p->next;
++j;
}
if(!
p||j>i-1)
returnERROR;
s=(LinkList)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
return(head);
}
/*****************删除*****************/
LNode*ListDelete(LinkList&L,inti,ElemTypee)
{
LNode*p,*q;
intj=0;
p=L;
while(p->next&&j{
p=p->next;
++j;
}
if(!
(p->next)||j>i-1)
returnERROR;
q=p->next;
p->next=q->next;
e=q->data;
free(q);
returnL;
}
/************主函数**************/
voidmain()
{
LNode*head,*p,a;
intn,i,e,j;
printf("输入链表的长度:
\n");
scanf("%d",&n);
head=&a;
head=creatlist(n);
p=head->next;
printf("\n\n输入插入的位置i和插入的数e:
\n");
scanf("%d%d",&i,&e);
printf("插入后的数组是:
");
head=ListInsert(head,i,e);
p=head->next;
while(p)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
//head=creatlist(n);/********此句可去,但结果不同********/
printf("输入删除的位置i:
\n");
scanf("%d",&i);
printf("删除后的数组是:
");
head=ListDelete(head,i,e);
p=head->next;
while(p)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
}
#include
#include
typedefintelemtype;
typedefstructnode
{
elemtypedate;
structnode*next;
}linklist;
#defineNULL0
linklist*creatlist(intn)
{
intx,k;
linklist*head,*r,*p;
p=(linklist*)malloc(sizeof(linklist));
head=p;
p->next=NULL;
r=p;
for(k=1;k<=n;k++)
{
printf("inputvalue:
\n");
scanf("%d",&x);
p=(linklist*)malloc(sizeof(linklist));
p->date=x;
p->next=NULL;
r->next=p;
r=r->next;
}
return(head);
}
linklist*deletesamenode(linklist*h)
{
linklist*p,*q,*s;
p=h->next;
s=p;
while(p!
=NULL)
{
q=p->next;
while(q!
=NULL)
{
if(q->date!
=p->date)
{
s=q;
q=q->next;
}
else
{
s->next=q->next;
free(q);
q=s->next;
}
}
p=p->next;
}
return(h);
}
/*q=p->next;
while(p!
=NULL)
{
while(q!
=NULL)
{
if(q->date!
=p->date)
{
s=q;
q=q->next;
}
else
{
s->next=q->next;
free(q);
q=s->next;
}
}
p=p->next;
q=p->next;
}
return(h);
}*/
voidoutput(linklist*h)
{
linklist*p;
p=h->next;
while(p)
{
printf("%d",p->date);
p=p->next;
}
}
voidmain()
{
linklist*head;
intn;
printf("intputthelengthofthelist:
\n");
scanf("%d",&n);
head=creatlist(n);
printf("outputthelist:
\n");
output(head);
printf("删除链表中结点的重复值!
\n");
head=deletesamenode(head);
output(head);
}
#defineNULL0
#include
#include
typedefstructnode
{
intnum;
structnode*next;
}linklist;
voidoutput(linklist*head)
{
linklist*p;
p=head->next;
while(p!
=head)
{
printf("%d",p->num);
p=p->next;
}
printf("\n");
}
linklist*creat(intn)
{
intk;
linklist*head,*r,*p;
p=(linklist*)malloc(sizeof(linklist));
head=p;
r=p;
p->next=p;
for(k=1;k<=n;k++)
{
p=(linklist*)malloc(sizeof(linklist));
p->num=k;
r->next=p;
r=p;
}
p->next=head;
return(head);
}
linklist*invert(linklist*head)
{
linklist*p,*q,*r;
p=head->next;
q=head;
while(p!
=head)
{
r=q;
q=p;
p=p->next;
q->next=r;
}
head->next=q;
return(head);
}
voidmain()
{
intn;
linklist*head;
printf("intputhowmuchdateyouwant:
\n");
scanf("%d",&n);
printf("outputthedate:
\n");
output(head);
printf("nowgoing!
\n");
head=invert(head);
printf("outputthedate:
\n");
output(head);
}
#include
#include
typedefstructnode
{
intnum;
intcipher;
structnode*next;
}linklist;
linklist*creat(intn)
{
intk;
linklist*head,*r,*p;
p=(linklist*)malloc(sizeof(linklist));
head=p;
p->num=1;
printf("输入第1个人的密码:
\n");
scanf("%d",&p->cipher);
r=p;
p->next=p;
for(k=2;k<=n;k++)
{
p=(linklist*)malloc(sizeof(linklist));
p->num=k;
printf("输入第%d个人的密码:
\n",k);
scanf("%d",&p->cipher);
r->next=p;
r=p;
}
p->next=head;
return(head);
}
linklist(*select(linklist*head,intm))
{
linklist*p,*q;
intk;
q=head;
k=1;
p=q->next;
while(q!
=p)
{
++k;
if(k%m==0)
{
printf("出列的序号为:
%d\n",p->num);
m=p->cipher;
q->next=p->next;
free(p);
k=0;
p=q->next;
}
else
{
q=p;
p=q->next;
}
}
head=p;
return(head);
}
voidmain()
{
intn,m;
linklist*head;
printf("输入n:
\n");
scanf("%d",&n);
printf("输出要呼叫的数:
\n");
scanf("%d",&m);
head=creat(n);
head=select(head,m);
printf("最后一个数是:
%d\n",head->num);
}
#include
#include
#include
#defineNULL0
typedefintQElemType;
typedefstructQNode
{
QElemTypedata;
structQNode*next;
}QNode,*QueuePtr;
typedefstruct
{
QueuePtrfront;
QueuePtrrear;
}LinkQueue;
/******************插入****************/
voidEnQueue(LinkQueue*Q,QElemTypee)
{
QueuePtrp;
p=(QueuePtr)malloc(sizeof(QNode));
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
}
/**************初始化***************/
voidInitQueue(LinkQueue*q)
{
QueuePtrh;
inti,n,x;
printf("输入建立队列的元素个数:
n=");
scanf("%d",&n);
h=(QueuePtr)malloc(sizeof(QNode));
h->next=NULL;
q->front=h;
q->rear=h;
printf("依次输入队列的元素:
");
for(i=1;i<=n;i++)
{
scanf("%d",&x);
EnQueue(q,x);
}
}
/*****************删除******************/
QElemTypeDeQueue(LinkQueue*q)
{
QueuePtrp;
QElemTypex;
if(q->front==q->rear)
{
printf("队列为空");
x=0