Untitled

 avatar
shinta0x01
python
2 months ago
18 kB
3
Indexable
Never
class Stack:
    def __init__(self):
        self.top = None

    def push(self, data):
        node = Node(data)
        node.next = self.top
        self.top = node

    def pop(self):
        popped_data = self.top.data
        self.top = self.top.next
        return popped_data

    def peek(self):
        if self.top is None:
            return None
        else:
            return self.top.data

    def is_palindrome(self, str1):
        for i in str1:
            self.push(i)

        for i in str1:
            if i == self.peek():
                self.pop()
            else:
                self.pop()
                return False
        return True

    def backtrack(self):
        current = self.top
        while current:
            if current.data < 0:
                for i in range(5):
                    self.pop()
            current = current.next

    def concatenate(self, other_stack):
        if other_stack.top is None:
            return

        # Traverse to the end of the current stack
        current = self.top
        while current.next:
            current = current.next

        # Concatenate the other stack to the end of the current stack
        current.next = other_stack.top

        # Clear the other stack after concatenation
        other_stack.top = None

    def display(self):
        current = self.top
        while current:
            print(current.data, '', end='')
            current = current.next

    def min(self):
        current = self.top
        minimum = self.top.data
        while current:
            if minimum < current.data:
                minimum = minimum
            else:
                minimum = current.data
            current = current.next

        return minimum

    def max(self):
        current = self.top
        maximum = self.top.data
        while current:
            if maximum > current.data:
                maximum = maximum
            else:
                maximum = current.data
            current = current.next
        return maximum

    def is_empty(self):
        return self.top is None

    def size(self):
        count = 0
        current = self.top
        while current:
            count += 1
            current = current.next
        return count

    def clear(self):
        self.top = None

    def search(self, data):
        current = self.top
        position = 0
        while current:
            if current.data == data:
                return position
            position += 1
            current = current.next
        return None

    def reverse(self):
        temp_stack = Stack()
        while self.top:
            temp_stack.push(self.pop())
        self.top = temp_stack.top

    def copy(self):
        copied_stack = Stack()
        current = self.top

        while current is not None:
            copied_stack.push(current.data)
            current = current.next

        return copied_stack


stack = Stack()
stack1 = Stack()
stack.push(0)
stack.push(2)
stack.push(4)
stack.push(8)
stack.display()
print()
print('Minimum', stack.min())
print('Maximum', stack.max())


class Linear:
    def __init__(self):
        self.front = self.rear = None

    def enqueue(self, data):
        node = Node(data)
        if self.rear is None:
            self.front = self.rear = node
        else:
            self.rear.next = node
            self.rear = node

    def dequeue(self):
        if self.rear is None:
            self.is_empty()
        else:
            self.front = self.front.next

    def front_q(self):
        if self.rear is None:
            self.is_empty()
        else:
            print(self.front.data)

    def rear_q(self):
        if self.rear is None:
            self.is_empty()
        else:
            print(self.rear.data)

    def is_empty(self):
        if self.rear is None:
            return 'Queue is empty'

    def display(self):
        current = self.front
        if self.rear is None:
            self.is_empty()
        else:
            while current:
                print(current.data, ' ', end='')
                current = current.next

que = Linear()
que.enqueue(10)
que.enqueue(9)
que.enqueue(1)
que.enqueue(4)
que.enqueue(2)
que.dequeue()
que.dequeue()
que.display()
que.front_q()
que.rear_q()

class Circular:
    def __init__(self):
        self.front = self.rear = None

    def enqueue(self, data):
        node = Node(data)
        if self.rear is None:
            self.front = self.rear = node
            self.rear.next = self.front
        else:
            self.rear.next = node
            self.rear = node
            self.rear.next = self.front

    def dequeue(self):
        if self.rear is None:
            self.is_empty()
        else:
            self.front = self.front.next
            self.rear.next = self.front

    def front_q(self):
        if self.rear is None:
            self.is_empty()
        else:
            print(self.front.data)

    def rear_q(self):
        if self.rear is None:
            self.is_empty()
        else:
            print(self.rear.data)

    def is_empty(self):
        if self.rear is None:
            print('List is empty')

    def display(self):
        current = self.front
        if self.rear is None:
            self.is_empty()
        else:
            while current:
                print(current.data, ' ', end='')
                current = current.next
                if current == self.front:
                    break

que = Circular()
que.enqueue(10)
que.enqueue(8)
que.enqueue(1)
que.enqueue(3)
que.enqueue(7)
que.dequeue()
que.display()
print()
que.front_q()
que.rear_q()



class DEque:
    def __init__(self):
        self.front = self.rear = None

    def enqueue_front(self, data):
        node = Node(data)
        if self.rear is None:
            self.front = self.rear = node
        else:
            node.next = self.front
            self.front = node
    def enqueue_rear(self, data):
        node = Node(data)
        if self.rear is None:
            self.front = self.rear = node
        else:
            self.rear.next = node
            self.rear = node

    def dequeue_front(self):
        if self.rear is None:
            self.is_empty()
        else:
            self.front = self.front.next

    def dequeue_rear(self):
        current = self.front
        current_prev = None
        if self.rear is None:
            self.is_empty()
        else:
            while current.next is not None:
                current_prev = current
                current = current.next
            current_prev.next = None
            self.rear = current_prev

    def front_q(self):
        if self.rear is None:
            self.is_empty()
        else:
            print(self.front.data)

    def rear_q(self):
        if self.rear is None:
            self.is_empty()
        else:
            print(self.rear.data)

    def is_empty(self):
        if self.rear is None:
            print('List is empty')

    def display(self):
        current = self.front
        if self.rear is None:
            self.is_empty()
        else:
            while current:
                print(current.data, ' ', end='')
                current = current.next

que = DEque()
que.enqueue_front(13)
que.enqueue_front(1)
que.enqueue_front(3)
que.enqueue_rear(12)
que.enqueue_front(222)
# que.dequeue_rear()
que.dequeue_front()
que.rear_q()
que.front_q()
que.display()


class Priority:
    def __init__(self):
        self.front = self.rear = None

    def add_task(self, task_name, priority=None):
        node = Node(task_name, priority)
        if self.front is None or self.rear is None:
            self.front = self.rear = node
            return
        if priority is None or self.rear.priority and priority > self.rear.priority:
            self.rear.next = node
            self.rear = node
            return
        if self.front is None or self.front.priority and priority < self.front.priority:
            node.next = self.front
            self.front = node
            return
        current = self.front
        while current.next:
            if current.next.priority is None or priority <= current.next.priority:
                break
            current = current.next
        temp = current.next
        current.next = node
        node.next = temp

    def get_task(self):
        current = self.front
        while current.next:
            print(current.task_name, current.priority, ' ', end='')
            current = current.next

    def change_task(self, task_name, new_priority):
        current = self.front
        if self.front.task_name == task_name:
            self.front = self.front.next
            self.add_task(task_name, new_priority)
        else:
            while current.task_name != task_name:
                pre_current = current
                current = current.next
            if self.rear.task_name == task_name:
                pre_current.next = None
                self.rear = pre_current
            else:
                pre_current.next = current.next
            self.add_task(task_name, new_priority)

p = Priority()
p.add_task(3, 4)
p.add_task(1, 2)
p.add_task(5, 6)
p.add_task(4, 9)
p.add_task(7, 1)
p.change_task(4, 3)
p.get_task()


class SinglyLinked:
    def __init__(self):
        self.head = None

    def insert_begin(self, data):
        node = Node(data)
        node.next = self.head
        self.head = node

    def insert_end(self,data):
        node = Node(data)
        current = self.head
        if self.head is None:
            self.head = node
        else:
            while current.next:
                current = current.next
            current.next = node

    def insert_before(self, key, data):
        node = Node(data)
        current = self.head
        if self.is_empty():
            self.is_empty()
            return
        if self.head.data == key:
            node.next = self.head
            self.head = node
            return
        while current.data != key:
            prev = current
            current = current.next
            if current.next is None and key != current.data:
                print('key not found')
                break

        prev.next = node
        node.next = current

    def insert_after(self, key, data):
        node = Node(data)
        current = self.head
        if self.is_empty():
            self.is_empty()
            return

        if self.head.data == key:
            node.next = current.next
            self.head.next = node
            return

        while current.data != key:
            current = current.next
            post_current = current.next
        current.next = node
        node.next = post_current

    def del_first(self):
        if self.is_empty():
            self.is_empty()
        else:
            self.head = self.head.next

    def del_last(self):
        current = self.head
        if self.is_empty():
            self.is_empty()
        else:
            while current.next:
                prev = current
                current = current.next
            prev.next = None

    def del_given(self, key):
        current = self.head
        if self.is_empty():
            self.is_empty()
        else:
            while current.data != key:
                prev = current
                current = current.next
                post_current = current.next
            prev.next = post_current

    def is_empty(self):
        if self.head is None:
            print('empty')

    def display(self):
        current = self.head
        while current:
            print(current.data, ' ', end='')
            current = current.next


s = Singly()
s.insert_begin(2)
s.insert_begin(4)
s.insert_begin(1)
s.insert_begin(6)
s.insert_before(6,12)
s.insert_after(12,100)
s.del_first()
s.del_last()
s.del_given(6)
s.display()


class CircularLinked:
    def __init__(self):
        self.head = self.tail = None

    def insert_begin(self, data):
        node = Node(data)
        if self.head is None:
            self.head = self.tail = node
            self.tail.next = self.head
        else:
            node.next = self.head
            self.head = node
            self.tail.next = self.head

    def insert_end(self, data):
        node = Node(data)
        current = self.head
        if self.head is None:
            self.head = self.tail = node
            self.tail.next = self.head
        else:
            while current.next != self.head:
                current = current.next
            current.next = node
            self.tail = node
            self.tail.next = self.head

    def insert_before(self, key, data):
        node = Node(data)
        current = self.head
        if self.head is None:
            self.is_empty()
            return

        if key == self.head.data:
            node.next = self.head
            self.head = node
            self.tail.next = self.head
            return

        while current.data != key:
            prev = current
            current = current.next
            if current == self.head:
                break
        prev.next = node
        node.next = current

    def insert_after(self, key, data):
        node = Node(data)
        current = self.head
        if self.head is None:
            self.is_empty()
            return
        if key == self.head.data:
            node.next = current.next
            self.head.next = node
            return
        while current.data != key:
            current = current.next
            post_current = current.next
            if current.data == key and current == self.tail:
                self.tail.next = node
                self.tail = node
                self.tail.next = self.head
                return
        current.next = node
        node.next = post_current

    def del_start(self):
        if self.head is None:
            self.is_empty()
        else:
            self.head = self.head.next

    def del_end(self):
        current = self.head
        while current.next != self.head:
            prev = current
            current = current.next
        prev.next = None
        self.tail = prev
        self.tail.next = self.head

    def is_empty(self):
        if self.head is None:
            print('empty')

    def display(self):
        current = self.head
        while True:
            print(current.data, ' ', end='')
            current = current.next
            if current == self.head:
                break

s = Circular()
s.insert_begin(3)
s.insert_begin(4)
s.insert_begin(1)
s.insert_begin(7)
s.insert_end(9)
s.insert_before(9,10)
s.insert_after(9,11)
s.del_end()
s.del_start()
s.display()



class DoublyLinked:
    def __init__(self):
        self.head = None

    def insert_begin(self, data):
        node = Node(data)
        if self.head is None:
            self.head = node
        else:
            node.next = self.head
            self.head.prev = node
            self.head = node

    def insert_end(self, data):
        node = Node(data)
        current = self.head
        if self.head is None:
            self.head = node
        else:
            while current.next:
                current = current.next
            current.next = node
            node.prev = current

    def insert_before(self, key, data):
        node = Node(data)
        current = self.head
        if self.head is None:
            self.is_empty()
            return
        if key == current.data:
            node.next = self.head
            self.head.prev = node
            self.head = node
            return
        while current.data != key:
            current = current.next
        node.next = current
        node.prev = current.prev
        current.prev.next = node
        current.prev = node

    def insert_after(self, key, data):
        node = Node(data)
        current = self.head

        if self.head is None:
            self.is_empty()
            return
        while current and current.data != key:
            current = current.next
        if not current:
            print("Key not found")
            return
        node.prev = current
        node.next = current.next
        if current.next:
            current.next.prev = node
        current.next = node

    def del_start(self):
        if self.head is None:
            self.is_empty()
        else:
            self.head = self.head.next
            self.head.prev = None

    def del_end(self):
        current = self.head
        while current.next:
            prev = current
            current = current.next
        prev.next = None

    def del_given(self, key):
        pass


    def is_empty(self):
        if self.head is None:
            print('empty')

    def display(self):
        current = self.head
        while current:
            print(current.data, ' ', end='')
            current = current.next

s = Doubly()
s.insert_begin(3)
s.insert_begin(1)
s.insert_begin(5)
s.insert_begin(2)
s.insert_end(10)
s.insert_end(101)
s.insert_before(2,11111)
s.insert_after(101,100)
s.del_start()
s.del_end()
s.display()
Leave a Comment