以下函数createlink的功能是创建带头结点的单链表,并输入n个整数,依次为各节点的数据域赋值。
#include<stdio.h>
#include<stdlib.h>typedef struct Node{int data;struct Node* next;
}A;A *createlink(int n)
{A *h=NULL,*p,*s;int i,x;p=(A*) malloc(sizeof(A));h=p;p->next=NULL;for(i=1;i<=n;i++){s=(A*)malloc(sizeof(A));scanf("%d",&x);s->data=x;s->next=p->next;p->next=s;p=s;}return h;
}void outlink(A *h)
{A *p;p=h->next;printf("the list is:");while(p){printf("->%d",p->data);p=p->next;}
}int main(){A *head;head = createlink(4);outlink(head);}