zoukankan      html  css  js  c++  java
  • C实现头插法和尾插法来构建单链表(不带头结点)

           链表的构建事实上也就是不断插入节点的过程。而节点的插入能够分为头插法和尾插法。

    头插法就是在头结点后插入该节点,始终把该节点作为第一个节点。尾插法就是在链表的最后一个节点处插入元素,作为最后一个节点。假设想要了解链表的概念和其它链表操作。请參考《数据结构与算法之链表》《C语言实现链表的基本操作》两篇文章。演示样例代码上传至  https://github.com/chenyufeng1991/HeadInsertAndTailInsert

    //
    //  main.c
    //  HeadInsertAndTailInsert
    //
    //  Created by chenyufeng on 16/2/25.
    //  Copyright © 2016年 chenyufengweb. All rights reserved.
    //
    
    /**
     *  分别使用头插法和尾插法建立单链表
     */
    
    #include <stdio.h>
    #include "stdlib.h"
    #include "string.h"
    
    typedef int elemType;
    //构造节点
    typedef struct ListNode{
        int element;
        struct ListNode *next;
    }Node;
    
    //初始化链表
    void initList(Node *pNode){
    
        pNode = NULL;
        printf("%s函数运行,头结点初始化完毕
    ",__FUNCTION__);
    }
    
    //打印链表
    void printList(Node *pNode){
        if (pNode == NULL) {
            printf("%s函数运行,链表为空,打印失败
    ",__FUNCTION__);
        }else{
            while (pNode != NULL) {
                printf("%d ",pNode->element);
                pNode = pNode->next;
            }
            printf("
    ");
        }
    }
    
    //头插法
    Node *HeadInsert(Node *pNode){
    
        Node *pInsert;
        pInsert = (Node*)malloc(sizeof(Node));
        if (pInsert == NULL) {
            printf("%s函数运行。内存分配失败,建立链表失败
    ",__FUNCTION__);
            return NULL;
        }
    
        memset(pInsert, 0, sizeof(Node));
        scanf("%d",&(pInsert->element));
        pInsert->next = NULL;
    
        if (pInsert->element <= 0) {
            printf("%s函数运行。输入数据有误,建立链表失败
    ",__FUNCTION__);
            return NULL;
        }
    
        while (pInsert->element > 0) {
    
            if (pNode == NULL) {
                pNode = pInsert;
            }else{
                //注意以下语句的顺序,否则可能造成链断裂
                pInsert->next = pNode;
                pNode = pInsert;
            }
    
            pInsert = (Node*)malloc(sizeof(Node));
            if (pInsert == NULL) {
                printf("%s函数运行,内存分配失败,建立链表失败
    ",__FUNCTION__);
                return NULL;
            }
    
            memset(pInsert, 0, sizeof(Node));
            scanf("%d",&(pInsert->element));
            pInsert->next = NULL;
        }
    
        printf("%s函数运行。头插法建立链表成功
    ",__FUNCTION__);
    
        return pNode;
    }
    
    //尾插法
    Node *TailInsert(Node *pNode){
    
        Node *pInsert; //要插入的节点
        Node *pMove; //遍历链表的节点
        pInsert = (Node*)malloc(sizeof(Node));
        if (pInsert == NULL) {
            printf("%s函数运行,内存分配失败,建立链表失败
    ",__FUNCTION__);
            return NULL;
        }
    
        memset(pInsert, 0, sizeof(Node));
        scanf("%d",&(pInsert->element));
        pInsert->next = NULL;
    
        if (pInsert->element <= 0) {
            printf("%s函数运行。输入数据有误,建立链表失败
    ",__FUNCTION__);
            return NULL;
        }
    
        pMove = pNode;
        while (pInsert->element > 0) {
            if (pNode == NULL) {
                //注意不要忘了改动pMove指针的指向,初始pMove一定要指向头节点
                pNode = pInsert;
                pMove = pNode;
            }else{
                //遍历找到最后一个节点
                while (pMove->next != NULL) {
                    pMove = pMove->next;
                }
                pMove->next = pInsert;
            }
    
            pInsert = (Node*)malloc(sizeof(Node));
            if (pInsert == NULL) {
                printf("%s函数运行。内存分配失败,建立链表失败
    ",__FUNCTION__);
                return NULL;
            }
    
            memset(pInsert, 0, sizeof(Node));
            scanf("%d",&(pInsert->element));
            pInsert->next = NULL;
        }
    
        printf("%s函数运行,尾插法建立链表成功
    ",__FUNCTION__);
    
        return pNode;
    }
    
    int main(int argc, const char * argv[]) {
    
        Node *pList;
    
        initList(pList);
        printList(pList);
    
        //头插法建立链表
        pList = HeadInsert(pList);
        printList(pList);
    
        //尾插法建立链表
        pList = TailInsert(pList);
        printList(pList);
    
        return 0;
    }
    
    


  • 相关阅读:
    古典密码仿射密码Affine
    git 修改远程仓库地址的方法
    git 修改已经 commit 的用户名和邮箱
    git 调整commit之间的顺序
    Go 逃逸分析
    docker 镜像中的 @sha256:cbeaf907fc78ac97ce7b625e4bf0de16e3ea725daf6b04f930bd14c67c671ff9 (digest)是什么意思
    Docker镜像列表中的<none>:<none>是什么镜像
    github 下fork后如何同步源的新更新内容
    SQL 中 exists 的用法
    OLEDB的Excel的IMEX和HDR是什么意思
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7168322.html
Copyright © 2011-2022 走看看