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:
![In [10]: class Queue: def __init__(self): #initializing empty queue self.items=[ def dequeue (self): #checking if the queue i](http://img.homeworklib.com/questions/77e948b0-bf23-11eb-afa6-77de8083e06d.png?x-oss-process=image/resize,w_560)
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...
Priority Queue Using A List in Python I would like to pass in a list to this class and modify the list using these functions only. Use a list of size 10 and use each of the functions with that list. class PQ_List(object): def __init__(self, sampleList): print ("creates an unsorted list from passed in list") # # Returns the list def enQueue(self, item): print ("adds an item to the PQ") # Add an item to the PQ def...
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: ");...
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...
I need help fixing my code. My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...