基于结构体数组的链表实现

 

 
 
  1. /*基于结构体数组的链表实现*/ 
  2. /* made by winlin 2011.4.11
  3. *initialize(  )初始化存储池 
  4. *insertNode(  )插入一个节点 
  5. *deleteNode(  )删除一个节点 
  6. *display_member(  )显示存储池里面的数据 
  7. *display_info(  )显示存储池当前的信息 
  8. *isempty(  )存储池是否已满 
  9.  */ 
  10. #include <iostream> 
  11. #include <cstdlib> 
  12.  
  13. typedef int elementType; 
  14. const int NULL_VALUE=-1;   //不存在的位置 
  15. //节点声明 
  16. typedef struct node 
  17.     elementType data;   //存放节点的数据 
  18.     int next;                  //存放下一个节点在存储池数组中的序号 
  19. }Node; 
  20.  
  21. //存储池 
  22. const int NUMNODES=2048; 
  23. Node  nodeList[NUMNODES]; 
  24. int freelist;       //其值为空闲节点链的在存储池中的起始位置 
  25. int first;   //其值为第一个节点在存储池中的位置 
  26. int emptyNodes; 
  27.  
  28. void initialize( ) 
  29.     { 
  30.         int i; 
  31.         for( i=0;i<NUMNODES;++i ) 
  32.             { 
  33.                 nodeList[ i ].next=i+1; 
  34.             } 
  35.         nodeList[ NUMNODES-1 ].next=NULL_VALUE; 
  36.         freelist=0; 
  37.         first=NULL_VALUE; 
  38.         emptyNodes=NUMNODES; 
  39.  
  40.     } 
  41.  
  42. bool isempty(  ) 
  43.     { 
  44.         if( emptyNodes==NUMNODES ) 
  45.             return 1; 
  46.         return 0; 
  47.          
  48.     } 
  49.  
  50. int insertNode(const elementType& nodedata) 
  51.     { 
  52.         int temp; 
  53.          
  54.         if( emptyNodes<=0 ) 
  55.             { 
  56.                 std::cout<<"the NodeList has full/n"
  57.                 exit( -1 ); 
  58.             } 
  59.          
  60.         temp=freelist; 
  61.         freelist=nodeList[ freelist ].next;   //从空闲链表中剔除开始的那个节点 
  62.         if( first==NULL_VALUE ) 
  63.             { 
  64.                 first=temp; 
  65.                 nodeList[ temp].data=nodedata; 
  66.                 nodeList[ temp].next=NULL_VALUE; 
  67.                  
  68.             } 
  69.         else 
  70.             { 
  71.                 nodeList[ temp ].data=nodedata; 
  72.                 nodeList[ temp ].next=first; 
  73.                 first=temp; 
  74.  
  75.             } 
  76.         --emptyNodes; 
  77.         return temp; 
  78.  
  79.     } 
  80.  
  81. void deleteNode(const  elementType& nodedata ) 
  82.     { 
  83.        if( first==NULL_VALUE ) 
  84.            { 
  85.                std::cout<<"the NodeList is empty/n"
  86.                exit( -1 ); 
  87.            } 
  88.        int temp_cur=first; 
  89.        int temp_pre=first; 
  90.        while( temp_cur !=NULL_VALUE) 
  91.            { 
  92.                if( nodeList[ first ].data==nodedata )    //第一个就是要删除的节点 
  93.                    { 
  94.                        int i=first; 
  95.                        first=nodeList[ i ].next;   //处理已使用链 
  96.                        //处理未使用链 
  97.                        nodeList[i].next=freelist; 
  98.                        freelist=i; 
  99.                    } 
  100.                else if( nodeList[ temp_cur].data==nodedata ) 
  101.                    { 
  102.                        nodeList[temp_pre].next=nodeList[ temp_cur ].next;    //处理已使用的链表 
  103.                        //处理未使用的链表 
  104.                        nodeList[ temp_cur ].next=freelist; 
  105.                        freelist=temp_cur; 
  106.                         
  107.                    } 
  108.                temp_pre=temp_cur; 
  109.                temp_cur=nodeList[ temp_cur].next; 
  110.            } 
  111.        ++emptyNodes; 
  112.         
  113.     } 
  114.  
  115. void display_member( ) 
  116.     { 
  117.         if( emptyNodes==NUMNODES ) 
  118.             { 
  119.                 std::cout<<"the NodeList is empty/n"
  120.                 exit( -1 ); 
  121.             } 
  122.         int temp=first; 
  123.          
  124.         while(temp!=NULL_VALUE) 
  125.             { 
  126.                 std::cout<<nodeList[ temp ].data<<"  "
  127.                 temp=nodeList[ temp ].next; 
  128.                  
  129.             } 
  130.         std::cout<<"/n"
  131.          
  132.     } 
  133. void display_info(  ) 
  134.     { 
  135.         std::cout<<"NodeList的总容量为:"<<NUMNODES<<"/nNodeList已经使用:" 
  136.                  <<NUMNODES-emptyNodes<<"/n剩余可用量为:"<<emptyNodes<<"/n"
  137.          
  138.     } 
  139. int main(  ) 
  140.     { 
  141.         initialize(  ); 
  142.         insertNode( 12 ); 
  143.         insertNode( 13 ); 
  144.         insertNode( 14 ); 
  145.         insertNode( 15 ); 
  146.         insertNode( 16 ); 
  147.         display_member(  ); 
  148.         display_info(  ); 
  149.  
  150.         deleteNode(16); 
  151.         deleteNode( 12 ); 
  152.         deleteNode( 13 ); 
  153.         display_member(  ); 
  154.         display_info(  ); 
  155.  
  156.         return 0; 
  157.          
  158.     } 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值