C/C++の宿題を片付けます 91代目at TECH
C/C++の宿題を片付けます 91代目 - 暇つぶし2ch162:デフォルトの名無しさん
07/06/25 16:26:54
>>127
#include <iostream>
#define STACK 10
#define QUEUE 10
typedef int stack;
typedef int queue;
static int sfirst = 0, slast = 0, qfirst = 0, qlast = 0, qcount = 0;
int push(stack *s, int i){ return slast == STACK ? -1 : (s[slast++] = i);}
int pop(stack *s){ return slast == sfirst ? -1 : s[--slast];}
int enqueue(queue *q, int i){
   if(qlast == qfirst && qcount) return -1;
   q[qlast] = i, qcount++;
   qlast = (qlast + 1)%QUEUE;
   return i;
}
int dequeue(queue *q){
   int ret;
   if(qlast == qfirst && !qcount) return -1;
   ret = q[qfirst], qcount--;
   qfirst = (qfirst + 1)%QUEUE;
   return ret;
}
int main(){
   stack s[STACK];
   queue q[QUEUE];
   std::cout << "push" << std::endl; for(int i = 0; i < 13; i++) std::cout << push(s, i) << std::endl;
   std::cout << "pop" << std::endl; for(int i = 0; i < 13; i++) std::cout << pop(s) << std::endl;
   std::cout << "enqueue" << std::endl;for(int i = 0; i < 13; i++) std::cout << enqueue(q, i) << std::endl;
   std::cout << "dequeue" << std::endl;for(int i = 0; i < 13; i++) std::cout << dequeue(q) << std::endl;
   return 0;
}


次ページ
続きを表示
1を表示
最新レス表示
レスジャンプ
類似スレ一覧
スレッドの検索
話題のニュース
おまかせリスト
オプション
しおりを挟む
スレッドに書込
スレッドの一覧
暇つぶし2ch