• <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

            隊列和棧的基本操作

                                          隊列和棧的基本操作

                 棧的操作,棧是一種先進后出的操作,需要注意的是棧的滿與空,??盏那闆r是 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;
            }
            是否是我寫錯了 請指教  回復  更多評論   


            伊人久久免费视频| 久久人人爽人人人人爽AV| 久久99国产综合精品| 久久久久亚洲AV无码永不| 久久国产色AV免费看| 狠狠色丁香久久综合婷婷| 久久久久亚洲爆乳少妇无 | 99久久久精品| 精品无码人妻久久久久久| 久久丫忘忧草产品| 青青草原综合久久| 熟妇人妻久久中文字幕| 嫩草影院久久99| 人妻少妇久久中文字幕一区二区| .精品久久久麻豆国产精品| 久久国产香蕉一区精品| 久久人人爽人人爽人人片av高请 | 久久综合给合久久狠狠狠97色69| 国产成人无码精品久久久免费 | 2021国产精品久久精品| 色综合久久精品中文字幕首页| 国产美女亚洲精品久久久综合 | 亚洲人成无码www久久久| 久久成人国产精品二三区| 国产成人综合久久精品红| 久久成人18免费网站| 69久久精品无码一区二区| 97精品伊人久久久大香线蕉 | 午夜精品久久久久| 国产福利电影一区二区三区久久久久成人精品综合 | 亚洲欧洲久久av| 欧美激情精品久久久久久| 品成人欧美大片久久国产欧美| 99久久99久久久精品齐齐| 伊人久久大香线蕉亚洲五月天 | 亚洲精品乱码久久久久久按摩| 亚洲国产成人久久综合一区77 | 久久国产精品99精品国产987| 欧美大香线蕉线伊人久久| 久久婷婷五月综合97色直播| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 |