C数据结构面试题 以及答案

时间:2022-07-13 14:03:56 面试 我要投稿
  • 相关推荐

C数据结构面试题 以及答案

做了这么多年java,做java偏向于应用,都用别人的插件和jar包。现在面临毕业,大公司技术笔试离不开数据结构,整理了下别人的题目。然后自己做了下。写这里留纪念,也希望对大家有帮助!
程序都在MinGW上跑通,VC我不知道。。
1.把一个链表反向
#include

C数据结构面试题 以及答案

typedef   struct   List
{   
int num;
struct List *next;
}test;
test *create_list()
{
test *head;
test *first;
test *temp =NULL;
first=head=new test;
for(int i=0;i<10;i++)
{
head->num=i;
temp=new test; //偷懒,用C++的new了,C用内存分配函数
head->next=temp;
temp->next=NULL;
head=temp;
}
return&#160;&#160; first;
}
void print(test *head)
{
while(head->next!=NULL)
{
printf("%d ",head->num);
head=head->next;
}
}
test *change_list(test *head)
{
test *temp,*sixer;
temp=head->next;
head->next=NULL;
while(temp->next!=NULL)
{
sixer=temp->next;
temp->next=head;
printf("%d-head-",temp->num);
head=temp;
temp=sixer;
printf("%d-temp-",temp->num);
}
temp->next=head;
return temp;
}

int&#160;&#160; main()
{
test *onelist;
onelist = create_list();
print(onelist);
onelist = change_list(onelist);
print(onelist);
}
2.&#160; 一个二叉树的三种遍历方法的输出结果
前序遍历,先根接点。中序,根左边的根右边的,例子:
abdgcefh,中序遍历访问顺序是dgbaechf,则其后续遍历的结点访问顺序是
a为根,dgb为左子树,echf为右子树
接下来看左子树的前序遍历为bdg
b首先被访问
可以知道b为左子树的根,与a相连
再看左子树的中序遍历dgb
d和g都在b之前就被访问
所以b和g应该在b的左子树上
而dg的确定再根据前序遍历
d先被访问
则d为根
再看中序遍历也是d先被访问
可以确定g为d的右子树
左边就可以确定出来了
如果上面看懂了
右边就很简单,一样的道理
前序遍历cefh
确定c为右子树的根
再看中序遍历echf
e为c的左子树,hf为c的右子树
hf的确定在看前序遍历f先被访问
f为根
中序遍历h先被访问
h为f的左子树
整棵树就出来了
3.
希表和数组的定义,区别,优缺点

百度
4.递归的折半查找算法
#include

using namespace std;
void creat(int a[])
{
for(int i=0;i<10;i++){
a[i]=i;
}
}
void print(int a[])
{
for(int i=0;i<10;i++){
printf("%d-",a[i]);
}
}
int Search(int a[],int key,int left ,int right)
{
int mid;&#160;
while(left<=right){&#160;
mid = (left+right)/2;&#160;
if(key==a[mid]){&#160;
return mid;&#160;
}&#160;
else if(keyright = mid-1;&#160;
return Search(a,key,left,right);&#160;
}&#160;
else{&#160;
left = mid+1;&#160;
return Search(a,key,left,right);&#160;
}&#160;
}&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
return -1;&#160;&#160;
}

int main()
{
int a[10];
int i;
creat(a);
print(a);
int key;
cin>>key;
int left = 0;&#160;
int right = 10;&#160;
i=Search(a,key,left,right);
printf("%d->%d",i,a[i]);
return 1;
}
5.sizeof()和strlen()的使用.
sizeof比特字节数长度。strlen长度比如char a[2]={1,2} 吗sizeof是2,strlen是3包括了/0而且sizeof是可以测int a[]这种的int a[2]={1,2} 的sizeof是8.2个4字节int。

【C数据结构面试题 以及答案】相关文章:

经典C/C++面试题07-11

c面试题08-04

上海交通大学软件工程专业C、C++、数据结构、电路系统以及离散结构的课件07-09

华为C++/MFC面试题07-11

软件测试面试题及答案12-30

某公司面试题及答案07-11

元宵节的灯谜以及答案08-03

已考面试题求答案07-11

东坡画扇阅读原文以及答案08-04

华为面试题C语言软件工程师07-11