c语言链表问题,太折磨人了。求大神指点。
#include<stdio.h>#include<stdlib.h>#include<string.h>structstudent//定义结构体{intnum;char...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student //定义结构体
{
int num;
char name[10];
struct student *point;
};
/****************主函数*************************/
int main (int kiki,int *sisi[]) //主函数
{
void sclb (struct student *); //声明输出函数
struct student *cjlb (void); //声明创建链表
struct student *head; //接受头链表地址
struct student *czqd (struct student *,char *);
struct student *czjd (struct student *,char *);
void xxx (struct student *,struct student *);
struct student *s;
struct student *k;
char y[10];
head = cjlb (); //接收cjlb函数地址 //输出函数
printf ("请输入你要删除人物的姓名:");
scanf ("%s",y);
k = czjd (head,y); //接收查找结点
s = czqd (s,y); //接收先驱结点
xxx (s,k);
sclb (head);
return 0;
}
/********************创建链表*******************************/
struct student *cjlb (void) //创建链表函数
{
int i;
int n;
struct student *head,*end,*next;
head=(struct student *)malloc(sizeof(struct student));
if (head==NULL)
{
printf ("空间分配失败。");
exit (0);
}
head->name[0]='\0';
head->point=NULL;
end=head;
printf ("请输入学生个数:");
scanf ("%d",&n);
for (i=0;i<n;i++)
{
printf ("请输入第%d个学生的信息(学号,姓名):\n",i+1);
struct student *next=(struct student *)malloc(sizeof(struct student));
if (next==NULL)
{
printf ("空间分配失败。");
exit (0);
}
scanf ("%d%s",&next->num,&next->name);
end->point=next;
next->point=NULL;
end=next;
}
return head;
}
/****************输出链表*************************************/
void sclb (struct student *head)
{
struct student *p = head->point; //p=head
while (p!=NULL)
{
printf ("学号%d,姓名%s\n",p->num,p->name);
p=p->point;
}
}
/**************查找结点*************************/
struct student *czjd (struct student *h,char *x)
{
struct student *p;
char *y;
p=h->point;
while (p!=NULL)
{
y=p->name;
if (strcmp (y,x)==0)
return (p);
else
p=p->point;
}
if (p==NULL)
printf ("未查找到结点");
}
/**************************查找前结点***********************************/
struct student *czqd (struct student *h,char *x) //先驱结点,查找结点的前一个结点
{
struct student *p,*s;
char *y;
s=h;
p=h->point;
while (p!=NULL)
{
y=p->name;
if (strcmp (y,x)==0)
{
return (s);
}
else
{
s=s->point;
p=p->point;
}
}
if (p==NULL)
printf ("未找到结点");
}
/*********************删除结点**************************/
void xxx (struct student *x,struct student *y)
{
struct student *s;
s=y;
x->point=y->point;
free (s);
} 展开
#include <stdlib.h>
#include <string.h>
struct student //定义结构体
{
int num;
char name[10];
struct student *point;
};
/****************主函数*************************/
int main (int kiki,int *sisi[]) //主函数
{
void sclb (struct student *); //声明输出函数
struct student *cjlb (void); //声明创建链表
struct student *head; //接受头链表地址
struct student *czqd (struct student *,char *);
struct student *czjd (struct student *,char *);
void xxx (struct student *,struct student *);
struct student *s;
struct student *k;
char y[10];
head = cjlb (); //接收cjlb函数地址 //输出函数
printf ("请输入你要删除人物的姓名:");
scanf ("%s",y);
k = czjd (head,y); //接收查找结点
s = czqd (s,y); //接收先驱结点
xxx (s,k);
sclb (head);
return 0;
}
/********************创建链表*******************************/
struct student *cjlb (void) //创建链表函数
{
int i;
int n;
struct student *head,*end,*next;
head=(struct student *)malloc(sizeof(struct student));
if (head==NULL)
{
printf ("空间分配失败。");
exit (0);
}
head->name[0]='\0';
head->point=NULL;
end=head;
printf ("请输入学生个数:");
scanf ("%d",&n);
for (i=0;i<n;i++)
{
printf ("请输入第%d个学生的信息(学号,姓名):\n",i+1);
struct student *next=(struct student *)malloc(sizeof(struct student));
if (next==NULL)
{
printf ("空间分配失败。");
exit (0);
}
scanf ("%d%s",&next->num,&next->name);
end->point=next;
next->point=NULL;
end=next;
}
return head;
}
/****************输出链表*************************************/
void sclb (struct student *head)
{
struct student *p = head->point; //p=head
while (p!=NULL)
{
printf ("学号%d,姓名%s\n",p->num,p->name);
p=p->point;
}
}
/**************查找结点*************************/
struct student *czjd (struct student *h,char *x)
{
struct student *p;
char *y;
p=h->point;
while (p!=NULL)
{
y=p->name;
if (strcmp (y,x)==0)
return (p);
else
p=p->point;
}
if (p==NULL)
printf ("未查找到结点");
}
/**************************查找前结点***********************************/
struct student *czqd (struct student *h,char *x) //先驱结点,查找结点的前一个结点
{
struct student *p,*s;
char *y;
s=h;
p=h->point;
while (p!=NULL)
{
y=p->name;
if (strcmp (y,x)==0)
{
return (s);
}
else
{
s=s->point;
p=p->point;
}
}
if (p==NULL)
printf ("未找到结点");
}
/*********************删除结点**************************/
void xxx (struct student *x,struct student *y)
{
struct student *s;
s=y;
x->point=y->point;
free (s);
} 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询