Skip to content
Course Notes: Data Structures and Algorithms in Python
  • AI Chat
  • Code
  • Report
  • Course Notes

    Use this workspace to take notes, store code snippets, and build your own interactive cheatsheet!

    Note that the data from the course is not yet added to this workspace. You will need to navigate to the course overview page, download any data you wish to use, and add it to the file browser.

    # Import any packages you want to use here
    

    Take Notes

    Add notes here about the concepts you've learned and code cells with code you want to keep.

    Add your notes here

    # Add your code snippets here
    class Node:
        def __init__(self, data):
            self.data = data
            self.next = None
    
    
    class LinkedList:
          def __init__(self):
            # Set the head and the tail with null values
            self.head = None
            self.tail = None
    
        def insert_at_beginning(self, data):
            # Create the new node
            new_node = Node(data)
            # Check whether the linked list has a head node
            if self.head:
              # Point the next node of the new node to the head
              new_node.next = self.head
              self.head = new_node
            else:
              self.tail = new_node      
              self.head = new_node
        
        def insert_at_end(self, data):
            # Create the new node
            new_node = Node(data)
            # Check whether the linked list has a head node
            if self.tail:
              # Point the next node of the new node to the head
              self.tail.next = new_node
              self.tail = new_node
            else:
              self.tail = new_node      
              self.head = new_node
        
        def insert_at_given_position(self, data, k):
            new_node = Node(data)
            temp = self.head
            for k in range(k):
                temp = temp.next
            new_node.next = temp.next
            temp.next = new_node
    
        def remove_at_beginning(self):
            # The "next" node of the head becomes the new head node
            self.head = self.head.next
    
        def remove_at_end(self):
            # The "next" node of the head becomes the new head node
            if self.head == None:
                self.head = None
            if self.head.next == None:
                self.head = None
    
            second_last = self.head
            while(second_last.next.next):
                second_last = second_last.next
            
            second_last.next = None
            return self.tail = second_last
    
        def search(self, data):
            current_node = self.head
            while current_node:
                if current_node.data == data:
                    return True
                else:
                    current_node = current_node.next
            return False
    
        class Stack:
            def __init__(self):
                # Initially there won't be any node at the top of the stack
                self.top = None
                # Initially there will be zero elements in the stack
                self.size = 0
            
            def push(self, data):
                # Create a node with the data
                new_node = Node(data)
                if self.top:
                  new_node.next = self.top
                # Set the created node to the top node
                self.top = new_node
                # Increase the size of the stack by one
                self.size += 1
                
          def pop(self):
                # Check if there is a top element
                if self.top is None:
                  return None
                else:
                  popped_node = self.top
                  # Decrement the size of the stack
                  self.size -= 1
                  # Update the new value for the top node
                  self.top = self.top.next
                  popped_node.next = None
                  return popped_node.data