Create three ContactNodes and use the nodes to build a linked list.

In main(), prompt the user for three contacts and output the user’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-2864
Person 2 Enter name: Juan Alberto Jr. Enter phone number: 410-555-9385 You entered: Juan Alberto Jr., 410-555-9385
Person 3 Enter name: Rachel Phillips Enter phone number: 310-555-6610 You entered: Rachel Phillips, 310-555-6610

The Correct Answer and Explanation is:

Here’s the correct implementation in Python, assuming a basic ContactNode class and linked list logic:

class ContactNode:
    def __init__(self, name='', phone_number=''):
        self.name = name
        self.phone_number = phone_number
        self.next_node = None

    def insert_after(self, node):
        self.next_node = node

    def get_next(self):
        return self.next_node

    def print_contact_node(self):
        print(f'Name: {self.name}')
        print(f'Phone number: {self.phone_number}')


def main():
    print("Person 1")
    name1 = input("Enter name: ")
    phone1 = input("Enter phone number: ")
    print(f"You entered: {name1}, {phone1}")
    node1 = ContactNode(name1, phone1)

    print("\nPerson 2")
    name2 = input("Enter name: ")
    phone2 = input("Enter phone number: ")
    print(f"You entered: {name2}, {phone2}")
    node2 = ContactNode(name2, phone2)
    node1.insert_after(node2)

    print("\nPerson 3")
    name3 = input("Enter name: ")
    phone3 = input("Enter phone number: ")
    print(f"You entered: {name3}, {phone3}")
    node3 = ContactNode(name3, phone3)
    node2.insert_after(node3)

    print("\nCONTACT LIST")
    current_node = node1
    while current_node is not None:
        current_node.print_contact_node()
        print()
        current_node = current_node.get_next()


if __name__ == "__main__":
    main()

Explanation

This program builds a simple singly linked list using a custom ContactNode 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.

  1. Class Definition: The ContactNode class includes the contact’s name, phone number, and a pointer to the next node (next_node). It also provides a method to insert another node after the current one and a method to print the contact information.
  2. User Input: In the main() 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 ContactNode instance.
  3. Linking Nodes: We create three ContactNode objects. Using the insert_after() method, we link the first node to the second and the second to the third. This forms the linked list: node1 → node2 → node3.
  4. Traversal and Output: After linking the nodes, we traverse the linked list starting from node1. At each node, we print the contact’s details using print_contact_node(). The traversal ends when the current node becomes None, indicating the end of the list.

This approach demonstrates basic object-oriented programming and linked list management, which are foundational concepts in data structures and software design.

Scroll to Top