Question

In: Computer Science

Write a code to implement a python stack class using linked list. use these operations isEmpty...

Write a code to implement a python stack class using linked list. use these operations

isEmpty   • push. • pop.   • peek. • size

Time and compare the performances ( this is optional but I would appreciate it)

Solutions

Expert Solution

# Node structure
class Node:
    def __init__(self, key, next=None):
        self.key = key
        self.next = next

# Main Stack structure
class Stack:

    def __init__(self):
        self.top = None
        self.count = 0

    # Utility function to add an element x in the stack
    def push(self, x):  # insert at the beginning

        # Allocate the new node in the heap
        node = Node(x)

        # check if stack  is full. Then inserting an element would
        # lead to stack overflow
        if node is None:
            print("Stack Overflow")
            return

        print("Pushing", x)

        # set the data in allocated node
        node.data = x

        # Set the .next pointer of the new node to point to the current
        # top node of the list
        node.next = self.top

        # update top pointer
        self.top = node
        self.count+=1
    def size(self):
        print("size of the current Stack is",self.count)
    # Utility function to check if the stack is empty or not
    def isEmpty(self):
        return self.top is None

    # Utility function to return top element in a stack
    def peek(self):

        # check for empty stack
        if not self.isEmpty():
            return self.top.data
        else:
            print("Stack is empty")
            return -1

    # Utility function to pop top element from the stack
    def pop(self):  # remove at the beginning

        # check for stack underflow
        if self.top is None:
            print("Stack Underflow")
            return

        print("Poping", self.peek())

        # update the top pointer to point to the next node
        self.top = self.top.next
        self.count-=1


if __name__ == '__main__':

    stack = Stack()

    stack.push(1)
    stack.push(2)
    stack.push(3)
    stack.size()
    print("Top element is", stack.peek())

    stack.pop()
    stack.size()
    stack.pop()
    stack.pop()

    if stack.isEmpty():
        print("Stack is empty")
    else:
        print("Stack is not empty")

Related Solutions

Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
This is the code what I have for doubly linked list for STACK. This is Python...
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List? ####THIS IS THE ENTIRE ASSIGNMENT#### ADD the Following feature: Include a class attribute in the container class called name. In the implementation - Pod: You should ask the user to enter the name of the container and the program should...
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack...
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack • When an operand is read, push it on statck • When an operator is read i.e +, *. /, - – Pop two from the top of the stack and apply the operator and push the result on stack if there is one value instead of two display an error message • Keep repeating until an equal sign, = is read; pop from...
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
All code should be in Python 3. Implement the Stack Class, using the push, pop, str,...
All code should be in Python 3. Implement the Stack Class, using the push, pop, str, init methods, and the insurance variable 'list'.
(a) Write a stack class that is based on a linked list. It can be just...
(a) Write a stack class that is based on a linked list. It can be just pop(), push(), and anything you need for those methods or testing. (b) Write a queue class that is based on a linked list. As above, it can be just enqueue() and dequeue(), as well as anything you need for those methods or testing. (c) Write some test cases, trying to include edge cases. Why did you choose those tests? Did you get the results...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the...
Please write a python code for the following. Use dictionaries and list comprehensions to implement the functions defined below. You are expected to re-use these functions in implementing other functions in the file. Include a triple-quoted string at the bottom displaying your output. Here is the starter outline for the homework: a. def count_character(text, char): """ Count the number of times a character occurs in some text. Do not use the count() method. """ return 0 b. def count_sentences(text): """...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT