C尾插法建立单链表

时不时的整一下c还是挺爽的。

#include <stdio.h>
#include <stdlib.h>

typedef struct LNode{
    int data;
    struct LNode *next;
}LNode,*LList;

LList Rear_Insert_LList(LList &L){
    int x;
    L = (LList)malloc(sizeof(LNode));
    LNode *s, *r=L;
    scanf("%d", &x);
    while(x != 9999){
        s = (LNode*)malloc(sizeof(LNode));
        s->data = x;
        r->next = s;
        r = s;
        scanf("%d", &x);
    }
    r->next = NULL;
    return L;
}

int main(void) { 
    printf("test");
    LList L, Ltemp;
    printf("请输入插入的数字,输入9999将成功建立单链表\n");
    Rear_Insert_LList(L);
    Ltemp = L->next;
    if(Ltemp != NULL){
        printf("建立的单链表为:\n");
        while(Ltemp){
            printf("\t%d, ",Ltemp->data);
            Ltemp = Ltemp->next;
        }
    }
    else
        printf("所建立的单链表为空!\n");
    return 0;
}


最后输出这做了一下处理,就不用另外写一个方法了。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容