jabela

Untitled

Jul 25th, 2024 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. class TickerNode:
  2.    def __init__(self, headline):
  3.        self.headline = headline
  4.        self.next = None  # initialize next pointer as null
  5.  
  6.  
  7.  
  8.  
  9. class CircularLinkedList:
  10.    def __init__(self):
  11.        self.head = None
  12.  
  13.  
  14.    def find_headline(self, headline):
  15.        if not self.head:
  16.            print("The list is empty.")
  17.            return
  18.  
  19.  
  20.        current = self.head
  21.        while True:
  22.            if current.headline == headline:
  23.                print("Headline found:", current.headline)
  24.                return
  25.            current = current.next
  26.            if current == self.head:
  27.                break
  28.  
  29.  
  30.        print("Headline not found.")
  31.  
  32.  
  33.    def add_in_order(self, new_node):
  34.        if not self.head:  # If the list is empty, initialize it with the new node
  35.            self.head = new_node
  36.            new_node.next = new_node  # Point to itself to make it circular
  37.            return
  38.  
  39.  
  40.        current = self.head
  41.        previous = None
  42.  
  43.  
  44.        # Special case for inserting before the head node
  45.        while True:
  46.            if new_node.headline < self.head.headline:
  47.                # Find the last node
  48.                last_node = self.head
  49.                while last_node.next != self.head:
  50.                    last_node = last_node.next
  51.  
  52.  
  53.                new_node.next = self.head
  54.                last_node.next = new_node
  55.                self.head = new_node
  56.                return
  57.  
  58.  
  59.            # Iterate through the list to find the insertion point
  60.            while current.next != self.head and current.headline < new_node.headline:
  61.                previous = current
  62.                current = current.next
  63.  
  64.  
  65.            # Insertion point found
  66.            if current.next == self.head and current.headline < new_node.headline:
  67.                # Insert at the end of the list
  68.                current.next = new_node
  69.                new_node.next = self.head
  70.                return
  71.  
  72.  
  73.            previous.next = new_node
  74.            new_node.next = current
  75.            return
  76.  
  77.  
  78.  
  79.  
  80. # Example usage
  81. news_list = CircularLinkedList()
  82.  
  83.  
  84. # Adding nodes to the list
  85. news_list.add_in_order(TickerNode("Breaking News: Market Crash"))
  86. news_list.add_in_order(TickerNode("Latest Update: Weather Forecast"))
  87. news_list.add_in_order(TickerNode("Sports: Team Wins Championship"))
  88. news_list.add_in_order(TickerNode("Technology: New Smartphone Release"))
  89.  
  90.  
  91. # Searching for headlines
  92. news_list.find_headline("Latest Update: Weather Forecast")  # Output: Headline found: Latest Update: Weather Forecast
  93. news_list.find_headline("Nonexistent Headline")  # Output: Headline not found.
  94.  
Advertisement
Add Comment
Please, Sign In to add comment