zoukankan      html  css  js  c++  java
  • 链表的一点总结

    自己实现的一个简单的链表,功能不是很全,而且有很重大的错误,就是一级指针是没办法修改之前传过来的指针的,所以要使用二级指针或者是返回指针

    //总结:1.参数检查 对于插入操作等要检查插入位置等 对于删除操作要检查链表是否为空
    //      2.创建链表可以不将创建所有的节点放在整个函数中,而是写一个类似的CreateNode(struct node* p_head, elemtype e)之类的函数
    
    
    //链表的基本操作的实现,使用单个的函数实现逐个的操作
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    ///////////////////////////////////
    
    //创建链表 参数为头指针和新建节点的个数
    int CreateLinkList(struct node* p_head, int n);
    //插入节点,参数为头指针,和插入位置。其中位置使用一个整数location来表达。
    //对location参数要进行判断。
    //1.loaction < 0抛出异常  2.location = 0这样插入的节点就成了第一个节点,注意转移头指针 3.loaction超过链表的节点个数,异常
    int InsertNode(struct node* p_head, int location, int id);
    
    //获取链表的长度,就是节点的个数,参数为头指针
    int GetLength(struct node* p_head);
    //打印整个链表 参数为头指针
    void ShowLinkList(struct node* p_head);
    
    
    ///////////////////////////////////
    
    //定义结构体  数据结构核心
    struct node
    {
        int id;
        //char name[20];
        struct node* next;
    };
    
    
    
    //p_head头指针,永远指向第一个节点
    struct node* p_head = NULL;
    //p_node可以指向任意一个节点
    struct node* p_node;
    //new_node指针
    struct node* new_node;
    
    //主函数////////////////////////////
    int main(int argc, char* argv[])
    {
        
        //创建一个链表  参数是头指针?
        //printf("%d
    ",CreateLinkList(p_head, 5));
        CreateLinkList(p_head, 5);
        ShowLinkList(p_head);
        printf("length = %d", GetLength(p_head));
    
        ///////////////////////////////////////
        //主函数结束 不要修改
        getchar();
        return 0;
    }
    
    int CreateLinkList(struct node* p_head, int n)
    {
        int i;
        //新建第一个节点
        new_node = (struct node*)malloc(sizeof(struct node));
        new_node -> next = NULL;
        new_node -> id = 0;
        //strcpy(new_node -> name, "a");
        //p_head和p_node都是指向第一个节点
        p_head = new_node;
        p_node = new_node;
        for(i = 0;i < n - 1;i++)
        {
            new_node = (struct node*)malloc(sizeof(struct node));
            new_node -> next = NULL;
            new_node -> id = i + 1;
            //p_node后移 指针变换
            p_node -> next = new_node;
            //p_node = p_node -> next;
            p_node = new_node;
        }
        //成功标志?
        //printf("1");
        return 1;
    }
    
    int GetLength(struct node* p_head)
    {
        int length = 0;
        for(p_node = p_head;p_node != NULL;p_node = p_node -> next)
        {
            length++;
        }
        return length;
    }
    
    void ShowLinkList(struct node* p_head)
    {
        for(p_node = p_head;p_node != NULL;p_node = p_node -> next)
        {
            printf("%d
    ", p_node -> id);
        }
    }
    
    
    
    int InsertNode(struct node* p_head, int location, int id)
    {
        int n = 1;
        if(location < 0 || location > GetLength(p_head))
        {
            printf("错误,插入位置错误");
            //错误处理 暂时省略
        }
        if(location = 0)
        {
            new_node = (struct node*)malloc(sizeof(struct node));
            new_node -> id = id;
            new_node -> next = p_head;
            p_head = new_node;
        }
        else
        {
            for(p_node = p_head;n < location;p_node = p_node -> next)
            {
                n++;
            }
            new_node = (struct node*)malloc(sizeof(struct node));
            new_node -> id = id;
            new_node -> next = p_node -> next;
            p_node -> next = new_node;
        }    
        return 1;
    }

    这个才是正确的

    #include <stdio.h>
    #include <stdlib.h>
    struct Node{
        int data;
        struct Node *next;
    };
    void InitList(struct Node **pHead);
    struct Node* CreatNode(int d);
    struct Node* FindKth(struct Node* head, int k);
    int Insert(struct Node** pHead, int index, int element);
    void PrintList(struct Node* head);
    void DestroyList(struct Node** pHead);
    int DeleteNode(struct Node** pHead, int index);
    int main(){
        
        struct Node *head;
        InitList(&head);
        Insert(&head, 1, 6);
        Insert(&head, 1, 61);
        Insert(&head, 1, 62);
        Insert(&head, 1, 63);
        Insert(&head, 1, 64);
        PrintList(head);
        printf("%d
    ",DeleteNode(&head, 6));
        PrintList(head);
        DestroyList(&head);
        return 0;
    }
    int DeleteNode(struct Node** pHead, int index){
        struct Node* pTmp, *pNodeToBeDeleted;
        if (*pHead == NULL) {
            return -1;
        }
        if (index == 1) {
            pTmp = *pHead;
            *pHead = (*pHead)->next;
            free(pTmp);
            return 0;
        }
        pTmp = FindKth(*pHead, index-1);
        if (!pTmp) { // if(pTmp == NULL)
            return -2;
        }
        if (pTmp->next == NULL) {
            return -3;
        }
        pNodeToBeDeleted = pTmp->next;
        pTmp->next = pNodeToBeDeleted->next;
        free(pNodeToBeDeleted);
        return 0;
    }
    void DestroyList(struct Node** pHead){
        struct Node* pTmp;
    
        while (*pHead) {
            pTmp = *pHead;
            *pHead = (*pHead)->next;
            free(pTmp);
        }
    }
    void PrintList(struct Node* head){
        struct Node* pTmp;
        pTmp = head;
        while (pTmp) {
            printf("%d  ",pTmp->data);
            pTmp = pTmp->next;
        }
        printf("
    ");
    }
    int Insert(struct Node** pHead, int index, int element){
        struct Node* pTmp, *pTmpNode;
        if (index == 1) {
            pTmpNode = CreatNode(element);
            pTmpNode->next = *pHead;
            *pHead = pTmpNode;
            return 0;
        }
        else{
            pTmp = FindKth(*pHead, index-1);
            if (pTmp) {
                pTmpNode = CreatNode(element);
                pTmpNode->next = pTmp->next;
                pTmp->next = pTmpNode;
                return 0;
            }
            else return -1;
        }
    }
    struct Node* FindKth(struct Node* head, int k){
        struct Node* pTmp;
        pTmp = head;
        int i=1;
        while (pTmp && i<k) {
            pTmp = pTmp->next;
            i++;
        }
        return pTmp;
    }
    void InitList(struct Node **pHead){
        *pHead = NULL;
    }
    struct Node* CreatNode(int d){
        struct Node* pTmp;
        pTmp = (struct Node*)malloc(sizeof(struct Node));
        pTmp->data = d;
        pTmp->next = NULL;
        
        return pTmp;
    }
  • 相关阅读:
    关于匹配的一些问题
    Codeforces Round #396 (Div. 2) A,B,C,D,E
    Codeforces Round #394 (Div. 2) A,B,C,D,E
    HDU 1848 SG函数博弈
    HDU 1536 S-Nim SG博弈
    HDU 2509 Be the Winner nim博弈变形
    HDU 1907 John nim博弈变形
    Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并
    BZOJ 1031: [JSOI2007]字符加密Cipher 后缀数组
    HDU 5769 Substring 后缀数组
  • 原文地址:https://www.cnblogs.com/virusdefender/p/3342986.html
Copyright © 2011-2022 走看看