Question

using python file and screenshot the file that show up is correct Your input data for...

using python file and screenshot the file that show up is correct

Your input data for each application is:

           

7   1   8   9   4   2   5   10   6   3

Treat this data as integer data type.

Push onto and pop off of each of the input items onto a stack implemented in Python.   Identify the item 4 when it is popped off of your stack.

2. Feed this same input data into a Queue implemented in Python.   Identify the item 4 when it is removed from the front of your queue.

3. Feed this same input data into a doubly linked list in its unsorted order. Traverse this list, find the element 4 and remove it from the list. Then remove the last element on this list.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

->For the first question "Stack implementation " code is given below.

Answer code

class Stack:

    # Constructor
    def __init__(self):
        self.stack = list()
        self.maxSize = 10
        self.top = 0

    # Adds element to the Stack
    def push(self, data):
        if self.top >= self.maxSize:
            return ("Stack Full!")
        self.stack.append(data)
        self.top += 1
        return True

    # Removes element from the stack
    def pop(self):
        if self.top <= 0:
            return ("Stack Empty!")
        item = self.stack.pop()
        self.top -= 1
        return item

    # Size of the stack
    def size(self):
        return self.top


s = Stack()
(s.push(7))
(s.push(8))
(s.push(9))
(s.push(4))
(s.push(2))
(s.push(5))
(s.push(6))
(s.push(3))
while s.size() >0 :
    temp=s.pop()
    if temp ==4:
        print("Value 4 is detected ")
    else:
        print ("pop operation executed \n")

For the second question "queue implementation " code is given below.

Answer code

class Queue:


    def __init__(self):
        self.queue = list()
        self.maxSize = 10
        self.head = 0
        self.tail = 0

    # Adding elements
    def enqueue(self, data):
        # Checking if the queue is full
        if self.size() >= self.maxSize:
            return ("Queue Full")
        self.queue.append(data)
        self.tail += 1
        return True

        # Deleting elements

    def dequeue(self):
        # Checking if the queue is empty
        if self.size() <= 0:
            self.resetQueue()
            return ("Queue Empty")
        data = self.queue[self.head]
        self.head += 1
        return data

    # Calculate size
    def size(self):
        return self.tail - self.head

    # Reset queue
    def resetQueue(self):
        self.tail = 0
        self.head = 0
        self.queue = list()


q = Queue()
#Values are entered in the format given =7   1   8   9   4   2   5   10   6   3


(q.enqueue(7))
(q.enqueue(1))
(q.enqueue(8))
(q.enqueue(9))
(q.enqueue(4))
(q.enqueue(2))
(q.enqueue(5))
(q.enqueue(10))
(q.enqueue(6))
(q.enqueue(3))
while q.size() > 0:
    temp=q.dequeue()
    if temp == 4:
        print("Value 4 is detected and dequeued \n")
    else:
        print("Dequeue operation is done \n")

For the second question "Double link list " code is given below.

Answer code

class Node:

    def __init__(self, data, nextNode=None):
        self.data = data
        self.nextNode = nextNode

    def getData(self):
        return self.data

    def setData(self, val):
        self.data = val

    def getNextNode(self):
        return self.nextNode

    def setNextNode(self, val):
        self.nextNode = val




class LinkedList:

    def __init__(self, head=None):
        self.head = head
        self.size = 0

    def getSize(self):
        return self.size

    def addNode(self, data):
        newNode = Node(data, self.head)
        self.head = newNode
        self.size += 1
        return True

    def printNode(self):
        curr = self.head
        while curr:
            print(curr.data)
            curr = curr.getNextNode()

    def removeNode(self, value):

        prev = None
        curr = self.head
        while curr:
            if curr.getData() == value:
                if prev:
                    prev.setNextNode(curr.getNextNode())
                else:
                    self.head = curr.getNextNode()
                return True

            prev = curr
            curr = curr.getNextNode()

        return False






myList = LinkedList()
print("Inserting 7   1   8   9   4   2   5   10   6   3 \n")
(myList.addNode(7))
(myList.addNode(1))
(myList.addNode(8))
(myList.addNode(9))
(myList.addNode(4))
(myList.addNode(2))
(myList.addNode(5))
(myList.addNode(10))
(myList.addNode(6))
(myList.addNode(3))
temp=(myList.removeNode(4))
if temp==True:
    print("Value 4 is removed successfully ")

Hope i was able to help you

Add a comment
Know the answer?
Add Answer to:
using python file and screenshot the file that show up is correct Your input data for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. The Operand Stack - opstack The operand stack should be implemented as a Python list....

    1. The Operand Stack - opstack The operand stack should be implemented as a Python list. The list will contain Python integers, strings, and later in Part 2 code arrays. Python integers and lists on the stack represent Postscript integer constants and array constants. Python strings which start with a slash / on the stack represent names of Postscript variables. When using a list as a stack, assume that the top of the stack is the end of the list...

  • 1)Given a Stack implemented with a Linked List, and an O(1) implementation of push and pop,show...

    1)Given a Stack implemented with a Linked List, and an O(1) implementation of push and pop,show the Linked List after the following commands are executed: Stack myStack = new Stack(); myStack.push(20); myStack.push(40); myStack.pop(); myStack.push(60); myStack.push(80); 2)If the same commands were used but the Stack was implemented with an Array with maximum size of 10, show what the array would look like after all these commands are executed. Assume O(1) implementation of push and pop here as well. 3)Given a Queue...

  • I was told I need three seperate files for these classes is there anyway to tie...

    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: ");...

  • (Deque) A deque is a data structure consisting of a list of items on which the...

    (Deque) A deque is a data structure consisting of a list of items on which the following operations are possible: a. push (x) : Insert item x on the front end of the deque. b. pop ): Remove the front item from the deque and return it. c. inject(x): Insert item x on the rear end of the deque. d. eject ): Remove the rear item from the deque and return it. Write routines to support the deque that take...

  • Python program - Write a Python program, in a file called sortList.py, which, given a list...

    Python program - Write a Python program, in a file called sortList.py, which, given a list of names, sorts the names into alphabetical order. Use a one dimensional array to hold the list of names. To do the sorting use a simple sorting algorithm that repeatedly takes an element from the unsorted list and puts it in alphabetical order within the same list. Initially the entire list is unsorted. As each element is placed in alphabetical order, the elements in...

  • 1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class...

    1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...

  • In this assignment, you are given several classes in the cpp file “DList.cpp”. Your task is...

    In this assignment, you are given several classes in the cpp file “DList.cpp”. Your task is to complete the implementation of the classes specified as below. Y 1 Your Task You are given a class “Item” that contains one integer value, and two pointers. You are going to build a doubly linked list class DLinkedList. I describe the tasks below. Task 1: Implement the constructors (default and copy) of DLinkedList. You need to make sure that the copy constructor makes...

  • Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DE...

    Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DEFINITIONS NEEDED for the OPERATIONS. OUTPUT: PRINT ALL THE ELEMENTS ON THE STACK. Stack Operations initializestack: Initializes the stack to an empty state. isEmptyStack: Determines whether the stack is empty. If the stack is empty, it returns the value true; otherwise, it returns the value false. isFul1stack: Determines whether the stack...

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

  • Write a PYTHON program that reads a file (prompt user for the input file name) containing...

    Write a PYTHON program that reads a file (prompt user for the input file name) containing two columns of floating-point numbers (Use split). Print the average of each column. Use the following data forthe input file: 1   0.5 2   0.5 3   0.5 4   0.5 The output should be:    The averages are 2.50 and 0.5. a)   Your code with comments b)   A screenshot of the execution Version 3.7.2

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT