您的位置:首页 > 职场人生

2道C编程面试题

2013-07-10 16:08 176 查看
1 将链表翻转比如1-2-3-4-5  翻转5-4-3-2-1
#include "slas_common.h"
/*
将链表翻转比如1-2-3-4-5  翻转5-4-3-2-1
*/

typedef struct _List_{
int num;
struct _List_ *next;
}List;

List *ListRev()
{
return NULL;
}

int AddNode(List **head, int num)
{
List *pnode;
pnode = *head;
if(!*head){
*head = (List *)malloc(sizeof(List));
(*head)->num = num;
(*head)->next = NULL;
}else{
while(pnode->next){
pnode = pnode->next;
}
pnode->next = (List *)malloc(sizeof(List));
pnode = pnode->next;
pnode->num = num;
pnode->next = NULL;
}
return 0;
}

int walklist(List *list)
{
List *pnode;
pnode = list;

while(pnode){
printf("pnode->num:%d\n", pnode->num);
pnode = pnode->next;
}

return 0;
}

/*1 2 3 4  5 -> 5 4 3 2 1*/
int main(int argc, char* argv[])
{
int i = 0;
List *list = NULL;
for(i = 1; i < 6; i++){
AddNode(&list, i);
}

walklist(list);

return 0;
}


2 从2个字符串中找到包含的最长的相同的字符串比如afaaahfasknjfasdhjj和fafaskfasdh最长的相同为fasdh

#include "slas_common.h"
/*
从2个字符串中找到包含的最长的相同的字符串
比如afaaahfasknjfasdhjj和fafaskfasdh
最长的相同为fasdh
*/

int fdmaxstr(char *mem, char *str){
char *p = NULL, *pmem = mem, *pstr = str, *pmax = NULL, *ppos = NULL;
int len = strlen(str);
int i = 0, max = 0, num = 0;
while(*pmem != '\0'){
pstr = str;
while(p = strchr(pmem, *pstr)){
ppos = p;
num = 0;
while(*p == *pstr){
p++;
pstr++;
num++;
}

if(num > max){
max = num;
pmax = ppos;
}

pstr++;
}
pmem++;
}

printf("pmax:%s, max:%d\n", pmax, max);

return 0;
}

int main(){
char mem[1024];
char str[100];
strcpy(mem, "afaaahfasknjfasdhjj");
strcpy(str, "fafaskfasdh");
fdmaxstr(mem, str);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: