classMyQueue { private: // store elements vector<int> data; // a pointer to indicate the start position int p_start; public: MyQueue() {p_start = 0;} /** Insert an element into the queue. Return true if the operation is successful. */ //入队增加元素 boolenQueue(int x){ data.push_back(x); returntrue; } /** Delete an element from the queue. Return true if the operation is successful. */ //出队删除一个元素 booldeQueue(){ if (isEmpty()) { returnfalse; } p_start++; //队列头部索引后移 returntrue; }; /** Get the front item from the queue. */ //队列第一个元素 intFront(){ return data[p_start]; }; /** Checks whether the queue is empty or not. */ //判断是否为空 boolisEmpty(){ return p_start >= data.size(); } };
// "static void main" must be defined in a public class.
classMyQueue{ // store elements private List<Integer> data; // a pointer to indicate the start position privateint p_start; publicMyQueue(){ data = new ArrayList<Integer>(); p_start = 0; } /** Insert an element into the queue. Return true if the operation is successful. */ publicbooleanenQueue(int x){ data.add(x); returntrue; }; /** Delete an element from the queue. Return true if the operation is successful. */ publicbooleandeQueue(){ if (isEmpty() == true) { returnfalse; } p_start++; returntrue; } /** Get the front item from the queue. */ publicintFront(){ return data.get(p_start); } /** Checks whether the queue is empty or not. */ publicbooleanisEmpty(){ return p_start >= data.size(); } };
publicclassMain{ publicstaticvoidmain(String[] args){ MyQueue q = new MyQueue(); q.enQueue(5); q.enQueue(3); if (q.isEmpty() == false) { System.out.println(q.Front()); } q.deQueue(); if (q.isEmpty() == false) { System.out.println(q.Front()); } q.deQueue(); if (q.isEmpty() == false) { System.out.println(q.Front()); } } }
classMyCircularQueue { private: vector<int> data; int head; int tail; int size; public: /** Initialize your data structure here. Set the size of the queue to be k. */ MyCircularQueue(int k) { data.resize(k); //重置size head = -1; tail = -1; size = k; } /** Insert an element into the circular queue. Return true if the operation is successful. */ boolenQueue(int value){ if (isFull()) { returnfalse; } if (isEmpty()) { head = 0; } tail = (tail + 1) % size; data[tail] = value; returntrue; } /** Delete an element from the circular queue. Return true if the operation is successful. */ booldeQueue(){ if (isEmpty()) { returnfalse; } if (head == tail) { head = -1; tail = -1; returntrue; } head = (head + 1) % size; returntrue; } /** Get the front item from the queue. */ intFront(){ if (isEmpty()) { return-1; } return data[head]; } /** Get the last item from the queue. */ intRear(){ if (isEmpty()) { return-1; } return data[tail]; } /** Checks whether the circular queue is empty or not. */ boolisEmpty(){ return head == -1; } /** Checks whether the circular queue is full or not. */ boolisFull(){ return ((tail + 1) % size) == head; } };
/** * Your MyCircularQueue object will be instantiated and called as such: * MyCircularQueue obj = new MyCircularQueue(k); * bool param_1 = obj.enQueue(value); * bool param_2 = obj.deQueue(); * int param_3 = obj.Front(); * int param_4 = obj.Rear(); * bool param_5 = obj.isEmpty(); * bool param_6 = obj.isFull(); */
/** Initialize your data structure here. Set the size of the queue to be k. */ publicMyCircularQueue(int k){ data = newint[k]; head = -1; tail = -1; size = k; } /** Insert an element into the circular queue. Return true if the operation is successful. */ publicbooleanenQueue(int value){ if (isFull() == true) { returnfalse; } if (isEmpty() == true) { head = 0; } tail = (tail + 1) % size; data[tail] = value; returntrue; } /** Delete an element from the circular queue. Return true if the operation is successful. */ publicbooleandeQueue(){ if (isEmpty() == true) { returnfalse; } if (head == tail) { head = -1; tail = -1; returntrue; } head = (head + 1) % size; returntrue; } /** Get the front item from the queue. */ publicintFront(){ if (isEmpty() == true) { return -1; } return data[head]; } /** Get the last item from the queue. */ publicintRear(){ if (isEmpty() == true) { return -1; } return data[tail]; } /** Checks whether the circular queue is empty or not. */ publicbooleanisEmpty(){ return head == -1; } /** Checks whether the circular queue is full or not. */ publicbooleanisFull(){ return ((tail + 1) % size) == head; } }
/** * Your MyCircularQueue object will be instantiated and called as such: * MyCircularQueue obj = new MyCircularQueue(k); * boolean param_1 = obj.enQueue(value); * boolean param_2 = obj.deQueue(); * int param_3 = obj.Front(); * int param_4 = obj.Rear(); * boolean param_5 = obj.isEmpty(); * boolean param_6 = obj.isFull(); */
intmain(){ // 1. Initialize a queue. queue<int> q; // 2. Push new element. q.push(5); q.push(13); q.push(8); q.push(6); // 3. Check if queue is empty. if (q.empty()) { cout << "Queue is empty!" << endl; return0; } // 4. Pop an element.移除一个元素 q.pop(); // 5. Get the first element.获取第一个元素 cout << "The first element is: " << q.front() << endl; // 6. Get the last element.获取最后一个元素 cout << "The last element is: " << q.back() << endl; // 7. Get the size of the queue.长度 cout << "The size is: " << q.size() << endl; }
// "static void main" must be defined in a public class. publicclassMain{ publicstaticvoidmain(String[] args){ // 1. Initialize a queue. Queue<Integer> q = new LinkedList(); // 2. Get the first element - return null if queue is empty. System.out.println("The first element is: " + q.peek()); // 3. Push new element. q.offer(5); q.offer(13); q.offer(8); q.offer(6); // 4. Pop an element. q.poll(); // 5. Get the first element. System.out.println("The first element is: " + q.peek()); // 7. Get the size of the queue. System.out.println("The size is: " + q.size()); } }
classMyStack { private: vector<int> data; // store elements public: /** Insert an element into the stack. */ voidpush(int x){ data.push_back(x); } /** Checks whether the queue is empty or not. */ boolisEmpty(){ return data.empty(); } /** Get the top item from the queue. */ inttop(){ return data.back(); } /** Delete an element from the queue. Return true if the operation is successful. */ boolpop(){ if (isEmpty()) { returnfalse; } data.pop_back(); returntrue; } };
intmain(){ MyStack s; s.push(1); s.push(2); s.push(3); for (int i = 0; i < 4; ++i) { if (!s.isEmpty()) { cout << s.top() << endl; } cout << (s.pop() ? "true" : "false") << endl; } }
// "static void main" must be defined in a public class. classMyStack{ private List<Integer> data; // store elements publicMyStack(){ data = new ArrayList<>(); } /** Insert an element into the stack. */ publicvoidpush(int x){ data.add(x); } /** Checks whether the queue is empty or not. */ publicbooleanisEmpty(){ return data.isEmpty(); } /** Get the top item from the queue. */ publicinttop(){ return data.get(data.size() - 1); } /** Delete an element from the queue. Return true if the operation is successful. */ publicbooleanpop(){ if (isEmpty()) { returnfalse; } data.remove(data.size() - 1); returntrue; } };
publicclassMain{ publicstaticvoidmain(String[] args){ MyStack s = new MyStack(); s.push(1); s.push(2); s.push(3); for (int i = 0; i < 4; ++i) { if (!s.isEmpty()) { System.out.println(s.top()); } System.out.println(s.pop()); } } }
intmain(){ // 1. Initialize a stack. stack<int> s; // 2. Push new element. s.push(5); s.push(13); s.push(8); s.push(6); // 3. Check if stack is empty. if (s.empty()) { cout << "Stack is empty!" << endl; return0; } // 4. Pop an element. s.pop(); // 5. Get the top element. cout << "The top element is: " << s.top() << endl; // 6. Get the size of the stack. cout << "The size is: " << s.size() << endl; }
// "static void main" must be defined in a public class. publicclassMain{ publicstaticvoidmain(String[] args){ // 1. Initialize a stack. Stack<Integer> s = new Stack<>(); // 2. Push new element. s.push(5); s.push(13); s.push(8); s.push(6); // 3. Check if stack is empty. if (s.empty() == true) { System.out.println("Stack is empty!"); return; } // 4. Pop an element. s.pop(); // 5. Get the top element. System.out.println("The top element is: " + s.peek()); // 6. Get the size of the stack. System.out.println("The size is: " + s.size()); } }