静态链表

#include<iostream.h> 
const int max=100; 
typedef struct SListNode

    int data; 
    int count; 
}StaticList[max];  
 void InitSList(StaticList & space ) 
 { 
     for(int i=0;i<max-1;i++) 
   space[i].count=i+1;  
   space[max-1].count= 0;
 }  
 void Free_SLNode(StaticList & space,int t) 
 { 
     space[t].count= space[0].count; 
     space[0].count= t; 
 }  
 int Build(StaticList & space ) 
 { 
     int i = space[0].count; 
     if(i) 
     { 
         space[0].count= space[i].count; 
     } 
     return i; 
 } 
 
 bool insert(StaticList & space,int h,int m,int t) 
 { 
  if(t<=0) 
  { 
    return false; 
  } 
  int i = h; 
  int n = 0;
  while(i) 
  { 
      if(n == t-1 && space[i].count)
    { 
        space[m].count= space[i].count; 
        space[i].count= m; 
        return true; 
    } 
    i = space[i].count; 
    n++; 
  } 
  return false; 
 }  
 bool dele(StaticList & space,int h, int n) 
 { 
     if(n<0) return false; 
     int i=h; 
     int m =0; 
     while(i) 
     { 
      if(m == n-1) 
      {   int t = space[i].count; 
          space[i].count = space[t].count; 
          Free_SLNode(space,t);
          return true; 
      } 
      i = space[i].count; 
      m++; 
     } 
     return false; 
 }  
 bool add(StaticList & space,int h,int m)
 { 
     int i = h; 
     while(space[i].count) 
     { 
       i = space[i].count; 
     } 
     space[i].count=m; 
     space[m].count=0; 
     return true; 
 } 
 
int main() 
{  
    StaticList List; 
    InitSList(List);  
    int h = Build(List); 
    List[h].count = 0;  
    int t = Build(List); 
    List[t].data = 1; 
    add(List,h,t); 
    t = Build(List); 
    List[t].data = 3; 
    add(List,h,t); 
    t = Build(List); 
    List[t].data = 7; 
    add(List,h,t);  
    cout<<"输出所有数据:"<<endl; 
    int i = List[h].count; 
    while (i) 
    { 
        cout<<List[i].data<<","; 
        i = List[i].count; 
    } 
    cout<<endl;  
    cout<<"在第1个元素之前插入一个值为6的新节点:"<<endl; 
    t = Build(List); 
    List[t].data = 6; 
    insert(List,h,t,1); 
     i = List[h].count; 
    while (i) 
    { 
        cout<<List[i].data<<","; 
        i = List[i].count; 
    } 
    cout<<endl; 
    cout<<"删除第2节点:"<<endl; 
    dele(List,h,2); 
    i = List[h].count; 
    while (i) 
    { 
        cout<<List[i].data<<","; 
        i = List[i].count; 
    } 
    cout<<endl; 
 return 0;
  } 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值