永发信息网

内存不可读的链表。。运行时插入结点,内存不可读

答案:1  悬赏:30  手机版
解决时间 2021-08-20 14:35

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct node
{
int num;
char str[20];
struct node *next;
};

//创建链表函数
struct node *creat(node *)
{
struct node *head=NULL;
char temp[30];
struct node *p1,*p2;
p1=p2=(struct node*)malloc(sizeof(struct node));
printf ("input num,content:\n");
printf ("exit:double times enter!\n");
gets(temp);
gets(p1->str);
p1->num=atoi(temp);
p1->next=NULL;

while (strlen(p1->str)>0)
{
if (head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct node*)malloc(sizeof(struct node));
printf ("input num,content: \n");
printf ("exit:double times Enter!\n");
gets(temp);
gets(p1->str);
p1->num=atoi(temp);
p1->next=NULL;
}
return head;
}
//插入节点
struct node *insert(node*,char*,int )
{
struct node *head;
char *pstr;
int n;
struct node *p1,*p2,*p3;
p1=(struct node*)malloc(sizeof(struct node));
strcpy(p1->str,pstr);
p1->num=n;
p2=head;
if(head==NULL)
{
head=p1;
p1->next=NULL;
}
else
{
while (n>p2->num&&p2->next!=NULL)
{
{
p3=p2;
p2=p2->next;
}
if (n<=p2->num)
if (head==p2)
{
head=p1;
p1->next=p2;
}
else
{
p3->next=p1;
p1->next=p2;
}
else
{
p2->next=p1;
p1->next=NULL;
}
}
}
return (head);
}
//删除节点函数
struct node *deltree(node*,char*)
{
struct node *head;
char *pstr;
struct node *temp,*p;
temp=head;
if (head==NULL)
printf ("\nList is null!\n");
else
{
temp=head;
while (strcmp(temp->str,pstr)!=0&&temp->next!=NULL)
{
p=temp;
temp=temp->next;
}
if (strcmp(temp->str,pstr)==0)
{
if (temp==head)
{
head=head->next;
free(temp);
}
else
{
p->next=temp->next;
printf("delete string:%s\n",temp->str);
free(temp);
}
}
else
{printf("\nno find string!\n");}
}
return (head);
}
//链表输出
void print (struct node *head)
{
struct node *temp;
temp=head;
printf ("\n output strings:\n");
while (temp!=NULL)
{
printf ("\n%d----%s\n",temp->num,temp->str);
temp=temp->next;
}
return;
}


//主函数
int main ()
{
struct node *head;
char str[20];
int n;
head=NULL;
head=creat(head);
print(head);
printf ("\n input inserted num,content:\n");
gets (str);
n=atoi(str);
gets(str);
head=insert (head,str,n);
print(head);
printf("\n input deleted content:\n");
gets(str);
head=deltree(head,str);
//printf (head);
return 0;
}
。。。。。。。。。。。谁帮我改下哦~~

最佳答案

struct node *insert(node*,char*,int )
{
struct node *head;
char *pstr;
int n;
struct node *p1,*p2,*p3;
p1=(struct node*)malloc(sizeof(struct node));
strcpy(p1->str,pstr);
p1->num=n;
p2=head;
你说插入节点的时候有错误,那就主要改这一段,你这个函数的形参没有名字,这种没有名字的只有在声明的时候才可以用,你在函数实现的时候这么做是严重的错误,传递的参数就一定作用没有了.


typedef struct
{
int num;
char str[20];
struct node *next;
}node; //define the struct


node *insert(node * head,char*pstr,int n)
{
node *p1,*p2=head; //define the new struct
while(p2->next)
p2=p2->next; //扫描链表到尾部
p1=(node*)malloc(sizeof(node));//分配内存
strcpy(p1->str,pstr);//进行赋值
p1->num=n;
p2->next=p1;//插到链表尾部
p1->next=NULL;
}


我只写出了一部分,供你参考,你问的主要问题都在这里.我没有在插入的时候进行排序.

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
人最终目标是什么
曾多次网恋的人还会有瘾吗?
一个两位数的个位上数字比十位上数字小3,这个
我下学期高二,成绩一般,英语特差(听力基本靠
原阳县新乡新世纪商务温泉宾馆地址在哪,我要
求一篇“阿甘正传”的观后感500字
韩庚和吴尊到底谁更帅啊?
女人是不是善变的,或者是现实的,更或者的是
常考的歇后语,小学常考歇后语 小学常考歇后语
谁给我一个U5的官方网站,可以下载的
为何人的嘴唇一天到晚都是干干的,还有点死皮
表示安静的诗词,找一首第一次见面却印象很深
我全部答对了!为什么什么也没有奖励!
怎样开通支付宝功能
洛宁县洛阳罗岭乡财政所这个地址在什么地方,
推荐资讯
红河谷中文歌词
泌阳县驻马店泌阳县产科急救中心在什么地方啊
内在美和外在美哪个更中要
Dnf40级剑魂哪里能打到粉装备
开一个中西医药店,专门卖药都需要哪些证件
元神有什么作用?
杞县开封中国联通(吕屯合作营业厅)地址有谁知
心痛了,该吃什么药?
开完反后 要怎么在对局中使用 如题
湘阴县岳阳高岭社区卫生服务站地址有谁知道?
驱魔力法双休PK拿什么镰刀好?
曹云金单口相声台词,求一个单口相声剧本。越
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?