{"id":203,"date":"2023-12-24T10:15:53","date_gmt":"2023-12-24T16:15:53","guid":{"rendered":"https:\/\/www.baizhao666.com\/?p=203"},"modified":"2024-07-17T18:45:30","modified_gmt":"2024-07-18T00:45:30","slug":"linked-list-stack-and-queue","status":"publish","type":"post","link":"https:\/\/www.baizhao666.com\/?p=203","title":{"rendered":"Linked list, stack, and queue"},"content":{"rendered":"\n<p>A linked list is an important programming concept that underlies a lot of modern data structures. Linked List is a linear data structure, in which elements are not stored at a contiguous location, rather they are linked using pointers. Linked List forms a series of connected nodes, where each node stores the data and the address of the next node.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h3 class=\"wp-block-heading\">Types of linked list:<\/h3>\n\n\n\n<p>1. Singly linked list:<\/p>\n\n\n\n<p>In a singly linked list, each node contains a reference to the next node in the sequence. We can only traverse the single linked list in one direction.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"812\" height=\"221\" src=\"https:\/\/www.baizhao666.com\/wp-content\/uploads\/2023\/12\/LLdrawio.png\" alt=\"\" class=\"wp-image-204\" srcset=\"https:\/\/www.baizhao666.com\/wp-content\/uploads\/2023\/12\/LLdrawio.png 812w, https:\/\/www.baizhao666.com\/wp-content\/uploads\/2023\/12\/LLdrawio-300x82.png 300w, https:\/\/www.baizhao666.com\/wp-content\/uploads\/2023\/12\/LLdrawio-768x209.png 768w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\n\n\n\n<p>2. Doubly linked list:<\/p>\n\n\n\n<p>In a doubly linked list, each node contains references to both the next and previous nodes. Because of the next and previous references, we can traverse the doubly linked list in both directions. On the other hand, these references require more memory space comparing with singly linked list.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"171\" src=\"https:\/\/www.baizhao666.com\/wp-content\/uploads\/2023\/12\/Doublylinkedlist-1024x171.png\" alt=\"\" class=\"wp-image-205\" srcset=\"https:\/\/www.baizhao666.com\/wp-content\/uploads\/2023\/12\/Doublylinkedlist-1024x171.png 1024w, https:\/\/www.baizhao666.com\/wp-content\/uploads\/2023\/12\/Doublylinkedlist-300x50.png 300w, https:\/\/www.baizhao666.com\/wp-content\/uploads\/2023\/12\/Doublylinkedlist-768x128.png 768w, https:\/\/www.baizhao666.com\/wp-content\/uploads\/2023\/12\/Doublylinkedlist.png 1096w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\n\n\n\n<p>3. Circular linked list:<\/p>\n\n\n\n<p>In a circular linked list, the last node points back to the head node, creating a circular structure. It can be either singly or doubly linked.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"632\" height=\"110\" src=\"https:\/\/www.baizhao666.com\/wp-content\/uploads\/2023\/12\/CircularLinkeList.png\" alt=\"\" class=\"wp-image-206\" srcset=\"https:\/\/www.baizhao666.com\/wp-content\/uploads\/2023\/12\/CircularLinkeList.png 632w, https:\/\/www.baizhao666.com\/wp-content\/uploads\/2023\/12\/CircularLinkeList-300x52.png 300w\" sizes=\"auto, (max-width: 632px) 100vw, 632px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Advantage of linked list:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Dynamic Size:<strong> <\/strong>A single node can be inserted or deleted from the linked list during the runtime. But we need to be careful when durling with the reference of next or prev.<\/li>\n\n\n\n<li>Insertion and Deletion:<strong> <\/strong>Inserting or deleting a node from a linked list is efficient, we only need to modify the reference rather than shifting the whole list.<\/li>\n\n\n\n<li>Flexibility: Linked lists can be easily reorganized and modified without requiring a contiguous block of memory.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Disadvantage of linked list:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Random Access: We can&#8217;t use the index to access the specific node like list. Instead, traversal is required for searching.<\/li>\n\n\n\n<li>Extra Memory: Linked lists require additional memory for storing the references, compared to lists.<\/li>\n<\/ul>\n\n\n\n<p>Here we will implement the simplifying singly linked list, more details will be shown in data structure.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># a class for a single node\nclass Node:\n    def __init__(self, data):\n        self.data = data\n        self.next = None\n\n\n# a class for a linked list\nclass LinkedList:\n    def __init__(self):\n        self.head = None\n\n    # insert a node to the end\n    def insert(self, data):\n        new_node = Node(data)\n        # if there is no node in the list\n        if self.head is None:\n            # the new node is the head of the list\n            self.head = new_node\n        # if there is some nodes in the list\n        else:\n            # find the last node\n            cur = self.head\n            while cur.next is not None:\n                cur = cur.next\n            # add the new node to the end\n            cur.next = new_node\n\n    # delete a node from the end\n    def delete(self):\n        # if there is no node in the list, do nothing\n        if self.head is None:\n            return\n        elif self.head.next is None:\n            self.head = None\n        else:\n            # find the node before the last node\n            cur = self.head\n            while cur.next.next is not None:\n                cur = cur.next\n            cur.next = None\n\n    # print out the list\n    def display(self):\n        cur = self.head\n        while cur is not None:\n            print(cur.data, end=&quot;-&gt;&quot;)\n            cur = cur.next\n        print()\n\n\n# create a linked list\nmy_list = LinkedList.LinkedList()\n# insert some node\nmy_list.insert(5)\nmy_list.insert(1)\nmy_list.insert(8)\nmy_list.insert(3)\nmy_list.insert(7)\n# print all the nodes in the list\nmy_list.display()\n# delete a node\nmy_list.delete()\nmy_list.display()\n# delete a node\nmy_list.delete()\nmy_list.display()\n# insert a node\nmy_list.insert(6)\nmy_list.display()<\/code><\/pre><\/div>\n\n\n\n<p>Output:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plain\"><code>5-&gt;1-&gt;8-&gt;3-&gt;7-&gt;\n5-&gt;1-&gt;8-&gt;3-&gt;\n5-&gt;1-&gt;8-&gt;\n5-&gt;1-&gt;8-&gt;6-&gt;<\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Stack and queue<\/h3>\n\n\n\n<p>Stack and queue are important data structures in computer science, which can be used in many situations for efficiency. Stacks are a Last In First Out (LIFO) data structure. Queues are a First In First Out (FIFO) data structure.<\/p>\n\n\n\n<p>Stacks and queues are used directly or indirectly in various different programming situations. For instance, Python codes are normally executed in the order they appear, in queue ordering in other words. Meanwhile, recursive function calls (and function calls in general) are handle through the call stack, meaning the first recursive function call is the last to be resolved.<\/p>\n\n\n\n<p>Stacks and queue can be implemented using linked list. But in these case, we no long have access to the node in the middle of the list. Instead, we can only add or delete from the front or the end. More details will be shown in the data structures.<\/p>\n\n\n\n<p>Reference:<\/p>\n\n\n\n<p>GreeksforGreeks &#8211; Python Linked List: https:\/\/www.geeksforgeeks.org\/python-linked-list\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A linked list is an important programming concept that underlies a lot of modern data structures. Linked List is a linear data structure, in which elements are not stored at a contiguous location, rather they are linked using pointers. Linked List forms a series of connected nodes, where each node stores the data and the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.baizhao666.com\/?p=203\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Linked list, stack, and queue&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=203"}],"version-history":[{"count":2,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts\/203\/revisions"}],"predecessor-version":[{"id":275,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts\/203\/revisions\/275"}],"wp:attachment":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}