链表的操作

1.建立链表

建立头结点,建立其他n-1个结点,连接起来

struct student *create(int n) {
 struct student *head,*pnew,*ptail;
 int i;
 pnew=(struct student *)malloc(sizeof(struct student));
 scanf("%d%s%f",&pnew->num,pnew->name,&pnew->score);
 head=ptail=pnew;
 for(i=1;i<n;i++){
  pnew=(struct student *)malloc(sizeof(struct student));
  scanf("%d%s%f",&pnew->num,pnew->name,&pnew->score);
  ptail->next=pnew;
  ptail=pnew;
 }
 ptail->next=NULL;
 return head;
}

2.遍历链表

判定是否为空,不为空则输出

void print(struct student *head){
 struct student *p=head;
 while(p!=NULL){
  printf("%d %s %.lf\n",p->num,p->name,p->score);
  p=p->next;
 }
}

3.在链表中插入结点

4.结点的删除

 

你可能感兴趣的:(链表)