常见的C语言面试编程题.docx

上传人:b****6 文档编号:4488227 上传时间:2022-12-01 格式:DOCX 页数:18 大小:16.67KB
下载 相关 举报
常见的C语言面试编程题.docx_第1页
第1页 / 共18页
常见的C语言面试编程题.docx_第2页
第2页 / 共18页
常见的C语言面试编程题.docx_第3页
第3页 / 共18页
常见的C语言面试编程题.docx_第4页
第4页 / 共18页
常见的C语言面试编程题.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

常见的C语言面试编程题.docx

《常见的C语言面试编程题.docx》由会员分享,可在线阅读,更多相关《常见的C语言面试编程题.docx(18页珍藏版)》请在冰豆网上搜索。

常见的C语言面试编程题.docx

常见的C语言面试编程题

常见的C语言面试编程题

(1)求n的阶乘,这是一个比较简单的题目,有很多方法,但用递归方法是最简单的了:

#include#include

intmain()

{

longfactorial(longn);

longn;

scanf("%ld",&n);

printf("%ld",factorial(n));

return0;

}

longfactorial(longd)//求阶乘

{

longm;

if(d<0)

{

printf("d的阶乘不存在!

");

}

elseif(d==0||d==1)

{

m=1;

}

else

{

m=d*factorial(d-1);

}

returnm;

}

(2)从一个文件读取整数,对其进行排序,然后再将排序的结果输入到原来文件当中,这

是一个经常考的题目,即考你的文件操作,又考了排序,我在这里用的是选择排序

#include#include

intreadtoarray(int*a,FILE*fp)//从文件里将整数读到数组里{

inti=0;

if(fp==NULL)

{

exit(0);

}

while(fgetc(fp)!

=EOF)

{

fscanf(fp,"%d",&a[i]);

printf("%d\n",a[i]);

i++;

}

returni;

}

voidwritetofile(inta[],FILE*fp,inti)//将数组写到文件里去

{

intk=0;

if(fp==NULL)

{

exit(0);

}

while(k

{

fprintf(fp,"%c%d",'',a[k++]);

}

}

voidselectionSort(int*a,inti)//选择排序{

intm,n;

inttmp,min;

for(m=0;m

{

min=m;

for(n=m+1;n

{

if(a[n]

min=n;

}

tmp=a[m];

a[m]=a[min];

a[min]=tmp;

}

}

intmain()

{

FILE*fp,*fpwrite;

inti;

inta[10];

fp=fopen("2.txt","r");

i=readtoarray(a,fp);

fclose(fp);

selectionSort(a,i);

fpwrite=fopen("2.txt","w");

writetofile(a,fpwrite,i);

fclose(fpwrite);

return0;

}

1,单向链表的插入,删除,逆序操作

#include

#include

typedefstructNode{

intkey;

structNode*next;}*node;

nodenewNode(intk){

noden=(node)malloc(sizeof(node));

n->key=k;

n->next=NULL;

returnn;

}

voidprintlist(noden){

if(!

n)

{

printf("nisNULLlist\n");

}

while(n)

{

printf("%d",n->key);

printf("");

n=n->next;

}

printf("\n");

}

nodenewList()

{

intk;

nodehead=(node)malloc(sizeof(node));;

scanf("%d",&k);

if(k==0)

{

head=NULL;

returnhead;

}

else

{

noden=newNode(k);

head=n;

while(k)

{

scanf("%d",&k);

if(k!

=0)

{

noden1=newNode(k);

n->next=n1;

n=n->next;

}

}

n->next=NULL;

returnhead;

}

}

nodeinsertNode(noden,intp,intk)

{

noden1=newNode(k);

nodehead=(node)malloc(sizeof(node));

head=n;

if(head==NULL)

{

n1->next=head;

returnn1;

}

else

{

if(p==1)

{

n1->next=head;

head=n1;

returnhead;

}

else

{

inti=2;

while(i!

=p&&(n->next))

{

n=n->next;

i++;

}

if(n->next==NULL)

{

printf("thepcan'tbefound\n");

returnhead;

}

else

{

n1->next=n->next;

n->next=n1;

returnhead;

}

}

}

}

nodedeleteNode(noden,intk)

{

noden1=(node)malloc(sizeof(node));

nodehead=(node)malloc(sizeof(node));

head=n;

if(head==NULL)

{

printf("listisNULL\n");

returnhead;

}

else

{

if(head->key==k)

{

head=head->next;

returnhead;

}

while(n->key!

=k&&n->next)

{

n1=n;

n=n->next;

}

if(n==NULL)

{

printf("can'tfindthesamevalueaskinthislist\n");

returnhead;

}

else

{

n1->next=n->next;

n=NULL;

returnhead;

}

}

}

nodereverse(noden){

noden1[10];

nodehead=(node)malloc(sizeof(node));

noden2=(node)malloc(sizeof(node));

head=n;

if(head==NULL)

{

returnhead;

}

else

{

inti=0;

while(head!

=NULL)

{

n2=head;

head=head->next;

n2->next=NULL;

n1[i]=n2;

i++;

}

head=n1[i-1];

for(intj=i-1;j>0;j--)

{

n1[j]->next=n1[j-1];

}

returnhead;

}

}

intmain()

{

noden=newList();

printlist(n);

//插入操作

intk,p;

scanf("%d,%d",&p,&k);

nodenn=insertNode(n,p,k);

printlist(nn);

//删除操作

intq;

scanf("%d",&q);

nodend=deleteNode(nn,q);

printlist(nd);

//链表的倒置操作

nodem=reverse(n);

printlist(m);

return0;

}

2,双向链表的插入删除操作

#include#include

typedefstructNode{

intkey;

structNode*pre;

structNode*next;

}*node;

nodenewNode(inti){

noden=(node)malloc(sizeof(node));

n->key=i;

n->pre=NULL;

n->next=NULL;

returnn;

}

nodenewduplinklist(){

inti;

scanf("%d",&i);

noden;

if(i==0)

{

n=NULL;

returnn;

}

n=newNode(i);

nodehead=n;

intk=1;

while(k!

=0)

{

scanf("%d",&k);

if(k!

=0)

{

noden1=newNode(k);

n->next=n1;

n1->pre=n;

n=n1;

}

}

n->next=head;

head->pre=n;

returnhead;

}

intsizeduplinklist(noden)

{if(n==NULL)return0;

nodehead=n;

inti=1;

while(head->next!

=n)

{

head=head->next;

i++;

}

returni;

}

voidprint(noden){

if(n==NULL)

printf("此时链表为空!

");

else

{

printf("输出链表:

\n");

for(inti=0;i

{

printf("%-2d",n->key);

n=n->next;

}

printf("\n");

}

}

nodeinsertNode(noden){

intp,k;

printf("插入位置p:

\n");

scanf("%d,%d",&p,&k);

printf("\n");

if(p>sizeduplinklist(n))

{

printf("此位置超出链表的长度!

\n");

returnn;

}

elseif(p<1)

{

printf("此位置不存在!

\n");

returnn;

}

else

{

noden1,head,m;

m=newNode(k);

head=n;

if(p==1)

{

m->pre=n->pre;

n->pre->next=m;

m->next=n;

n->pre=m;

returnm;

}

else

{

inti=1;

while(i!

=p)

{

n1=n;

n=n->next;

i++;

}

n1->next=m;

m->pre=n1;

m->next=n;

n->pre=m;

returnhead;

}

}

}

nodedelNode(noden)

{

intp;

printf("删除位置p:

\n");

scanf("%d",&p);

printf("\n");

if(p>sizeduplinklist(n))

{

printf("此位置超出链表的长度!

\n");

returnn;

}

elseif(p<1)

{

printf("此位置不存在!

\n");

returnn;

}

else

{

noden1,head;

if(p==1)

{head=n->next;

n->pre->next=n->next;

n->next->pre=n->pre;

returnhead;

}

else

{

head=n;

inti=1;

while(i!

=p)

{

n1=n;

n=n->next;

i++;

}

n1->next=n->next;

n->next->pre=n1;

returnhead;

}

}

}

intmain()

{

noden=newduplinklist();

printf("%d\n",sizeduplinklist(n));

print(n);

nodem=insertNode(n);print(m);

nodem1=delNode(m);print(m1);

return0;

}

3,栈操作,用数组实现的包含出栈,入栈的操作

#include#include

typedefstructSta{

inta[20];

intnum;

}*sta;

staNullStack()

{

stas=(sta)malloc(sizeof(sta));

s->num=0;

returns;

}

stapushstack(stas,inti)

{

s->a[s->num]=i;

s->num=s->num+1;

returns;

}

stapopstack(stas){

if(s->num==0)

{

printf("stackisNULL\n");

returns;

}

s->num=s->num-1;

returns;

}

voidprintstack(stas){

if(s->num==0)

{

printf("stackisNULL\n");

}

else

{

for(inti=s->num-1;i>=0;i--)

{

printf("%d\n",s->a[i]);

}

}

}

intmain()

{

stas=NullStack();s=pushstack(s,1);s=pushstack(s,2);printstack(s);

s=popstack(s);

s=popstack(s);

printstack(s);

return0;

}

4,队列操作,类似于栈,

#include#include

typedefstructQue{

inta[20];

intnum;

}*que;

queNullQueue(){

queq=(que)malloc(sizeof(que));

q->num=0;

returnq;

}

queenq(queq,inti){

q->a[q->num]=i;

q->num=q->num+1;

returnq;

}

quedeq(queq)

{

if(q->num==0)

{

printf("queueisNULL\n");

returnq;

}

for(inti=1;inum;i++)

{

q->a[i-1]=q->a[i];

}

q->num=q->num-1;

returnq;

}

voidprintqueue(queq){

if(q->num==0)

{

printf("queueisNULL\n");

}

else

{

for(inti=q->num-1;i>=0;i--)

{

printf("%3d",q->a[i]);

}

}

}

intmain()

{

queq=NullQueue();q=enq(q,1);

q=enq(q,2);

q=enq(q,3);

q=enq(q,4);

printqueue(q);

printf("\n");

q=deq(q);

//printf("%d",s->a[0]);

//q=deq(q);

printqueue(q);

return0;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 英语

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1