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;
    }
  • 相关阅读:
    setTimeout的时间设为0的问题
    nodejs的简单服务器程序
    使用Promise规定来处理ajax请求的结果
    使用myfocus制作焦点图
    给Array添加删除重复元素函数
    css派生选择器
    Javascript 参数传递
    Node.js 搞Javascript开发的无论如何要尝试一下
    CSS九宫格带边框的多种实现
    80%人会答错的JS基础面试题
  • 原文地址:https://www.cnblogs.com/kbyd/p/3998160.html
Copyright © 2011-2022 走看看