help finish Queue, don't think I have the right thing.
Hi,
Hope you are doing fine. You have done a great job with the code of Stack and even queue class. I see that you need help with the queue. In the code, you have only made a few eerors in the class definitions of the Queue class. I have corrected the code and have executed it successfully in jupyter notebook. The code has been explained using comments that have been highlighted in bold. I haven't put any explanation for the testQueue() class as the print statements give you the whole picture. Also, have a look at the code snippet and sample output get a clearer insight on the indentation and output of the code. Also note that I haven't made any new methods and have corrected only the existing ones.
Program:
class Queue:
def __init__(self):
#initializing empty queue
self.items=[]
def dequeue(self):
#checking if the queue is not empty
if(len(self.items)>0):
#we delete the first element of queue as it follows FIFO
priciple
front=self.items.pop(0)
#returning the deleted element
return front
#if the queue is empty
else:
print("Error: you can't dequeue from an empty queue")
#gives the size of queue
def size(self):
return len(self.items)
#adds elements to the queue
def enqueue(self,item):
#we simply append the item to the list.
self.items.append(item)
#Testing class
def testQueue():
q=Queue()
print("Created an empty Queue")
print("Size is now: {}".format(q.size()))
print("Enqueue-ing: 3, then 'hi', then 99")
q.enqueue(3)
q.enqueue("hi")
q.enqueue(99)
print("Size is now: {}".format(q.size()))
print("Dequeue-ing ...")
print(q.dequeue())
print("Size is now: {}".format(q.size()))
print("Dequeue-ing ...")
print(q.dequeue())
print("Size is now: {}".format(q.size()))
print("Enqueue-ing: [1,2]")
#the list [1,2] is taken as one element and not two.
q.enqueue([1,2])
print("Size is now: {}".format(q.size()))
print("Dequeue-ing ...")
print(q.dequeue())
print("Size is now: {}".format(q.size()))
print("Dequeue-ing ...")
print(q.dequeue())
print("Size is now: {}".format(q.size()))
print(q.dequeue())
print("Size is now: {}".format(q.size()))
testQueue()
Executable code snippet:
Sample output:
help finish Queue, don't think I have the right thing. # 1. After studying the Stack...
HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...
In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...
I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> { // Data fields private Node<T> topNode; // references the first node in the chain private int numberOfEntries; public...
in python and according to this #Creating class for stack class My_Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def Push(self, d): self.items.append(d) def Pop(self): return self.items.pop() def Display(self): for i in reversed(self.items): print(i,end="") print() s = My_Stack() #taking input from user str = input('Enter your string for palindrome checking: ') n= len(str) #Pushing half of the string into stack for i in range(int(n/2)): s.Push(str[i]) print("S",end="") s.Display() s.Display() #for the next half checking the upcoming string...
JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working fine, can you fix it please! MyQueue.java Implement a queue using the MyStack.java implementation as your data structure. In other words, your instance variable to hold the queue items will be a MyStack class. enqueue(String item): inserts item into the queue dequeue(): returns and deletes the first element in the queue isEmpty(): returns true or false...
Implement the stack queue data structure with a linked list implementation to get the given test code in driver.cpp to work properly: driver.cpp code: #include <iostream> #include "stackLL.h" using namespace std; int main() { /////////////Test code for stack /////////////// stackLL stk; stk.push(5); stk.push(13); stk.push(7); stk.push(3); stk.push(2); stk.push(11); cout << "Popping: " << stk.pop() << endl; cout << "Popping: " << stk.pop() << endl; stk.push(17); stk.push(19); stk.push(23); while( ! stk.empty() ) { cout << "Popping: " << stk.pop() << endl; }...
I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts { public static void main(String[] args) { Scanner input = new Scanner(System.in); String sentence; String again; do { System.out .println("Enter a sentence, I will tell you if it is a palindrome: ");...
Need help the first picture is the code I have so far the second are the requirements. It’s a deque array in python class dequeArray: DEFAULT-CAPACITY 10 #moderate capacity for all new queues def init (self): self.capacity-5 capacity self.capacity self.data self, make array(self. capacity) self. size self. front-θ def len (self): return self. size def _getiten-(self, k); #Return element at index k if not θ k < self. size: raise IndexError('invalid index) return self._data[k] def isEmpty(self): if self. data0: return...
Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue (ABQ). A stack is a linear data structure which follows the Last-In, First-Out (LIFO) property. LIFO means that the data most recently added is the first data to be removed. (Imagine a stack of books, or a stack of papers on a desk—the first one to be removed is the last one placed on top.) A queue...
Design and implement a class Q that uses Q.java as a code base. The queue ADT must use class LinkedList from Oracle's Java class library and its underlying data structure (i.e. every Q object has-a (contains) class LinkedList object. class Q is not allowed to extend class LinkedList. The methods that are to be implemented are documented in Q.java. Method comment blocks are used to document the functionality of the class Q instance methods. The output of your program must...