卫士通A卷笔试题2.docx
《卫士通A卷笔试题2.docx》由会员分享,可在线阅读,更多相关《卫士通A卷笔试题2.docx(11页珍藏版)》请在冰豆网上搜索。
卫士通A卷笔试题2
2014卫士通A卷笔试题
(2)
2.单链表反置。
1.structListNode
2.{
3.intm_nKey;
4.ListNode*m_pNext;
5.};
6.
7.#includestdafx.h
8.#include
9.#include
10.
11.usingnamespacestd;
12.
13.structListNode
14.{
15.intm_nKey;
16.ListNode*m_pNext;
17.};
18.
19.//构造链表
20.voidCreateList(ListNode*pHead)
21.{
22.fstreamfin(list.txt
23.ListNode*pNode=NULL;
24.ListNode*pTmp=NULL;
25.intdata;
26.findata;
27.while(data)
28.{
29.pNode=newListNode;
30.pNode-m_nKey=data;
31.pNode-m_pNext=NULL;
32.if(NULL==pHead)
33.{
34.pHead=pNode;
35.pTmp=pNode;
36.}
37.else
38.{
39.pTmp-m_pNext=pNode;
40.pTmp=pNode;
41.}
42.
43.findata;
44.}
45.}
46.
47.//翻转单链表
48.voidReverseLink(ListNode*pHead)
49.{
50.if(NULL==pHead)
51.{
52.return;
53.}
54.ListNode*pNode=pHead;
55.ListNode*Prev=NULL;
56.ListNode*pNext=NULL;
57.while(NULL!
=pNode)
58.{
59.pNext=pNode-m_pNext;
60.if(NULL==pNext)
61.{
62.pHead=pNode;
63.}
64.pNode-m_pNext=Prev;
65.Prev=pNode;
66.pNode=pNext;
67.}
68.}
69.
70.voidPrintList(ListNode*pHead)
71.{
72.if(NULL==pHead)
73.{
74.return;
75.}
76.ListNode*pNode=pHead;
77.while(NULL!
=pNode)
78.{
79.coutpNode-m_nKey
80.pNode=pNode-m_pNext;
81.}
82.coutendl;
83.}
84.
85.int_tmain(intargc,_TCHAR*argv[])
86.{
87.ListNode*pHead=NULL;
88.cout原来的链表:
89.CreateList(pHead);
90.PrintList(pHead);
91.ReverseLink(pHead);
92.cout翻转的链表:
93.PrintList(pHead);
94.
95.return0;
96.}
3.实现atoi函数
1.#includestdio.h
2.#includectype.h
3.#includestdlib.h
4.
5./*
6.Convertsacharacterstringintoanintorlong
7.将一个字符串转化为整数
8.*/
9.intmy_atoi(chars[])
10.{
11.inti,n,sign;
12.
13.for(i=0;isspace(s[i]);i++);//跳过空白
14.
15.sign=(s[i]==-)?
-1:
1;
16.if(s[i]==+||s[i]==-)//跳过符号位
17.i++;
18.for(n=0;isdigit(s[i]);i++)
19.n=10*n+(s[i]-0//将数字字符转换成整形数字
20.returnsign*n;
21.}
22.
23./*
24.Convertsanintorlongintoacharacterstring
25.将一个整数转化为字符串
26.*/
27.voidmy_itoa(intn,chars[])
28.{
29.inti,j,sign;
30.
31.if((sign=n)0)//记录符号
32.n=-n;//使n成为正数
33.i=0;
34.do{
35.s[i++]=n%10+0//取下一个数字
36.}while((n/=10)//循环相除
37.
38.if(sign0)
39.s[i++]=-
40.s[i]=\0
41.for(j=i-1;jj--)//生成的数字是逆序的,所以要逆序输出
42.printf(%c,s[j]);
43.}
44.
45.
46.voidmain()
47.{
48.intn;
49.charstr[100];
50.my_itoa(-123,str);
51.printf(\n
52.printf(%d\n,my_atoi(123));
53.system(pause
54.}
四、问答
1.你对卫士通的了解?
2.说出你胜任这份工作的理由?