欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

数据结构 - 单链表的基本操作(表头插入、表尾插入)

最编程 2024-06-11 12:53:46
...

//头插法
LinkList* create(LinkList &L){
   LNode *s ,int x

   L=(LinkList)malloc(sizeof(LNode));//创建头节点

   L->next=null;

   while(scanf("%d",&x) !=EOF){
      s=(LNode)malloc(sizeof(LNode));//创建新节点

     s->data=x;

     s->next=L->next;

     L->next=s;

     scanf("%d",&x);

}
     return L;

}

//尾插法

LinkList Creat2(LinkList &L) {
     int x; 
     L = (LinkList)malloc(sizeof(LNode));
     LNode *s, *r = L; 
     while(scanf("%d",&x) != EOF) { 
         s = (LNode*)malloc(sizeof(LNode));
         s->data=x;
         r->next=s;

        r=s;
        scanf("%d",&x);

}

     r->next=null;

     return L;

}

推荐阅读