c语言中用链表实现数据创建,输入,输出,算出多少个数据,并删除想要删除的数据。。。求代码
2个回答
展开全部
算你运气好,我今天刚好写了一个
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h> //contain dynamic memory allocation function of head files
#define N 10
typedef struct node //establish a single table structure
{
char name[20];
struct node *link;
}stud;
stud *creat(int n) //creat a single table function
{
stud *p,*h,*s; //h save head node pointer,p point to the node before the current node,s point to the current node
int i;
if((h=(stud *)malloc(sizeof(stud)))==NULL) //allocate memory space and detect it
{
printf("memory allocate is failure,please deallocate!\n");
exit(0);
}
else
h->name[0]='\0'; //put the data space empty
h->link=NULL; //put the link space empty
p=h;
for(i=0;i<n;i++)
{
if((s=(stud *)malloc(sizeof(stud)))==NULL) //allocate memory space and detect it
{
printf("I am sorry,memory allocate is failure,please deallocate\n!");
exit(0);
}
else
p->link=s;
printf("please input the %d name :",i+1);
scanf("%s"悉册,s->name);
s->link=NULL;
p=s;
}
return h;
}
stud *search(stud *h,char *x) //a function of searching link,h is head pointing,x point to the name
{
stud *p; //point to current node
char *y;
p=h->link;
while(p!=NULL)
{
y=p->name;
if(strcmp(y,x)==0)
{
printf("yes,we find it!!!"山陆明);
return(p);
}
else
p=p->link;
}
if(p==NULL)
printf("can't find the name,please input again!\n");
}
stud *search2(stud *h,char *x)
{
stud *p,*s;
char *y;
p=h->link;
s=h;
while(p!=NULL)
{
y=p->name;
if(strcmp(y,x)==0)
{
printf("we find the node!!!\n");
return(s);
}
else
{
p=p->link;
s=s->link;
}
}
if(p==NULL)
printf("Don't find it!!!\n");
}
void del(stud *x,stud *y)
{
stud *s;
s=y;
x->link=y->逗告link;
free(s);
}
void insert(stud *p)
{
char stuname[20];
stud *s;
if((s=(stud *)malloc(sizeof(stud)))==NULL)
{
printf("can't allocate memory space,please try again!\n");
exit(0);
}
else
printf("please input what you want name!\n");
scanf("%s",stuname);
strcpy(s->name,stuname);
s->link=p->link;
p->link=s;
}
void main()
{
stud *head,*searchpoint,*forepoint;
char fullname[20];
head=creat(N);
printf("please input you want delete name:");
scanf("%s",fullname);
searchpoint=search(head,fullname);
forepoint=search2(head,fullname);
del(forepoint,searchpoint);
insert(searchpoint);
}
C语言代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h> //contain dynamic memory allocation function of head files
#define N 10
typedef struct node //establish a single table structure
{
char name[20];
struct node *link;
}stud;
stud *creat(int n) //creat a single table function
{
stud *p,*h,*s; //h save head node pointer,p point to the node before the current node,s point to the current node
int i;
if((h=(stud *)malloc(sizeof(stud)))==NULL) //allocate memory space and detect it
{
printf("memory allocate is failure,please deallocate!\n");
exit(0);
}
else
h->name[0]='\0'; //put the data space empty
h->link=NULL; //put the link space empty
p=h;
for(i=0;i<n;i++)
{
if((s=(stud *)malloc(sizeof(stud)))==NULL) //allocate memory space and detect it
{
printf("I am sorry,memory allocate is failure,please deallocate\n!");
exit(0);
}
else
p->link=s;
printf("please input the %d name :",i+1);
scanf("%s"悉册,s->name);
s->link=NULL;
p=s;
}
return h;
}
stud *search(stud *h,char *x) //a function of searching link,h is head pointing,x point to the name
{
stud *p; //point to current node
char *y;
p=h->link;
while(p!=NULL)
{
y=p->name;
if(strcmp(y,x)==0)
{
printf("yes,we find it!!!"山陆明);
return(p);
}
else
p=p->link;
}
if(p==NULL)
printf("can't find the name,please input again!\n");
}
stud *search2(stud *h,char *x)
{
stud *p,*s;
char *y;
p=h->link;
s=h;
while(p!=NULL)
{
y=p->name;
if(strcmp(y,x)==0)
{
printf("we find the node!!!\n");
return(s);
}
else
{
p=p->link;
s=s->link;
}
}
if(p==NULL)
printf("Don't find it!!!\n");
}
void del(stud *x,stud *y)
{
stud *s;
s=y;
x->link=y->逗告link;
free(s);
}
void insert(stud *p)
{
char stuname[20];
stud *s;
if((s=(stud *)malloc(sizeof(stud)))==NULL)
{
printf("can't allocate memory space,please try again!\n");
exit(0);
}
else
printf("please input what you want name!\n");
scanf("%s",stuname);
strcpy(s->name,stuname);
s->link=p->link;
p->link=s;
}
void main()
{
stud *head,*searchpoint,*forepoint;
char fullname[20];
head=creat(N);
printf("please input you want delete name:");
scanf("%s",fullname);
searchpoint=search(head,fullname);
forepoint=search2(head,fullname);
del(forepoint,searchpoint);
insert(searchpoint);
}
C语言代码
展开全部
算出蛮多个数据的、。
追问
printf("\ninput the inserted records:\n");
stu=(struct student *)malloc(sizeof(ST));
scanf("%d,%f",&stu->num,&stu->score);
while(stu->num!=0)
{
head=insert(head,stu);
printf(head);
printf("\ninput the inserted records:\n");
stu=(struct student *)malloc(sizeof(ST));
scanf("%1d,%f",&stu->num,&stu->score);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询