数据结构与算法面试题集锦

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.链表和数组的区别在哪里?
链表是一种常见的数据组织形式,它采用动态分配内存的形式实现。需要时可以用new分配内存空间,不需要时用delete将已分配的空间释放,不会造成内存空间的浪费。

A 从逻辑结构来看

A-1. 数组必须事先定义固定的长度(元素个数),不能适应数据动态地增减的情况。当数据增加时,可能超出原先定义的元素个数;当数据减少时,造成内存浪费。

A-2. 链表动态地进行存储分配,可以适应数据动态地增减的情况,且可以方便地插入、删除数据项。(数组中插入、删除数据项时,需要移动其它数据项)


2.编写实现链表排序的一种算法。说明为什么你会选择用这样的方法?

3.编写实现数组排序的一种算法。说明为什么你会选择用这样的方法?

4.请编写能直接实现strstr()函数功能的代码。
#include
#include
char *strstr(char *haystack, char *needle)
{
char *pChar;
pChar = needle;
int len = strlen(needle);
for(; strchr(haystack,*pChar) != '\0'; haystack++)
{
if( strncmp(haystack,pChar,len) == 0)
{
return haystack;
}
}
return NULL;
}

int main()
{
char s[20]="abcdcdef",s1[20]="cd";
char *p;
p = strstr(s,s1);
cout<return 0;
}

5.编写反转字符串的程序,要求优化速度、优化空间。
static void reverse(char [] s){
int begin = 0;
int end = s.length-1;
while(begin+char c = s[begin];
s[begin] = s[end];
s[end]= c;
}
}
--
#include
#include
using namespace std;
char * reverse(char *s)
{
int len = strlen(s);
char *p = s;
char *q = s + len - 1;
while(q - p > 0)
{
char tmp = *p;
*p = *q;
*q = tmp;
p++;
q--;
}
return s;
}
int main()
{
char s[] = "ABCDEFG";
cout << reverse(s) << endl;
return 0;
}
6.在链表里如何发现循环链接?
定义两个指针, 一个每次前进两个节点, 一个前进一个, 如果慢的能追上快的就有环.
#include
#include

typedef struct Node
{
int data;
struct Node* next;
}node;
node* CreateLinkList(const int *ptr,const int len,bool circle)
{
node* p, *q;
node* head = NULL;
for(int i = 0; i < len; i++)
{
p = (node*) malloc(sizeof(node));
p->data

= ptr[i];
p->next = NULL;
if(!head)
{
head = p;
q = p;
}
else
{
q->next = p;
q = p;
}
}
if(circle)
{
p ->next = head;
}
return head;
};
int ClearLinkList(node* ptr, int len)
{
node* p;
for(int i = 0; i < len; i++)
{
p = ptr;
if(ptr->next)
{
ptr = ptr->next;
}
free(p);
}
return 1;
};
bool searchLinkList(const node* p, const node* q)
{
while(q->next)
{
q = q->next;
if(q == p)
{
printf("It's a circle linklist!\n");
return true;
}
}
printf("It's not a circle linklist!\n");
return false;
}
void main()
{
int arrLen = 20; //数组长度(实际),用于形成测试链表;
node* p, *q;
int array[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
p = q = CreateLinkList(array,arrLen,1); // 设定测试链表,1为循环,0为不循环;
searchLinkList(p,q); //测试;
ClearLinkList(p,arrLen); //清理内存
}

7.给出洗牌的一个算法,并将洗好的牌存储在一个整形数组里。

8.写一个函数,检查字符是否是整数,如果是,返回其整数值。(或者:怎样只用4行代码编写出一个从字符串到长整形的函数?)

9.给出一个函数来输出一个字符串的所有排列。
#include
#include
#include
#include
#include
#include
using namespace std;

char *p = "abcde";
vector st;

/**找index以后的、没有在st中的数。
*如果不存在则返回-1
*/
int nextVal(int index)
{
int len = strlen(p);
vector::iterator it;
for(int i = (index + 1); i < len; i++ )
{
for(it = st.begin(); it < st.end(); it++)
{
if(p[*(it)] == p[i]) break;
}
if(it >= st.end())
{
return i;
}
}
return -1;
}

/**
*z在屏幕上输出vector st的内容
*/
void printVector()
{
vector::iterator it;
for(it = st.begin(); it < st.end(); it++)
{
cout <<*it << " ";
}
cout << endl;
}

/**
*输出p字符串中的字符的排列
*/
void arrange(char *p)
{
assert(p != NULL);
int len = strlen(p);
int val;
int ret;
int temp;
int temp2;
int count = 0;
while(true)
{
temp = (len - st.size());
for(int i = 0; i < temp; i++)
{
temp2 = nextVal(-1);
st.push_back(temp2);

}//end of for

printVector();
cout << "the count is" << ++count << endl;

while(st.size() != 0)
{
val = *(st.end()-1);
st.pop_back();
ret = nextVal(val);
if(ret != -1)
{

st.push_back(ret);
break;
}
else
{
}
}//end of while

if(st.size() ==0)
{
break;
}
}//end of while
}//end of arrange

int main()
{
arrange(p);
return 1;
}

本文来自CSDN博客,转载请标明出处:/xuyongfeng/archive/2005/09/06/473163.aspx
10.请编写实现malloc()内存分配函数功能一样的代码。

11.给出一个函数来复制两个字符串A和B。字符串A的后几个字节和字符串B的前几个字节重叠。

12.怎样编写一个程序,把一个有序整数数组放到二叉树中?

13.怎样从顶部开始逐层打印二叉树结点数据?请编程。

14.怎样把一个链表掉个顺序(也就是反序,注意链表的边界条件并考虑空链表)?

void Reverse(Lnode *head)
{
Lnode * p;
Lnode * q;
p=head-> next;
head-> next=NULL;
while(p!=NULL)
{
q=p-> next;
p-> next=head-> next;
head-> next=p;
p=q;
}
}





相关文档
最新文档