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


            久久亚洲国产精品123区| 国产精品久久久久9999| 久久婷婷五月综合色奶水99啪 | AV色综合久久天堂AV色综合在| 亚洲va久久久噜噜噜久久狠狠 | 久久久噜噜噜久久| 久久久久久精品免费免费自慰| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 国产精品gz久久久| 思思久久99热只有频精品66| 久久精品国产亚洲77777| 99久久精品国产毛片| 久久综合亚洲色HEZYO社区| 久久精品国产亚洲av麻豆小说| 精品久久国产一区二区三区香蕉| 色欲综合久久中文字幕网| 久久亚洲AV永久无码精品| 国产亚洲欧美成人久久片| 久久午夜福利无码1000合集 | 伊人色综合久久天天网| 国产成人无码精品久久久久免费 | 亚洲欧美成人久久综合中文网 | 久久综合精品国产二区无码| 亚洲国产香蕉人人爽成AV片久久| 久久97精品久久久久久久不卡| 影音先锋女人AV鲁色资源网久久 | 久久国产亚洲高清观看| 久久久久99精品成人片三人毛片| 伊人久久大香线蕉影院95| 久久久无码精品亚洲日韩蜜臀浪潮| 91久久成人免费| 久久国产视屏| 久久99免费视频| 亚洲AV日韩AV永久无码久久| AV色综合久久天堂AV色综合在| 91久久精品国产免费直播| 日产精品久久久久久久| 88久久精品无码一区二区毛片| 亚洲伊人久久综合影院| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 久久久久国产精品麻豆AR影院|