1. 程式人生 > >鏈表的建立及釋放

鏈表的建立及釋放

nbsp malloc col log color n) can 建立 struct

鏈表是建立

struct Data
{
int num;
int score;
struct Data *next;
};
struct Data *input()
{
    struct Data *head,*p1,*p2;
    p1=(struct Data*)malloc(LEN);
    p2=(struct Data*)malloc(LEN);
    n=0;
    scanf("%d",&p1->num);
    while(p1->num!=0)
    {
            scanf("%d",&p1->score);
        n
++; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(struct Data*)malloc(LEN); scanf("%d",&p1->num); } p2->next=NULL; return head; }

鏈表的釋放

void Free_List(struct Data *Head)
{
    struct Data *Pointer=NULL;
    while
(NULL != Head) { Pointer = Head; Head = Head->Next; // 下一個節點 free(Pointer); } }

稍後的一些補充

鏈表的建立及釋放