原题出处:http://topic.youkuaiyun.com/u/20081011/15/9ee842e0-9c0d-4804-8376-42abdfe80698.html
1.编写函数,检查给定字符串是否整数,如果是,返回其整数值(注:不允许使用某种特定的库函数)。
2.有两个无序链表lsit1和list2,编写函数把list1和list2合并成一个递增的链表。
3.编写一段高效的代码,从排序的后的整数中找出指定整数n的个数。如找出[1,1,3,3,3,
5,5,5,9,9,9,9]中的5的个数(为3个)。
4. 编写函数实现字符串分割。SourceStr为源串,splitStr为分割字符串,SplitStr中的每一个字符在SourceStr被看作是分
割符。函数需要返回被分割符分割好的字符串链表。
例如: 如果
SourceStr为“this is pen ,that is a ball.”。
SplitStr 为“, .”
则返回的字符串链表每一项值分别为:“this” “is” “ pen”…
5.某银行有N个业务办理窗口,每一个窗口能办理各种业务, 银行客户分为VIP可与和普通客户,VIP客户有优先办理权,请你结合实际的应用场景为该银行设计排队机系统。
1)设计关键的数据结构和算法并用代码表示。
2)用自己的方式表达出系统的运转流程。
这里只做下第一题和题四题,第五题等以后有空再想想。
- /****************************************************************************
- 题目要求 :编写函数,检查给定字符串是否整数,如果是,返回其整数值(注:不允许使用某种特定的库函数)。
- 用C描述的算法,注释略
- ****************************************************************************/
- int is_int(const char *str, int *num)
- {
- char *p = (char *)str;
- int temp = 0;
- char sign = '+';
- if ( !str )
- {
- assert(0);
- return 0;
- }
- do
- {
- if ( *p == ' ' )
- {
- p++;
- }
- else
- {
- break;
- }
- } while( *p != '/0' );
- if ( *p == '-' || *p == '+' )
- {
- sign = *p++;
- }
- while ( *p != '/0' )
- {
- if ( (*p >= '0') && (*p <= '9') )
- {
- temp = temp * 10 + (*p - '0');
- }
- else
- {
- return 0;
- }
- p++;
- }
- *num = sign == '-' ? -temp : temp;
- return 1;
- }
- /****************************************************************************
- 题目要求 :编写函数实现字符串分割。SourceStr为源串,splitStr为分割字符串,SplitStr中的每一个字符在SourceStr被看作是分
- 割符。函数需要返回被分割符分割好的字符串链表。
- 例如: 如果
- SourceStr为“this is pen ,that is a ball.”。
- SplitStr 为“, .”
- 用C描述的算法,注释略
- ****************************************************************************/
- struct node{
- char *str;
- struct node *next;
- };
- node* split_string(const char *str, const char *token)
- {
- char *p = (char *)str;
- char *ch = (char *)token;
- char map[128];
- char *pos = p;
- char *substr = NULL;
- node l;
- node *plist = &l;
- node *pnode = NULL;
- int loop = 0;
- if ( str == NULL || token == NULL )
- {
- assert(0);
- return NULL;
- }
- l.next = NULL;
- while ( *ch != '/0' )
- {
- map[*ch] = *ch++;
- }
- while ( *p != '/0' )
- {
- if ( map[*p] == *p )
- {
- pnode = (node *)malloc(sizeof(node));
- substr = (char *)malloc(sizeof(char) * (p - pos + 1));
- while ( pos != p )
- {
- substr[loop++] = *pos++;
- }
- substr[loop] = '/0';
- pnode->next = NULL;
- pnode->str = substr;
- plist->next = pnode;
- plist = pnode;
- loop = 0;
- pos = ++p;
- }
- else
- {
- ++p;
- }
- }
- return l.next;
- }
- void print_list(node *l)
- {
- while ( l != NULL )
- {
- printf("%s|", l->str);
- l = l->next;
- }
- }
- void free_memory(node *l)
- {
- node *p = l;
- if ( l == NULL )
- {
- return;
- }
- do
- {
- p = l->next;
- free(l->str);
- free(l);
- l = p;
- } while( l != NULL );
- }
- /*两算法简单测试*/
- #include <stdio.h>
- #include <stdlib.h>
- int main(int argc, char* argv[])
- {
- int num = 0;
- char *strtest = " this is pen , that is a bal. ";
- node *l = NULL;
- is_int(" +51111", &num);
- printf("%d/n", num);
- cout << endl << endl;
- cout << strtest << endl;
- l = split_string(strtest, ", .");
- print_list(l);
- free_memory(l);
- l = NULL;
- system("pause");
- return 0;
- }