• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            jake1036

            隊列和棧的基本操作

                                          隊列和棧的基本操作

                 棧的操作,棧是一種先進后出的操作,需要注意的是棧的滿與空,棧空的情況是 top == 0 ,棧滿的情況是 top == N 。
                 棧實現的代碼如下:
                

            #include<iostream>
             
            using namespace std ;
             
            const int N = 100 ;
             
            struct Stack
             
            {
                
            int a [N] ;    
                
            int top    ;    
             }
             ;
             
             
             
            void iniStack(Stack *stack) ;
             
            bool empty(Stack *stack) ;
             
            void push(Stack *stack , int x) ;
             
            int pop(Stack *stack ) ;
             
            bool full(Stack * stack) ;
             
             
            int main()
             
            {
                Stack 
            * stack = new Stack ;
                iniStack(stack) ;
                push(stack , 
            4);
                push(stack , 
            5) ;
                cout
            <<pop(stack) ;
                cout
            <<pop(stack) ;
                cin.
            get() ;
              
              
            return 0 ;    
             }

             
             
            void push(Stack *stack , int x) 
             
            {
               
            if(!full(stack))
                  
            {
                    stack
            ->a[stack->top++= x ;
                                  
                  }
             
               
            else 
                  cout
            <<"error full"<<endl ;
             }

             
             
            int pop(Stack * stack)
             
            {
               
            if(empty(stack))  
                 
            return - 1 ;
               
            else
                
            return stack->a[--stack->top] ;
                   
             }
              
              
              
              
            void iniStack(Stack * stack)
              
            {
                stack
            ->top = 0 ;      
              }


              
            bool empty(Stack * stack)
              
            {
                
            return stack->top == 0 ;       
              }

              
              
            bool full(Stack * stack)
               
            {
                 
            return stack->top == N ;     
               }

              
              
               
               
             

               
             


            2 隊列的操作
               隊列為空的判斷條件,tail == head 。隊列采用head插入,TAIL彈出。

               隊列為滿的條件是,(HEAD + 1) % N = TAIL。
              
              

               代碼如下:
                

            #include <iostream>
             
            using namespace std ;
             
            const int N = 10 ;
             
            struct Queue
             
            {
                
            int head ; //頭部插入    
                int tail ; //尾部取出    
                int a[N] ;    
             }
             ;
             
             
            bool empty(Queue * queue) ;
             
            bool full (Queue * queue) ;
             
            void enqueue( Queue * queue , int x) ;
             
            int  dequeue(Queue * queue) ;
             
            void iniQueue(Queue * queue) ;
             
            int main()
             
            {
               Queue 
            * queue = new Queue ;
               iniQueue(queue);
               enqueue(queue , 
            4) ;
               enqueue(queue , 
            5) ;
               cout
            <<dequeue(queue) ;
               cout
            <<dequeue(queue) ;
               cin.
            get() ;
               
            return 0 ;    
             }
             

             
            void iniQueue(Queue * queue)
             
            {
               queue
            ->head = 0 ;
               queue
            ->ta il = 0 ;        
             }


              
            void enqueue( Queue * queue , int x)
              
            {
                   
            if(!full(queue))
                   
            {
                    queue
            ->a[queue->head++]  = x;                
                   }

                   
            else
                    cout
            <<"full"<<endl ;
              }


              
            int dequeue(Queue * queue) 
              
            {
                
            if(!empty(queue))
                  
            {
                    
            return queue->a[queue->tail++] ;             
                  }

                  
            else
                  
            {
                  cout
            <<"empty"<<endl;  
                   
            return -1 ;
                  }

              }

              
              
              
            bool empty(Queue * queue)
              
            {
                
            return queue->tail == queue->head ;        
              }

              
               
            bool full(Queue * queue)
               
            {
                
            return (queue->head + 1 ) % N == queue->tail ;      
               }

              





             

            posted on 2011-04-10 10:42 kahn 閱讀(800) 評論(1)  編輯 收藏 引用 所屬分類: 算法相關

            Feedback

            # re: 隊列和棧的基本操作 2011-12-11 07:19 jemmyLiu

            博主判斷隊列滿的情況好像不對
            bool full(Queue* queue)
            {
            if (queue->head != 0)
            {
            return (queue->head)%N == queue->tail;
            }

            return false;
            }
            是否是我寫錯了 請指教  回復  更多評論   


            久久精品国产91久久麻豆自制 | 久久久久久毛片免费播放| 久久一本综合| 精品伊人久久大线蕉色首页| 亚洲人成网亚洲欧洲无码久久 | 国产亚州精品女人久久久久久| 日本欧美国产精品第一页久久| 久久热这里只有精品在线观看| 2021久久国自产拍精品| 久久人妻少妇嫩草AV蜜桃| 激情伊人五月天久久综合| 亚洲国产成人久久一区久久| 国产亚洲精品美女久久久| 色婷婷综合久久久久中文字幕 | 国产高潮国产高潮久久久91| 精品久久亚洲中文无码| 久久精品二区| 久久香蕉综合色一综合色88| 国内精品人妻无码久久久影院导航| 久久综合九色综合97_久久久| 亚洲精品美女久久777777| 久久国产一片免费观看| 久久国产乱子伦免费精品| 久久亚洲av无码精品浪潮| 久久青草国产精品一区| 99久久99久久精品免费看蜜桃| 久久强奷乱码老熟女网站| 亚洲精品WWW久久久久久| 丰满少妇人妻久久久久久4| 97久久超碰国产精品2021| 乱亲女H秽乱长久久久| 一本一本久久A久久综合精品| 亚洲国产香蕉人人爽成AV片久久 | 97久久超碰成人精品网站| 久久久久免费看成人影片| 一本色道久久99一综合| 中文字幕乱码人妻无码久久| 久久亚洲欧美国产精品 | 精品国产日韩久久亚洲| 一本色综合久久| 亚洲精品无码久久久久去q|