#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct STUDENT{
char name[20];
float score;
struct STUDENT *next;
};
int lenth(struct STUDENT *head);
void printlink(struct STUDENT *head);
struct STUDENT *search(struct STUDENT *head,struct STUDENT target);
void linkfree(struct STUDENT *head);
int main()
{
struct STUDENT *head=NULL;
struct STUDENT *p1=NULL;
struct STUDENT *p2=NULL;
struct STUDENT target;
struct STUDENT *ret=NULL;
int i;
int len;
char temp[10];
printf("请按行输入链表数据(以0000结尾):\n");
scanf("%s",temp);
while(strcmp(temp,"0000")!=0){
p2=(struct STUDENT *)malloc(1*sizeof(struct STUDENT));
head=p2;
strcpy(p2->name,temp);
scanf("%f",&p2->score);
p2->next=p1;
p1=p2;
scanf("%s",temp);
}
len=lenth(head);
printf("链表长度为:%d\n",len);
printf("你输入的链表为:\n");
printlink(head);
printf("请输入查找目标:\n");
{
scanf("%s",target.name);
scanf("%f",&target.score);
}
ret=search(head,target);
linkfree(head);
if(ret==NULL){
printf("找不到目标元素!");
}
else{
printf("目标地址为:%#X\n",ret);
}
return 0;
}
int lenth(struct STUDENT *head)
{
struct STUDENT *p=head;
int count=0;
while(p!=NULL){
count++;
p=p->next;
}
return count;
}
void printlink(struct STUDENT *head)
{
struct STUDENT *p=head;
while(p!=NULL){
printf("%-10s %-10g\n",p->name,p->score);
p=p->next;
}
}
struct STUDENT *search(struct STUDENT *head,struct STUDENT target)
{
struct STUDENT *p=head;
while(p!=NULL){
if(strcmp(target.name,p->name)==0&&target.score==p->score){
return p;
}
p=p->next;
}
return NULL;
}
void linkfree(struct STUDENT *head)
{
struct STUDENT *p=head;
struct STUDENT *q=head;
while(p!=NULL){
q=p->next;
free(p);
p=q;
}
printf("free完成!\n");
}
单向动态链表基本操作
猜你喜欢
转载自blog.csdn.net/howoldareyougg/article/details/105566987
今日推荐
周排行