Advertisement
ppathak35

Linked List Visualizer

Jun 15th, 2022 (edited)
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.65 KB | None | 0 0
  1. import pygame
  2.  
  3. pygame.init()
  4. SCREEN = WIDTH, HEIGHT = 640, 380
  5.  
  6. info = pygame.display.Info()
  7. width = info.current_w
  8. height = info.current_h
  9.  
  10. if width >= height:
  11.     win = pygame.display.set_mode(SCREEN, pygame.NOFRAME)
  12. else:
  13.     win = pygame.display.set_mode(SCREEN, pygame.NOFRAME | pygame.SCALED | pygame.FULLSCREEN)
  14.  
  15. clock = pygame.time.Clock()
  16. FPS = 60
  17.  
  18. # COLORS **********************************************************************
  19.  
  20. WHITE = (255, 255, 255)
  21. BLUE = (30, 144,255)
  22. RED = (255, 0, 0)
  23. GREEN = (0, 255, 0)
  24. BLACK = (0, 0, 20)
  25.  
  26. font = pygame.font.SysFont('Arial', 15)
  27. font1 = pygame.font.SysFont("Arial", 20, True)
  28. cross = font1.render("X", True, RED)
  29. visualizer = font1.render("Linked List Visualizer", True, BLUE)
  30.  
  31. textArray = [
  32.     '1. Insert at Front',
  33.     '2. Insert at Rear',
  34.     '3. Delete from Front',
  35.     '4. Delete from Rear',
  36.     '5. Get Linked List Size'
  37. ]
  38.  
  39.  
  40. class Node:
  41.     def __init__(self, data):
  42.         self.data = data
  43.         self.next = None
  44.  
  45. class LinkedList:
  46.     def __init__(self):
  47.         self.start = None
  48.         self.initial = 40
  49.         self.length = 0
  50.  
  51.     def insertFront(self, data):
  52.         node = Node(data)
  53.         if self.start is None:
  54.             self.start = node
  55.         else:
  56.             node.next = self.start
  57.             self.start = node
  58.         self.length += 1
  59.  
  60.     def insertEnd(self, data):
  61.         node = Node(data)
  62.         ptr = self.start
  63.         while (ptr.next is not None):
  64.             ptr = ptr.next
  65.         ptr.next = node
  66.         self.length += 1
  67.  
  68.     def removeFront(self):
  69.         if self.size():
  70.             ptr = self.start
  71.             self.start = self.start.next
  72.             self.length -= 1
  73.             return ptr.data
  74.         return None
  75.  
  76.     def removeRear(self):
  77.         if self.size():
  78.             data = None
  79.             if self.start.next is None:
  80.                 data = self.start.data
  81.                 self.start = None
  82.             else:
  83.                 ptr = self.start
  84.                 while(ptr.next.next is not None):
  85.                     ptr = ptr.next
  86.                 temp = ptr.next
  87.                 ptr.next = None
  88.                 data = temp.data
  89.             self.length -= 1
  90.             return data
  91.         return None
  92.  
  93.     def size(self):
  94.         return self.length
  95.  
  96. radius = 15
  97. def displayList(head):
  98.     size = ll.size()
  99.     initial = WIDTH // 2 - ((size * 50) // 2)
  100.     i = 0
  101.     if head is None:
  102.         rect = pygame.draw.circle(win, WHITE, (initial + (50 * i), 110), radius)
  103.         pygame.draw.circle(win, RED, (initial + (50 * i), 110), radius, 1)
  104.         pygame.draw.line(win, WHITE, (rect.right, rect.centery), (rect.right+30, rect.centery), 2)
  105.     else:
  106.         while (head is not None):
  107.             rect = pygame.draw.circle(win, WHITE, (initial + (50 * i), 110), radius)
  108.             pygame.draw.circle(win, RED, (initial + (50 * i), 110), radius, 1)
  109.             text = font.render(f'{head.data}', True, RED)
  110.             win.blit(text, (rect.centerx-7, rect.centery-10))
  111.             pygame.draw.line(win, WHITE, (rect.right, rect.centery), (rect.right+30, rect.centery), 2)
  112.             head = head.next
  113.             i += 1
  114.     win.blit(cross, (rect.right+30, rect.centery-12))
  115.  
  116. ll = LinkedList()
  117.  
  118. mode = None
  119. data = ''
  120. msg = ''
  121. takeInput = False
  122.  
  123. running = True
  124. while running:
  125.     win.fill(BLACK)
  126.     for event in pygame.event.get():
  127.         if event.type == pygame.QUIT:
  128.             running = False
  129.  
  130.         if event.type == pygame.KEYDOWN:
  131.             if event.key == pygame.K_ESCAPE or event.key == pygame.K_q:
  132.                 running = False
  133.  
  134.             if takeInput:
  135.                 if pygame.key.name(event.key).isdigit():
  136.                     data += pygame.key.name(event.key)
  137.  
  138.             if event.key == pygame.K_1 and not mode:
  139.                 mode = 1
  140.                 takeInput = True
  141.                 msg = ''
  142.  
  143.             if event.key == pygame.K_2 and not mode:
  144.                 mode = 2
  145.                 takeInput = True
  146.                 msg = ''
  147.  
  148.             if event.key == pygame.K_3 and not mode:
  149.                 d = ll.removeFront()
  150.                 if d:
  151.                     msg = f'Front Element deleted : {d}'
  152.  
  153.             if event.key == pygame.K_4 and not mode:
  154.                 d = ll.removeRear()
  155.                 if d:
  156.                     msg = f'Rear Element deleted : {d}'
  157.  
  158.             if event.key == pygame.K_5 and not mode:
  159.                 msg = f'Linked List Size : {ll.size()}'
  160.  
  161.             if event.key == pygame.K_SPACE or event.key == pygame.K_RETURN:
  162.                 if mode == 1 and data:
  163.                     ll.insertFront(int(data))
  164.                 if mode == 2 and data:
  165.                     ll.insertEnd(int(data))
  166.                 mode = None
  167.                 takeInput = False
  168.                 data = ''
  169.  
  170.     win.blit(visualizer, (WIDTH//2-visualizer.get_width()//2, 30))
  171.     displayList(ll.start)
  172.     pygame.draw.line(win, WHITE, (0, 180), (WIDTH, 180), 3)
  173.     for index, text in enumerate(textArray):
  174.         color = WHITE
  175.         if mode and index == mode - 1:
  176.             color = GREEN
  177.         textImage = font.render(text, True, color)
  178.         win.blit(textImage, (40, (200 + (30*index))))
  179.  
  180.     if takeInput:
  181.         datatext = font.render(f'Enter data : {data}', True, WHITE)
  182.         win.blit(datatext, (WIDTH//2, HEIGHT//2+50))
  183.  
  184.     if msg:
  185.         datatext = font.render(f'{msg}', True, WHITE)
  186.         win.blit(datatext, (WIDTH//2, HEIGHT//2+50))
  187.  
  188.     pygame.draw.rect(win, WHITE, (0, 0, WIDTH, HEIGHT), 3)
  189.                
  190.     clock.tick(FPS)
  191.     pygame.display.update()
  192.  
  193. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement