常见的C语言面试编程题.doc_第1页
常见的C语言面试编程题.doc_第2页
常见的C语言面试编程题.doc_第3页
常见的C语言面试编程题.doc_第4页
常见的C语言面试编程题.doc_第5页
免费预览已结束,剩余10页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

(1) 求n的阶乘,这是一个比较简单的题目,有很多方法,但用递归方法是最简单的了:#include #include int main() long factorial(long n); long n; scanf(%ld,&n); printf(%ld,factorial(n); return 0;long factorial(long d)/求阶乘 long m; if(d0) printf(d的阶乘不存在!); else if(d=0|d=1) m=1; else m=d*factorial(d-1); return m;(2)从一个文件读取整数,对其进行排序,然后再将排序的结果输入到原来文件当中,这是一个经常考的题目,即考你的文件操作,又考了排序,我在这里用的是选择排序#include #include int readtoarray(int *a,FILE *fp)/从文件里将整数读到数组里 int i=0; if(fp=NULL) exit(0); while(fgetc(fp)!=EOF) fscanf(fp,%d,&ai); printf(%dn,ai); i+; return i;void writetofile(int a,FILE *fp,int i)/将数组写到文件里去 int k = 0; if(fp=NULL) exit(0); while(ki) fprintf(fp,%c%d, ,ak+); void selectionSort(int *a,int i)/选择排序 int m,n; int tmp,min; for(m=0;m i-1;m+) min=m; for(n=m+1;n i;n+) if(anamin) min=n; tmp=am; am=amin; amin=tmp; int main() FILE* fp,* fpwrite; int i; int a10; 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); return 0;1,单向链表的插入,删除,逆序操作#include #include typedef struct Node int key; struct Node* next;* node;node newNode(int k) node n=(node)malloc(sizeof(node); n-key=k; n-next=NULL; return n;void printlist(node n) if(!n) printf(n is NULL listn); while(n) printf(%d,n-key); printf( ); n=n-next; printf(n);node newList() int k; node head=(node)malloc(sizeof(node); scanf(%d,&k); if(k=0) head=NULL; return head; else node n=newNode(k); head=n; while(k) scanf(%d,&k); if(k!=0) node n1=newNode(k); n-next=n1; n=n-next; n-next=NULL; return head; node insertNode(node n,int p,int k) node n1=newNode(k); node head=(node)malloc(sizeof(node); head=n; if(head=NULL) n1-next=head; return n1; else if(p=1) n1-next=head; head=n1; return head; else int i=2; while(i!=p&(n-next) n=n-next; i+; if(n-next=NULL) printf(the p cant be foundn); return head; else n1-next=n-next; n-next=n1; return head; node deleteNode(node n,int k) node n1=(node)malloc(sizeof(node); node head=(node)malloc(sizeof(node); head=n; if(head=NULL) printf(list is NULLn); return head; else if(head-key=k) head=head-next; return head; while(n-key!=k&n-next) n1=n; n=n-next; if(n=NULL) printf(cant find the same value as k in this listn); return head; else n1-next=n-next; n=NULL; return head; node reverse(node n) node n110; node head=(node)malloc(sizeof(node); node n2=(node)malloc(sizeof(node); head=n; if(head=NULL) return head; else int i=0; while(head!=NULL) n2=head; head=head-next; n2-next=NULL; n1i=n2; i+; head=n1i-1; for(int j=i-1;j0;j-) n1j-next=n1j-1; return head; int main() node n=newList(); printlist(n); /插入操作 int k,p; scanf(%d,%d,&p,&k); node nn=insertNode(n,p,k); printlist(nn); /删除操作 int q; scanf(%d,&q); node nd=deleteNode(nn,q); printlist(nd); /链表的倒置操作 node m=reverse(n); printlist(m); return 0;2,双向链表的插入删除操作#include #include typedef struct Node int key; struct Node* pre; struct Node* next;* node;node newNode(int i) node n=(node)malloc(sizeof(node); n-key=i; n-pre=NULL; n-next=NULL; return n;node newduplinklist() int i; scanf(%d,&i); node n; if(i=0) n=NULL; return n; n=newNode(i); node head=n; int k=1; while(k!=0) scanf(%d,&k); if(k!=0) node n1=newNode(k); n-next=n1; n1-pre=n; n=n1; n-next=head; head-pre=n; return head;int sizeduplinklist(node n) if(n=NULL) return 0; node head=n; int i=1; while(head-next!=n) head=head-next; i+; return i;void print(node n) if(n=NULL) printf(此时链表为空!); else printf(输出链表:n); for(int i=0;ikey); n=n-next; printf(n); node insertNode(node n) int p,k; printf(插入位置p:n); scanf(%d,%d,&p,&k); printf(n); if(psizeduplinklist(n) printf(此位置超出链表的长度!n); return n; else if(ppre=n-pre; n-pre-next=m; m-next=n; n-pre=m; return m; else int i=1; while(i!=p) n1=n; n=n-next; i+; n1-next=m; m-pre=n1; m-next=n; n-pre=m; return head; node delNode(node n) int p; printf(删除位置p:n); scanf(%d,&p); printf(n); if(psizeduplinklist(n) printf(此位置超出链表的长度!n); return n; else if(pnext; n-pre-next=n-next; n-next-pre=n-pre; return head; else head=n; int i=1; while(i!=p) n1=n; n=n-next; i+; n1-next=n-next; n-next-pre=n1; return head; int main()node n=newduplinklist();printf(%dn,sizeduplinklist(n);print(n);node m=insertNode(n);print(m);node m1=delNode(m);print(m1);return 0;3,栈操作,用数组实现的包含出栈,入栈的操作#include #include typedef struct Sta int a20; int num;* sta;sta NullStack() sta s=(sta)malloc(sizeof(sta); s-num=0; return s;sta pushstack(sta s,int i) s-as-num=i; s-num=s-num+1; return s;sta popstack(sta s) if(s-num=0) printf(stack is NULLn); return s; s-num=s-num-1; return s;void printstack(sta s) if(s-num=0) printf(stack is NULLn); else for(int i=s-num-1; i=0;i-) printf(%dn,s-ai); int main()sta s=NullStack();s=pushstack(s,1);s=pushstack(s,2);printstack(s);s=popstack(s);s=popstack(s);printstack(s);return 0;4,队列操作,类似于栈,#include #include typedef struct Que int a20; int num;* que;que NullQueue() que q=(que)malloc(sizeof(que); q-num=0; return q;que enq(que q,int i) q-aq-num=i; q-num=q-num+1; return q;que deq(que q) if(q-num=0) printf(queue is NULLn); return q; for(int i=1;inum;i+) q-ai-1=q-ai; q-num=q-num-1; return q;voi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论