zoukankan      html  css  js  c++  java
  • 链表的简单创建及使用

    //
    //  ViewController.m
    //  Link_list_Demo
    //
    //  Created by 张凯泽 on 16/5/10.
    //  Copyright © 2016年 rytong_zkz. All rights reserved.
    //
    
    #import "ViewController.h"
    struct Link_list{
        int data;
        struct Link_list * Pnext;
    };
    struct Link_list * head_list;
    //创建链表 head节点
    bool creatLink_list()
    {
        head_list = (struct Link_list*)malloc(sizeof(struct Link_list));
        if (head_list == NULL) {
            return false;
        }else{
            head_list ->data = 0;
            head_list ->Pnext = NULL;
            return true;
        }
        
        
    }
    //增加节点从尾部添加
    bool addLink_list(struct Link_list * note)
    {
        //没有头节点
        if (head_list == NULL) {
            return false;
        }
        //只有头节点
        if (head_list ->Pnext == NULL) {
            head_list->Pnext = note;
            note ->Pnext = NULL;
            return true;
        }
        //有两个节点
        struct Link_list * p = head_list ->Pnext;
        struct Link_list * temp = head_list;
        while (p != NULL) {
            temp = p;
            p = p ->Pnext;
    
        }
        temp->Pnext  = note;
        note ->Pnext = NULL;
        
        return true;
    
    }
    //插入
    bool insertLink_list(struct Link_list * note)
    {
        if (head_list == NULL) {
            return false;
        }
        struct Link_list * p = head_list;
        
        note->Pnext = p->Pnext;
        p -> Pnext = note;
            return true;
        
    }
    
    //删除
    bool deleteLink_list(int index)
    {
        if (head_list == NULL) {
            return false;
        }
        struct Link_list * p = head_list ->Pnext;
        int lenth = 0;
        while (p != NULL) {
            lenth ++;
            p = p->Pnext;
            
        }
        if (lenth<index) {
            return false;
        }else{
        struct Link_list * q = head_list;
            p = head_list;
            for (int i = 0; i <index; i++) {
                q = p;
                p = p ->Pnext;
                
            }
            
            q ->Pnext = p ->Pnext;
            free(p);
            return true;
        
        }
        
    }
    //销毁链表
    void destroyNodeList()
    {
        //没有头节点
        if (head_list == NULL) {
            return;
        }
        //只有头节点
        if (head_list ->Pnext == NULL) {
            free(head_list);
            return;
        }
        //两个节点以上者(包括两个节点)
        struct Link_list * p = head_list->Pnext;
        // = NULL;
        while (p != NULL) {
            struct Link_list * q = p;
            p = p->Pnext;
            free(q);
        }
        free(head_list);
        head_list = NULL;
    }
    // 展示
    void showLink_list()
    {
        if (head_list == NULL) {
            return ;
            
        }
        struct Link_list * p = head_list;
        while (p != NULL) {
            NSLog(@"%p-----%d",p,p ->data);
           p = p ->Pnext;
            
        }
        
    }
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        creatLink_list();
        
        struct Link_list * note1 = (struct Link_list*)malloc(sizeof(struct Link_list));
        note1 ->data = 1;
        addLink_list(note1);
        
        struct Link_list * note2 = (struct Link_list*)malloc(sizeof(struct Link_list));
        note2 ->data = 2;
        addLink_list(note2);
        
        struct Link_list * note3 = (struct Link_list*)malloc(sizeof(struct Link_list));
        note3 ->data = 3;
        addLink_list(note3);
        
        struct Link_list * note4 = (struct Link_list*)malloc(sizeof(struct Link_list));
        note4 ->data = 4;
        addLink_list(note4);
        
        showLink_list();
        //destroyNodeList();
        deleteLink_list(2);
        showLink_list();
    
        
        
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
  • 相关阅读:
    关于sencha touch中给文本添加焦点无效的解决方案
    sencha touch 入门系列 (五)sencha touch运行及代码解析(上)
    关于用phonegap 3.0+ 打包后sencha touch按钮点击切换动画延迟接近一秒的以及界面闪烁的解决方案
    Building a Simple User Interface(创建一个简单的用户界面)
    Running Your App(运行你的应用程序)
    android GridLayout布局
    Android Studio SVN的使用
    Android Library项目发布到JCenter最简单的配置方法
    AndroidStudio项目提交(更新)到github最详细步骤
    Android RecyclerView的使用
  • 原文地址:https://www.cnblogs.com/zkzzkz/p/5481300.html
Copyright © 2011-2022 走看看