数据结构 C语言
答案:1 悬赏:0 手机版
解决时间 2021-11-14 11:29
- 提问者网友:喧嚣尘世
- 2021-11-13 14:47
数据结构 C语言
最佳答案
- 五星知识达人网友:雪起风沙痕
- 2021-11-13 15:23
考察单向链表,题意描述不太明确,所以先建了一个空链表,然后接收输入并存储到链表。第3问程序里直接添加到了链表的最后,不过我觉得,应该添加到该大写字母对应的小写字母的后面,更能体现对链表的操作。希望题主能根据我的模板程序,自行完成该功能,增强对链表的理解。
#include
#include
#include
typedef struct mylink
{
char c;
struct mylink *next;
}mylink;
void append(mylink *p, char c)
{
mylink *data;
mylink *last;
last = p;
while (last->next != NULL)
last = last->next;
data = (mylink *)calloc(1, sizeof(mylink));
if (data == NULL)
{
printf("申请内存失败,程序出错!
");
return;
}
data->c = c;
data->next = NULL;
last->next = data;
}
void show(mylink p)
{
mylink *last;
last = p.next;
printf("当前链表内容为:
");
while (last != NULL)
{
printf("%c", last->c);
last = last->next;
}
printf("
");
}
int delete(mylink *p, int i)
{
mylink *cur, *prev;
int n;
prev = p;
cur = prev->next;
n = 1;
while (n != i)
{
prev = cur;
cur = cur->next;
if (cur == NULL)
{
printf("第%d个元素不存在,仅有%d个元素!
", i, n);
return -1;
}
n++;
}
prev->next = cur->next;
free(cur);
return 0;
}
int insert_after(mylink *p, int i, char c)
{
return 0;
}
int search(mylink p, char c)
{
return 0;
}
void freelink(mylink *p)
{
mylink *last, *cur;
last = p->next;
while (last != NULL)
{
cur = last;
free(cur);
last = last->next;
}
printf("
");
}
int main()
{
mylink p;
char c;
int i;
p.c = '0';
p.next = NULL;
printf("请顺序输入所有元素,回车符结束
");
while(1)
{
scanf("%c", &c);
if (c == '
') break;
append(&p, c);
}
show(p);
printf("请给定位置i(从1开始计算):
");
scanf("%d", &i);
scanf("%c", &c);
delete(&p, i);
show(p);
while(1)
{
printf("给定任意大写字母,回车符结束
");
scanf("%c", &c);
if ((c >= 'A') && (c <= 'Z')) break;
printf("输入错误,[%02X]不是大写字母,请重试
", c);
scanf("%c", &c);
}
append(&p, c);
i = search(p, c - 'A' + 'a');
if (i >= 0)
insert_after(&p, i, c);
else
printf("对应小写字母没有找到
");
show(p);
freelink(&p);
printf("程序结束
");
}
#include
#include
#include
typedef struct mylink
{
char c;
struct mylink *next;
}mylink;
void append(mylink *p, char c)
{
mylink *data;
mylink *last;
last = p;
while (last->next != NULL)
last = last->next;
data = (mylink *)calloc(1, sizeof(mylink));
if (data == NULL)
{
printf("申请内存失败,程序出错!
");
return;
}
data->c = c;
data->next = NULL;
last->next = data;
}
void show(mylink p)
{
mylink *last;
last = p.next;
printf("当前链表内容为:
");
while (last != NULL)
{
printf("%c", last->c);
last = last->next;
}
printf("
");
}
int delete(mylink *p, int i)
{
mylink *cur, *prev;
int n;
prev = p;
cur = prev->next;
n = 1;
while (n != i)
{
prev = cur;
cur = cur->next;
if (cur == NULL)
{
printf("第%d个元素不存在,仅有%d个元素!
", i, n);
return -1;
}
n++;
}
prev->next = cur->next;
free(cur);
return 0;
}
int insert_after(mylink *p, int i, char c)
{
return 0;
}
int search(mylink p, char c)
{
return 0;
}
void freelink(mylink *p)
{
mylink *last, *cur;
last = p->next;
while (last != NULL)
{
cur = last;
free(cur);
last = last->next;
}
printf("
");
}
int main()
{
mylink p;
char c;
int i;
p.c = '0';
p.next = NULL;
printf("请顺序输入所有元素,回车符结束
");
while(1)
{
scanf("%c", &c);
if (c == '
') break;
append(&p, c);
}
show(p);
printf("请给定位置i(从1开始计算):
");
scanf("%d", &i);
scanf("%c", &c);
delete(&p, i);
show(p);
while(1)
{
printf("给定任意大写字母,回车符结束
");
scanf("%c", &c);
if ((c >= 'A') && (c <= 'Z')) break;
printf("输入错误,[%02X]不是大写字母,请重试
", c);
scanf("%c", &c);
}
append(&p, c);
i = search(p, c - 'A' + 'a');
if (i >= 0)
insert_after(&p, i, c);
else
printf("对应小写字母没有找到
");
show(p);
freelink(&p);
printf("程序结束
");
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯