|
发表于 2023-11-28 19:31:48
From FishC Mobile
|
显示全部楼层
|阅读模式
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
typedef struct LINK
{
int date;
int item;
LINK* next;
}link,*Link;
void Creatlink(Link* head)//创建链表
{
if(*head==NULL)
{
*head=(Link)malloc(sizeof(Link));
(*head)->item=0;
(*head)->next=NULL;
}
int x;
int count;
Link p;
p=*head;
printf("请输入数据,输入到0为止");
do
{
scanf("%d",&x);
if(x==0)
{
p->next=NULL;
}
else
{
p->next=(Link)malloc(sizeof(Link));
}
p->date=x;
p->item=count++;
p=p->next;
}while(x);
}
void InsertList1(Link* head,int item,int rc)//向链表指定位置插入元素。
{
Link p,q;
p=*head;
for(;p->item==item;p=p->next);//
q=p->next;
p->next=(Link)malloc(sizeof(Link));
p->next->date=rc;
p->next->item=p->item;
p->next->next=q;
p=p->next;
for(;p;p=p->next)
{
p->item++;
}
}
void InsertList2(Link* head,int rc) //向有序链表指定位置插入元素。
{
Link p,q;
p=*head;
for(;p;p=p->next)
{
if(p->date==rc)
{
q=p->next;
p->next=(Link)malloc(sizeof(Link));
p->next->date=rc;
p->next->item=p->item;
p->next->next=q;
for(;p;p=p->next)
{
p->item++;
}
}
}
}
void DeleteList(Link* head,int item)//删除指定元素值的链表记录。
{
Link p,q;
p=*head;
for(;p->item==item;p=p->next);//
q=p;
p=p->next;
free(q);
for(;p;p=p->next)
{
p->item--;
}
}
int FindList(Link* head,int rc)//查找链表中的元素。
{
Link p;
p=*head;
for(;p;p=p->next)
{
if(p->date==rc)
return p->item;
}
}
void OutputList(Link* head)//输出链表元素。
{
Link p;
p=*head;
for(;p;p=p->next)
{
printf("%d ",p->date);
}
}
int main()
{
int a;
Link head;
Creatlink(&head);
InsertList1(&head,2,2);
InsertList2(&head,2);
DeleteList(&head,5);
a=FindList(&head,4);
printf("在%d\n",a);
OutputList(&head);
return 0;
}
在你的代码中有两个问题导致程序出错。首先是链表创建时的数据输入部分,在循环中你没有给p->next赋值为NULL,导致链表无法正确终止,从而进入了死循环。解决方法是将`p->next=NULL;`放到else语句的外面。
其次是在插入元素时,你使用了错误的数据类型,导致指针p没有正确指向Link类型的结构体。解决方法是将`LINK* next;` 改为 `struct LINK* next;`。
下面是修正后的代码: - #include <stdio.h>
- #include <stdlib.h>
- typedef struct LINK
- {
- int date;
- int item;
- struct LINK* next;
- } link, *Link;
- void Creatlink(Link* head)//创建链表
- {
- if(*head==NULL)
- {
- *head=(Link)malloc(sizeof(Link));
- (*head)->item=0;
- (*head)->next=NULL;
- }
- int x;
- int count = 1; // 初始化为1
- Link p;
- p=*head;
- printf("请输入数据,输入到0为止:");
- do
- {
- scanf("%d",&x);
- if(x==0)
- {
- p->next=NULL;
- }
- else
- {
- p->next=(Link)malloc(sizeof(Link));
- p->next->item=count++;
- }
- p->date=x;
- p=p->next;
- }while(x);
- }
- void InsertList1(Link* head,int item,int rc)//向链表指定位置插入元素。
- {
- Link p,q;
- p=*head;
- for(;p->item!=item;p=p->next);// 改为不等于
- q=p->next;
- p->next=(Link)malloc(sizeof(Link));
- p->next->date=rc;
- p->next->item=p->item;
- p->next->next=q;
- p=p->next;
- for(;p;p=p->next)
- {
- p->item++;
- }
- }
- void InsertList2(Link* head,int rc) //向有序链表指定位置插入元素。
- {
- Link p,q;
- p=*head;
- for(;p;p=p->next)
- {
- if(p->date==rc)
- {
- q=p->next;
- p->next=(Link)malloc(sizeof(Link));
- p->next->date=rc;
- p->next->item=p->item;
- p->next->next=q;
- for(;p;p=p->next)
- {
- p->item++;
- }
- }
- }
- }
- void DeleteList(Link* head,int item)//删除指定元素值的链表记录。
- {
- Link p,q;
- p=*head;
- for(;p->item==item;p=p->next);// 改为不等于
- q=p;
- p=p->next;
- free(q);
- for(;p;p=p->next)
- {
- p->item--;
- }
- }
- int FindList(Link* head,int rc)//查找链表中的元素。
- {
- Link p;
- p=*head;
- for(;p;p=p->next)
- {
- if(p->date==rc)
- return p->item;
- }
- return -1; // 如果没有找到,返回-1
- }
- void OutputList(Link* head)//输出链表元素。
- {
- Link p;
- p=*head;
- for(;p;p=p->next)
- {
- printf("%d ",p->date);
- }
- }
- int main()
- {
- int a;
- Link head = NULL;
- Creatlink(&head);
- InsertList1(&head,2,2);
- InsertList2(&head,2);
- DeleteList(&head,5);
- a=FindList(&head,4);
- printf("在%d\n",a);
- OutputList(&head);
- return 0;
- }
复制代码
这样,你应该可以正确地创建链表并进行其他操作了。如果还有其他问题,请随时提问。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
|
|