使用后插法创建一个有n个结点的单向链表

 typedef struct  Node {

     int data;

     struct  Node * next;

 } NODE;

void linklist_create_with_taiI_insert(int* data,unsigned short n)

{
    NODE* pHead = (Node*)malloc(sizeof(Node));
    NODE* pCurr = NULL;
    NODE* pTail = pHead;

    for(int i = 0; i < n; ++i) {
        pCurr = (NODE*)malloc(sizeof(NODE));
        pCurr -> data = data[i];
        pTail -> next = pCurr;
        pTail = pCurr;
    }
    pTail -> next = NULL;
    printf("尾插法:\n");
    for(int j = 0; j < n; ++j) {
        printf("%d\t", pHead -> next -> data);
        pHead = pHead -> next;
    }
}

猜你喜欢

转载自blog.csdn.net/u014689845/article/details/88208181