有关链表一些基础小知识

对链表的操作主要有以下几种:建立链表,结构体的查找与输出,插入一个节点,删除一个节点等
下面就一个典型的例子,谈谈链表的建立.

#include<stdio.h>
#include<stdlib.h>
struct node 
{
int num,score;
struct node *next;
};
int main()
{
     struct node *creat (struct node *head,int n);
     void print(struct node *head);
     struct node *head=NULL;             //*定义表头指针*
     head=creat(head,5);
     print(head);
     return 0;
}
struct node *creat(struct node *head,int n);     
{                                       //*链表的建立*
     struct node *p,*q;
     int i;
     for(i=1;i<n;i++)
     {                         //*申请节点空间*
     q=(struct node *)malloc(sizeof(struct node));
     printf("Input%d num,score:\n",i);
     scanf("%d,%d",&q->num,&q->score);
     q->next=NULL;
     if(head==NULL)
          head=q;            //*新节点作为表头插入链表*
     else
          p->next=q;        //*新节点作为表尾插入链表*
     p=q;
     }
     return head;
}
void print(struct node *head)
{
     struct node *p=head;
     printf("num\tscore\n");
     while(p!=NULL)
     {
     printf("%d\t%d\n",p->num,p->score);
     p=p->next;         //*p指向下一个节点*
     }
}



     

猜你喜欢

转载自blog.csdn.net/qq_43405325/article/details/85929838