• <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 閱讀(798) 評論(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;
            }
            是否是我寫錯了 請指教  回復  更多評論   


            狠狠色丁香婷婷综合久久来 | 久久九九久精品国产免费直播| 精品久久无码中文字幕| 51久久夜色精品国产| 久久久国产精品| 久久婷婷五月综合国产尤物app | 合区精品久久久中文字幕一区 | 中文字幕亚洲综合久久| 久久精品18| 国内精品久久久久久99| 色综合久久中文字幕综合网| 亚洲精品tv久久久久久久久 | 欧美日韩中文字幕久久伊人| 久久精品国产亚洲精品| 久久久亚洲欧洲日产国码aⅴ| 国内精品久久久久影院网站| 国产毛片欧美毛片久久久| 久久综合久久综合久久| 欧美黑人又粗又大久久久| 伊人久久大香线蕉成人| 久久本道久久综合伊人| 久久久久成人精品无码中文字幕 | 久久精品九九亚洲精品| 久久久久久久女国产乱让韩| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久国产一区二区| 久久精品中文无码资源站| 久久夜色精品国产噜噜亚洲a| 成人精品一区二区久久| 精品精品国产自在久久高清| 国内精品伊人久久久久AV影院| 无码国内精品久久人妻蜜桃| 三级三级久久三级久久| 亚洲国产精品无码久久一区二区| 日本WV一本一道久久香蕉| 国内精品久久久久影院老司 | 国产Av激情久久无码天堂| 99久久久国产精品免费无卡顿| 国产三级久久久精品麻豆三级| 久久久精品人妻一区二区三区蜜桃 | 久久综合九色综合网站|