C 语言链表其他实现

#include<stdio.h>
#include<stdlib.h>
#define N 10

typedef struct list
    {
     int data;
     struct list *next;
    }SLIST;

main()
   {
    SLIST *head ,*p,*q;

    int i;

    int a[10]={1,2,3,4,5,6,7,8,9};

    head=p=(SLIST *)malloc(sizeof(SLIST));
    for(i=0;i<N;i=i+1)
        {

          q=(SLIST *)malloc(sizeof(SLIST));

      //    printf("%p\n",q);
          q->data=a[i];
          p->next=q;
          p=q;
//          printf("%p\n",q);

        }




     p=head->next;           //下面为打印一个链表
     while(p!=NULL)
       {
  //      printf("%p",p);
        printf("%d\n",p->data);//输出当前节点的数据
        q=p;                //删除当前节点
        p=p->next;

        free(q);            //释放删除的
        }


    return ;

   }
posted @ 2022-08-19 22:56  luoganttcc  阅读(9)  评论(0)    收藏  举报