学习

将二叉树的两个孩子换位置,即左变右,右变左。不能用递规

 

  1. void exchange(BiTree BT)  
  2. {  
  3.     BiTree   r,p;  
  4.     BiTree   stack[100];  
  5.     int   tp=0;  
  6.     stack[0]=BT;  
  7.     while(tp >= 0)  
  8.     {  
  9.         p=stack[tp];  
  10.         tp=tp-1;  
  11.         if(p!=NULL)  
  12.         {  
  13.             r=p-> lchild;  
  14.             p-> lchild=p-> rchild;  
  15.             BT-> rchild=r;    
  16.             stack[tp+1]=p-> lchild;  
  17.             tp=tp+1;  
  18.             stack[tp+1]=p-> rchild;  
  19.             tp=tp+1;  
  20.         }  
  21.     }  

 

A,B,C,D四个进程,A向buf里面写数据,B,C,D向buf里面读数据,

当A写完,且B,C,D都读一次后,A才能再写。用P,V操作实现。

 

 

semaphore empty = n

semaphore full;

semaphore mutex =1;

semaphore b = 1;

semaphore c = 1 ;

semaphore d = 1;

A (){
while(true){
      p(empty);
      p(b);
      p(c);
 p(d);
 p(mutex);
 write();
 v(mutex);
 v(full);
 }
}

B (){
while(true){
      p(full);
      p(mutex);
 write();
 v(mutex);
 v(empty);
 v(b);
 }
}

C(){
while(true){
      p(full);
      p(mutex);
 write();
 v(mutex);
 v(empty);
 v(c);
 }
}

D (){
while(true){
      p(full);
      p(mutex);
 write();
 v(mutex);
 v(empty);
 v(d);
 }
}

posted on 2016-08-27 16:58  Dos尚  阅读(110)  评论(0)    收藏  举报