{"id":220625,"date":"2025-05-28T14:26:37","date_gmt":"2025-05-28T14:26:37","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=220625"},"modified":"2025-05-28T14:26:39","modified_gmt":"2025-05-28T14:26:39","slug":"create-three-contactnodes-and-use-the-nodes-to-build-a-linked-list","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/05\/28\/create-three-contactnodes-and-use-the-nodes-to-build-a-linked-list\/","title":{"rendered":"Create three ContactNodes and use the nodes to build a linked list."},"content":{"rendered":"\n<p>In main(), prompt the user for three contacts and output the user&#8217;s input. Create three ContactNodes and use the nodes to build a linked list. (2 pts)<\/p>\n\n\n\n<p>Ex:<\/p>\n\n\n\n<p>Person 1 Enter name: Roxanne Hughes Enter phone number: 443-555-2864 You entered: Roxanne Hughes, 443-555-2864<br>Person 2 Enter name: Juan Alberto Jr. Enter phone number: 410-555-9385 You entered: Juan Alberto Jr., 410-555-9385<br>Person 3 Enter name: Rachel Phillips Enter phone number: 310-555-6610 You entered: Rachel Phillips, 310-555-6610<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-1-color\">The Correct Answer and Explanation is:<\/mark><\/strong><\/p>\n\n\n\n<p>Here\u2019s the correct implementation in Python, assuming a basic <code>ContactNode<\/code> class and linked list logic:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ContactNode:\n    def __init__(self, name='', phone_number=''):\n        self.name = name\n        self.phone_number = phone_number\n        self.next_node = None\n\n    def insert_after(self, node):\n        self.next_node = node\n\n    def get_next(self):\n        return self.next_node\n\n    def print_contact_node(self):\n        print(f'Name: {self.name}')\n        print(f'Phone number: {self.phone_number}')\n\n\ndef main():\n    print(\"Person 1\")\n    name1 = input(\"Enter name: \")\n    phone1 = input(\"Enter phone number: \")\n    print(f\"You entered: {name1}, {phone1}\")\n    node1 = ContactNode(name1, phone1)\n\n    print(\"\\nPerson 2\")\n    name2 = input(\"Enter name: \")\n    phone2 = input(\"Enter phone number: \")\n    print(f\"You entered: {name2}, {phone2}\")\n    node2 = ContactNode(name2, phone2)\n    node1.insert_after(node2)\n\n    print(\"\\nPerson 3\")\n    name3 = input(\"Enter name: \")\n    phone3 = input(\"Enter phone number: \")\n    print(f\"You entered: {name3}, {phone3}\")\n    node3 = ContactNode(name3, phone3)\n    node2.insert_after(node3)\n\n    print(\"\\nCONTACT LIST\")\n    current_node = node1\n    while current_node is not None:\n        current_node.print_contact_node()\n        print()\n        current_node = current_node.get_next()\n\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p>This program builds a simple singly linked list using a custom <code>ContactNode<\/code> class. In computer science, a linked list is a linear data structure where each element (node) contains data and a reference (or pointer) to the next node in the sequence. This is useful for dynamically managing a sequence of objects without requiring contiguous memory allocation, unlike arrays.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Class Definition<\/strong>: The <code>ContactNode<\/code> class includes the contact\u2019s name, phone number, and a pointer to the next node (<code>next_node<\/code>). It also provides a method to insert another node after the current one and a method to print the contact information.<\/li>\n\n\n\n<li><strong>User Input<\/strong>: In the <code>main()<\/code> function, we prompt the user to enter contact information (name and phone number) for three people. Each set of inputs is stored in a new <code>ContactNode<\/code> instance.<\/li>\n\n\n\n<li><strong>Linking Nodes<\/strong>: We create three <code>ContactNode<\/code> objects. Using the <code>insert_after()<\/code> method, we link the first node to the second and the second to the third. This forms the linked list: <code>node1 \u2192 node2 \u2192 node3<\/code>.<\/li>\n\n\n\n<li><strong>Traversal and Output<\/strong>: After linking the nodes, we traverse the linked list starting from <code>node1<\/code>. At each node, we print the contact&#8217;s details using <code>print_contact_node()<\/code>. The traversal ends when the current node becomes <code>None<\/code>, indicating the end of the list.<\/li>\n<\/ol>\n\n\n\n<p>This approach demonstrates basic object-oriented programming and linked list management, which are foundational concepts in data structures and software design.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/05\/learnexams-banner6-94.jpeg\" alt=\"\" class=\"wp-image-220626\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In main(), prompt the user for three contacts and output the user&#8217;s input. Create three ContactNodes and use the nodes to build a linked list. (2 pts) Ex: Person 1 Enter name: Roxanne Hughes Enter phone number: 443-555-2864 You entered: Roxanne Hughes, 443-555-2864Person 2 Enter name: Juan Alberto Jr. Enter phone number: 410-555-9385 You entered: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-220625","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/220625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=220625"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/220625\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=220625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=220625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=220625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}