结构体指针问题
把if(i=1)修改下 就是搜索 防止重名写个链表 把搜出来的存入新建链表 为了删除方便 用了next 希望把删除的函数也给点指点 #include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
#define len sizeof(struct memorydress)
struct student
{ char num[50];
char score[20];
struct student *next;
};
struct memorydress{
struct student*p;
struct memorydress *next;
};
int n;
struct student*creat(void)
{struct student* head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student*) malloc(LEN);
scanf("%s,%s",p1->num,p1->score);
head=NULL;
while((p1->num[0])!='0')
{n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
scanf("%s,%s",p1->num,p1->score);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)//链表输出//
{struct student *p;
printf("\nNow,These %d records are:\n",n);
p=head;
//if(head!=NULL)
do
{printf("%s,%s\n",(p->num),(p->score));
p=p->next;
}while(p!=NULL);
}
/*struct student*insert(struct student *head,struct student*stud)
{struct student *p0,*p1,*p2;
p1=p2=head;
p1=p1->next;
p0=stud;
p2->next=p0;
p0->next=p1;
n=n+1;
return(head);
} */
struct memorydress* exas( struct student*head,char string[100],int i)//精确查找函数
{ int j=0;
struct memorydress*q1,*q2;
struct memorydress*head1;
struct student* p3;
p3=head; //标记下
if(i==1)
{ q1=q2=(struct memorydress*) malloc(len);
head1=NULL;
// while((p3->next)!=NULL)
{ if(strcmp(p3->num,string)==0)
{q1->p=p3;
while((q1->p)->num[0]!=0)
{j=j+1;
if(j==1) head1=q1;
else q2->next=q1;
q2=q1;
q1=(struct memorydress*)malloc(len);
p3->next=p3;
q1->p=p3;
}
q2->next=NULL;
}
}
return(head1);
}
else if(i==2)
{ q1=q2=(struct memorydress*)malloc(len);
head1=NULL;
// while((p3->next)!=NULL)
{ if(strcmp(p3->score,string)==0)
{q1->p=p3;
while((q1->p)->score[0]!=0)
{j=j+1;
if(j==1) head1=q1;
else q2->next=q1;
q2=q1;
q1=(struct memorydress*)malloc(len);
p3->next=p3;
q1->p=p3;
}
q2->next=NULL;
}
}
}
return(head1);
}
int main()
{struct student *head,*stu;
struct memorydress* q;
// long del_num;
char string[100];
int i;
char c;
printf("input records:\n");
head=creat();
print(head);
/* printf("\ninput the inserted record:");
stu=(struct student*)malloc(LEN);
scanf("%s,%s",stu->num,stu->score);
while((stu->num[0])!='0')
{
head=insert(head,stu);
print(head);
printf("\ninput the inserted record:");
stu=(struct student*)malloc(LEN);
scanf("%s,%s",stu->num,stu->score);
} */
printf("输入查找方式:1 按num查找 ; 2 按score查找; 请输入:");
scanf("%d",&i);
printf("输入要查找的对象:");
c=getchar();
gets(string);
q=exas(head, string, i);
while(q->next!=NULL)
// printf("%s,%s\n",(q->p)->num,(q->p)->score);
{ puts((q->p)->num);
puts((q->p)->score);
q->next=q;
}
system("pause");
}