编程题部分
1. 单连表的建立,把'a'--'z'26个字母插入到链表中,并且逆序,还要打印!
2. 写一个函数,它的原形是
int continumax(char *outputstr,char *intputstr);
功能:在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指内存。例如:"abcd12345ed125ss123456789"的首地址传给intputstr后,函数将返回9,outputstr所指的值为123456789;
3.编写自己的atoi函数;
1、
#include <stdio.h>
#include <malloc.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *head;
void SingleLinkListCreate()
{
head = (struct Node *)malloc(sizeof(struct Node));
head->next = NULL;
}
void SingleLinkListAdd(int data, int i)
{
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
struct Node *p = head;
while(i--)
{
if (p->next == NULL) exit(-1);
p = p->next;
}
ptr->next = p->next;
p->next = ptr;
ptr->data = data;
}void SingleLinkListDelete(int i)
{
struct Node *p = head;
struct Node *tmp;
while(i--)
{
if (p->next == NULL) exit(-1);
p = p->next;
}
tmp = p->next;
p->next = tmp->next;
free(tmp);
}
void SingleLinkListShow()
{
struct Node *ptr = head;
while(ptr->next)
{
ptr = ptr->next;
printf("%c\t",ptr->data);
}
}
void reverse()
{
struct Node *ptr;
struct Node *pre;
struct Node *next;
if (head->next == NULL || head->next->next ==NULL) return;
pre = head->next;
ptr = pre->next;
next = ptr->next;
pre->next = NULL;
while(next)
{
ptr->next = pre;
pre = ptr;
ptr = next;
next = next->next;
}
ptr->next = pre;
head->next = ptr;
}int main()
{
int i;
SingleLinkListCreate();
for(i = 0; i<26; i++)
{
SingleLinkListAdd(i+'a',i);
}
printf("原字符串:\n");
SingleLinkListShow();
reverse();
printf("\n逆序转换后:\n");
SingleLinkListShow();
return 0;
}
2、
#include <stdio.h>
#include <malloc.h>
int continumax(char *outputstr,char *inputstr)
{
int prelength = 0;
char *pretail = inputstr;
int length = 0;
char *head = inputstr;
char *pstr = inputstr;
int head_finded = 0;
int *dstStraddr = outputstr;
while (*pstr != '\0')
{
if (head_finded == 0)
{
if (*pstr >= '0' && *pstr <= '9')
{
head = pstr;
head_finded = 1;
length = 1;
}
}
else
{
if (*pstr >= '0' && *pstr <= '9')
{
length++;
}
else
{
if (length > prelength)
{
outputstr = head;
prelength = length;
pretail = pstr;
}
head_finded = 0;
}
}
pstr++;
}
if (length > prelength)
{
outputstr = head;
prelength = length;
pretail = pstr;
}
*dstStraddr = outputstr;
return length;
}
int main(void)
{
char *srcStr = "abcd12345ed125ss123456789";
char *dstStr;
dstStr = &dstStr;
int length = continumax(dstStr,srcStr);
printf("最长数字串长:%d\n最长数字串:\n", length);
while (length--)
{
printf("%c",*dstStr++);
}
return 0;
}
在这个题目中遇到了一个困难,即题目中的“outputstr所指的值为123456789;”,由于函数原型为“int continumax(char *outputstr,char *intputstr);”,所以题目就是说,要在指针作为函数参数的条件下修改指针所保存的地址,然而众所周知,形参的改变仅仅在函数内部,当函数调用结束后实参不会被改变。一般情况下,解决该问题的方法称为“传地址”,应用到本题中则是应该是将函数原型更改为“int continumax(char **outputstr,char *intputstr);”,显然与题意矛盾。百思而不得其解,忽而灵光一闪,有了如上解决办法:将指针的地址赋值给自己!这样就能修改形参了。
3、
#include <stdio.h>
int my_atoi(const char *nptr)
{
int result = 0;
int flag = 1;
if (*nptr == '-')
{
flag = -1;
nptr++;
}
else if (*nptr == '+')
{
nptr++;
}
while (*nptr != '\0')
{
if (*nptr > '9' || *nptr < '0')
{
break;
}
result = result * 10 + *nptr -'0';
nptr++;
}
return result * flag;
}
int main()
{
char *str = "+124324ab";
char *str2 = "-1234.567891";
printf("%d\n", my_atoi(str));
printf("%f\n", my_atof(str2));
printf("%d",atoi("-4afsf12r4-345f"));
return 0;
}
int main()
{
char *str = "+124324ab";
char *str2 = "-1234.567891";
printf("%d\n", my_atoi(str));
return 0;
}

本文通过三个编程实例详细介绍了单链表的基本操作、最长连续数字串的查找及自定义atoi函数的实现过程。首先,实现了单链表的创建、插入、删除和逆序显示等操作;其次,探讨了如何在字符串中寻找最长连续数字串并返回其长度;最后,提供了一个简单的atoi函数实现。
&spm=1001.2101.3001.5002&articleId=7737963&d=1&t=3&u=558fd84eba4043e5919842dbb3570469)
1511

被折叠的 条评论
为什么被折叠?



