zoukankan      html  css  js  c++  java
  • 线性表之静态链表

    #include<iostream>
    #include <iomanip>
    #include"windows.h"
    using namespace std;
    struct StaticLinkNode
    {
        int data;
        int next;
    };
    struct StaticLink
    {
        StaticLinkNode* nodes;
        int maxCnt;
        int cnt;
    };
    StaticLink createStaticLink(int max)
    {
        StaticLink p;
        p.nodes=new StaticLinkNode[max+2];
        for(int i=0;i<max;i++)
            p.nodes[i].next=i+1;
        p.nodes[max].next=0;
        p.nodes[max+1].next=0;
        p.maxCnt=max;
        p.cnt=0;
        return p;
    }
    int getSpare(StaticLink* p)
    {
        int first = (*p).nodes[0].next;
        if(first!=0)
            (*p).nodes[0].next = (*p).nodes[first].next;
        return first;
    }
    void releaseToSpare(StaticLink* p,int i)
    {
        int first = (*p).nodes[0].next;
        (*p).nodes[i].next=first;
        (*p).nodes[0].next=i;
    }
    bool add(StaticLink* p,int i,int value)
    {
        int target =getSpare(p);
        if(target==0)
            return 0;
        int first =(*p).maxCnt+1;
        for(int j=0;j<i-1 && first;j++)
            first = (*p).nodes[first].next;
        if(first!=0)
        {
            (*p).nodes[target].data=value;
            (*p).nodes[target].next = (*p).nodes[first].next;
            (*p).nodes[first].next=target;
            (*p).cnt++;
            return 1;
        }
        return 0;
    }
    bool remove(StaticLink* p,int i)
    {
        int first =(*p).maxCnt+1;
        if(first==0)
            return 0;
        for(int j=0;j<i-1 && (*p).nodes[first].next;j++)
            first = (*p).nodes[first].next;
        if((*p).nodes[first].next)
        {
            int t=(*p).nodes[first].next ;
            (*p).nodes[first].next = (*p).nodes[(*p).nodes[first].next].next;
            releaseToSpare(p,t);
            (*p).cnt--;
            return 1;
        }
        return 0;
    }
    void output(StaticLink* p)
    {
        int first =(*p).nodes[(*p).maxCnt+1].next;
        while(first!=0)
        {
            cout<<(*p).nodes[first].data<<" ";
            first=(*p).nodes[first].next;
        }
        cout<<endl;
    }
    void main()
    {
        int max=28;
        StaticLink p= createStaticLink(28);
        for(int i=0;i<15;i++)
        {
            int x = rand()%100;
            cout<<x<<" ";
            add(&p,p.cnt+1,x);
        }
        cout<<endl;
        output(&p);
    
        add(&p,2,45);
        output(&p);
    
        add(&p,1,55);
        output(&p);
    
        add(&p,p.cnt,65);
        output(&p);
        add(&p,p.cnt+1,75);
        output(&p);
    
        remove(&p,2);
        output(&p);
    
        remove(&p,1);
        output(&p);
    
        remove(&p,p.cnt);
        output(&p);
        remove(&p,p.cnt+1);
        output(&p);
    
        cin>>max;
    }
  • 相关阅读:
    webpack里CommonJS的require与ES6 的module.exports加载模块有何不同
    前端项目使用module.exports文件一定要Webpack编译吗?请问gulp可以编译这种文件吗
    Webpack之“多页面开发”最佳实战
    webpack 单页面应用实战
    也谈谈同源策略和跨域问题
    (转)Babel-现在开始使用 ES6
    webpack 配置简单说几句 ?
    Javascript 严格模式详解
    JavaScript6 新语法 let 有什么优势
    js中let和var定义变量的区别
  • 原文地址:https://www.cnblogs.com/kbyd/p/3998160.html
Copyright © 2011-2022 走看看