C语言链表——头插法和尾插法

2021-03-04 07:29

阅读:401

标签:else   null   define   list   ==   eof   warning   bsp   col   

#define _CRT_SECURE_NO_WARNINGS
#include
#include

typedef struct ListNode{
    int val;
    ListNode* next;
}Node_t, *pNode_t;

void print_list(ListNode *head) {
    ListNode *p = head;
    while (p != nullptr) {
        printf("%d ", p->val);
        p = p->next;
    }
}

void headInsert(pNode_t *p_p_head, pNode_t *p_p_tail, int val) {
    pNode_t pNew = (pNode_t)calloc(1,sizeof(Node_t));
    pNew->val = val;
    if (*p_p_head == NULL) {
        *p_p_head = pNew;
        *p_p_tail = pNew;
    }
    else {
        pNew->next = *p_p_head;
        *p_p_head = pNew;
    }
}

void tailInsert(pNode_t *p_p_head, pNode_t *p_p_tail, int val) {
    pNode_t pNew = (pNode_t)calloc(1, sizeof(Node_t));
    pNew->val = val;
    if (*p_p_head == NULL) {
        *p_p_head = pNew;
        *p_p_tail = pNew;
    }
    else {
        (*p_p_tail)->next = pNew;
        *p_p_tail = pNew;
    }
}

int main() {
    pNode_t head = nullptr;
    pNode_t tail = nullptr;
    int num = 0;
    while (scanf("%d", &num) != EOF) {
        tailInsert(&head, &tail, num);
    }
    print_list(head);
}

 

C语言链表——头插法和尾插法

标签:else   null   define   list   ==   eof   warning   bsp   col   

原文地址:https://www.cnblogs.com/Ping697/p/14364182.html


评论


亲,登录后才可以留言!