69,382
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
#define type struct stu
#define LEN sizeof(struct stu)
struct stu
{
int num;
char sex;
struct stu *next;
};
type *list(int n)
{
int i;
struct stu *head,*q,*p;
for(i=0;i<n;i++)
{
q=(type*)malloc(LEN);
scanf("%d%c",&q->num,&q->sex);
if(i==0)
p=head=q;
else
p->next=q;
q->next=NULL;
p=q;
}
head->next=NULL;
return (head);
}
main()
{
int i,conut=6;
struct stu *student;
student=list(conut);
printf("num\tsex\n");
for(i=0;i<conut;i++)
{
student=student->next;
printf("%d\t%c",student->num,student->sex);
}
}
for(i=0;i<n;i++)
{
q=(type*)malloc(LEN);
scanf("%d %c",&q->num,&q->sex);//留个空格
if(i==0)
p=head=q;
else
{
p->next=q;
//q->next=NULL;
p=q;
}
}
p->next=NULL; //head->next=NULL改为p->next=NULL
return (head);
}
//这是一个有添加和删除的小程序,供参考
#include<stdio.h>
#include<malloc.h>
#define N 3
struct student *cj();
void disp(struct student *head);
void tj(struct student *head,int i);
void del(struct student *head,int i);
struct student{
int xuehao;
char name[10];
struct student *next;
};
void main()
{
struct student *head;
int i;
head=cj();
disp(head);
printf("请输入要添加的位置");
scanf("%d",&i);
tj(head,i);
disp(head);
printf("请输入要删除的位置");
scanf("%d",&i);
del(head,i);
disp(head);
}
struct student *cj()
{
struct student *head,*pnew,*p;
int i=0,k;
head=(struct student *)malloc(sizeof(struct student));
head->next=NULL;
p=head;
if(head==NULL)
return NULL;
while(i<3)
{
printf("input xuehao");
scanf("%d",&k);
getchar();
i++;
pnew=(struct student *)malloc(sizeof(struct student));
if(pnew==NULL)
return NULL;
pnew->xuehao=k;
printf("input name");
gets(pnew->name);
p->next=pnew;
p=pnew;
}
p->next=NULL;
return head;
}
void disp(struct student *head)
{
struct student *p=head->next;
while(p!=NULL)
{
printf("%d %s\n",p->xuehao,p->name);
p=p->next;
}
}
void tj(struct student *head,int i)
{
int j=0,k;
struct student *p=head, *s;
struct student *pnew;
while(j<i&&p)
{s=p;
p=p->next;
j++;
}
pnew=(struct student *)malloc(sizeof(struct student));
if(pnew==NULL)
return;
printf("input xuehao");
scanf("%d",&k);
getchar();
pnew->xuehao=k;
printf("input name");
gets(pnew->name);
if (p)
{
pnew->next=p->next;
p->next=pnew;
}
else
{
s->next=pnew;
pnew->next=NULL;
}
}
void del(struct student *head,int i)
{
struct student *p=head->next;
struct student *d, *r;
int j=1;
if (head==NULL||head->next==NULL)
{
return ;
}
if (i==0||i==1)
{
d=head->next;
head->next=head->next->next;
free(d);
}
else
{
r=head;
while(j<i&&p)
{
r=p;
p=p->next;
j++;
}
if (p)
{
d=p;
r->next=p->next;
free(d);
}
else
{
printf("node dose not exist\n");
}
}
}
#include<stdio.h>
#include<stdlib.h>
#define type struct stu
#define LEN sizeof(struct stu)
struct stu
{
int num;
char sex;
struct stu *next;
};
type *list(int n)
{
int i;
struct stu *head,*q,*p;
for(i=0;i<n;i++)
{
q=(type*)malloc(LEN);
scanf("%d %c",&q->num,&q->sex);//留个空格
if(i==0)
p=head=q;
else
{
p->next=q;
//q->next=NULL;
p=q;
}
}
p->next=NULL;
return (head);
}
main()
{
int i,conut=6;
struct stu *student;
student=list(conut);
printf("num\tsex\n");
for(i=0;i<conut;i++)
{
//student=student->next;
printf("%d\t%c\n",student->num,student->sex);
student=student->next;
}
}