链接表的定义

#include <stdio.h> #include <iostream> #include <math.h> #define NULL 0 #define LEN sizeof(student) struct student { int num; double score; student* next; }; int n; student* create(void) { student *head; student *p1,*p2; n=0; p1=p2=(student*)malloc(LEN); scanf("%d%lf",&p1->num,&p1->score); head=NULL; while(p1->num!=0) { n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(student*)malloc(LEN); scanf("%d%lf",&p1->num,&p1->score); } p2->next=NULL; return head; } void print(student *head) { student *p; p=head; while(head!=NULL && p!=NULL) { printf("student:%d score:%0.2lf/n",p->num,p->score); p=p->next; } } student *del(student *head,int num) { if(head==NULL) { printf("/nList is NULL!/n"); return head; } student *p1,*p2; p1=head; while(num!=p1->num && p1->next!=NULL) { p2=p1; p1=p1->next; } if(p1->num==num) { if(p1==head) head=p1->next; else p2->next=p1->next; } return head; } student *insert(student *head,student *ins) { student *p0,*p1,*p2; p1=head; p0=ins; if(head==NULL) { head=p0; p0->next=NULL; return head; } while((p0->num>p1->num) && (p1->next!=NULL)) { p2=p1; p1=p1->next; } if(p0->num<=p1->num) { if(p1==head) head=p0; else { p2->next=p0; p0->next=p1; } } else { p1->next=p0; p0->next=NULL; } return head; } int main() { student *head; head=create(); print(head); int num; scanf("%d",&num); head=del(head,num); print(head); student *stu; stu=(student*)malloc(LEN); scanf("%d%lf",&stu->num,&stu->score); head=insert(head, stu); print(head); system("pause"); }

你可能感兴趣的:(struct,null,System,insert,include,p2p)