常见的C语言面试编程题Word文档格式.docx
《常见的C语言面试编程题Word文档格式.docx》由会员分享,可在线阅读,更多相关《常见的C语言面试编程题Word文档格式.docx(18页珍藏版)》请在冰豆网上搜索。
(2)从一个文件读取整数,对其进行排序,然后再将排序的结果输入到原来文件当中,这
是一个经常考的题目,即考你的文件操作,又考了排序,我在这里用的是选择排序
intreadtoarray(int*a,FILE*fp)//从文件里将整数读到数组里{
inti=0;
if(fp==NULL)
exit(0);
while(fgetc(fp)!
=EOF)
fscanf(fp,"
%d"
a[i]);
%d\n"
a[i]);
i++;
returni;
voidwritetofile(inta[],FILE*fp,inti)//将数组写到文件里去
intk=0;
while(k<
i)
fprintf(fp,"
%c%d"
'
'
a[k++]);
voidselectionSort(int*a,inti)//选择排序{
intm,n;
inttmp,min;
for(m=0;
m<
i-1;
m++)
min=m;
for(n=m+1;
n<
i;
n++)
if(a[n]<
a[min])
min=n;
tmp=a[m];
a[m]=a[min];
a[min]=tmp;
FILE*fp,*fpwrite;
inti;
inta[10];
fp=fopen("
2.txt"
"
r"
i=readtoarray(a,fp);
fclose(fp);
selectionSort(a,i);
fpwrite=fopen("
w"
writetofile(a,fpwrite,i);
fclose(fpwrite);
1,单向链表的插入,删除,逆序操作
typedefstructNode{
intkey;
structNode*next;
}*node;
nodenewNode(intk){
noden=(node)malloc(sizeof(node));
n->
key=k;
next=NULL;
returnn;
voidprintlist(noden){
if(!
n)
nisNULLlist\n"
while(n)
n->
key);
"
n=n->
next;
\n"
nodenewList()
intk;
nodehead=(node)malloc(sizeof(node));
;
k);
if(k==0)
head=NULL;
returnhead;
noden=newNode(k);
head=n;
while(k)
if(k!
=0)
noden1=newNode(k);
next=n1;
nodeinsertNode(noden,intp,intk)
if(head==NULL)
n1->
next=head;
returnn1;
if(p==1)
head=n1;
inti=2;
while(i!
=p&
&
(n->
next))
if(n->
next==NULL)
thepcan'
tbefound\n"
next=n->
nodedeleteNode(noden,intk)
noden1=(node)malloc(sizeof(node));
listisNULL\n"
if(head->
key==k)
head=head->
while(n->
key!
=k&
next)
n1=n;
if(n==NULL)
can'
tfindthesamevalueaskinthislist\n"
n=NULL;
nodereverse(noden){
noden1[10];
noden2=(node)malloc(sizeof(node));
while(head!
=NULL)
n2=head;
n2->
n1[i]=n2;
head=n1[i-1];
for(intj=i-1;
j>
0;
j--)
n1[j]->
next=n1[j-1];
noden=newList();
printlist(n);
//插入操作
intk,p;
%d,%d"
p,&
nodenn=insertNode(n,p,k);
printlist(nn);
//删除操作
intq;
q);
nodend=deleteNode(nn,q);
printlist(nd);
//链表的倒置操作
nodem=reverse(n);
printlist(m);
2,双向链表的插入删除操作
structNode*pre;
}*node;
nodenewNode(inti){
key=i;
pre=NULL;
nodenewduplinklist(){
i);
noden;
if(i==0)
n=newNode(i);
nodehead=n;
intk=1;
while(k!
pre=n;
n=n1;
head->
intsizeduplinklist(noden)
{if(n==NULL)return0;
inti=1;
while(head->
next!
=n)
voidprint(noden){
此时链表为空!
输出链表:
for(inti=0;
i<
sizeduplinklist(n);
i++)
%-2d"
nodeinsertNode(noden){
intp,k;
插入位置p:
if(p>
sizeduplinklist(n))
此位置超出链表的长度!
elseif(p<
1)
此位置不存在!
noden1,head,m;
m=newNode(k);
m->
pre=n->
pre;
pre->
next=m;
next=n;
pre=m;
=p)
pre=n1;
nodedelNode(noden)
intp;
删除位置p:
p);
noden1,head;
{head=n->
next->
noden=newduplinklist();
sizeduplinklist(n));
print(n);
nodem=insertNode(n);
print(m);
nodem1=delNode(m);
print(m1);
3,栈操作,用数组实现的包含出栈,入栈的操作
typedefstructSta{
inta[20];
intnum;
}*sta;
staNullStack()
stas=(sta)malloc(sizeof(sta));
s->
num=0;
returns;
stapushstack(stas,inti)
a[s->
num]=i;
num=s->
num+1;
stapopstack(stas){
if(s->
num==0)
stackisNULL\n"
num-1;
voidprintstack(stas){
for(inti=s->
i>
=0;
i--)
s->
stas=NullStack();
s=pushstack(s,1);
s=pushstack(s,2);
printstack(s);
s=popstack(s);
printstack(s);
4,队列操作,类似于栈,
typedefstructQue{
}*que;
queNullQueue(){
queq=(que)malloc(sizeof(que));
q->
returnq;
queenq(queq,inti){
a[q->
num=q->
quedeq(queq)
if(q->
queueisNULL\n"
for(inti=1;
num;
a[i-1]=q->
a[i];
voidprintqueue(queq){
for(inti=q->
%3d"
q->
queq=NullQueue();
q=enq(q,1);
q=enq(q,2);
q=enq(q,3);
q=enq(q,4);
printqueue(q);
q=deq(q);
//printf("
a[0]);
//q=deq(q);