Knowee
Questions
Features
Study Tools

Write program to perform INSERT_FIRST (to insert a node at the first position) andREVERSE_TRAVERSE (to display the data in nodes in reverse order) operations indoubly linked list

Question

Write program to perform INSERT_FIRST (to insert a node at the first position) andREVERSE_TRAVERSE (to display the data in nodes in reverse order) operations indoubly linked list

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple Python program that creates a doubly linked list, inserts a node at the first position, and traverses the list in reverse order.

class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None
        self.prev = None

class DoublyLinkedList:
    def __init__(self):
        self.head = None

    def insert_first(self, data):
        node = Node(data)
        if self.head is not None:
            self.head.prev = node
            node.next = self.head
        self.head = node

    def reverse_traverse(self):
        temp = self.head
        while temp.next is not None:
            temp = temp.next
        while temp is not None:
            print(temp.data, end=' ')
            temp = temp.prev

# Test the program
dll = DoublyLinkedList()
dll.insert_first(10)
dll.insert_first(20)
dll.insert_first(30)
dll.reverse_traverse()  # Output: 10 20 30

In this program, we first define a Node class that will be used to create new nodes. Each node has a data attribute to hold the data and next and prev attributes to point to the next and previous nodes in the list.

We then define a DoublyLinkedList class that has a head attribute to keep track of the first node in the list. The insert_first method creates a new node and inserts it at the beginning of the list. The reverse_traverse method starts from the end of the list and traverses it in reverse order, printing the data in each node.

This problem has been solved

Similar Questions

Problem Statement23hr leftGiven a linked list, write a program to insert or delete nodes from the beginning.Input:n = 5[6, 5, 4, 3, 2, 1]Notice that here, we are inserting each element from the beginning. Hence when you insert a new element, the list becomes newEle + OldLL.For example, if you add 3 to [4, 5, 6] then it becomes [3, 4, 5, 6].Output:[1, 2, 3, 4, 5, 6] -> after inserting elements [2, 3, 4, 5, 6] -> after deleting the beginning elementConstraint:If the linked list is empty, print an empty space.

What will be the output of the following code?class Node:    def __init__(self, data=None):        self.data = data        self.next = Noneclass LinkedList:    def __init__(self):        self.head = None    def print_list(self):        current_node = self.head        while current_node:            print(current_node.data)            current_node = current_node.next    def insert_at_beginning(self, new_data):        new_node = Node(new_data)        new_node.next = self.head        self.head = new_nodellist = LinkedList()llist.insert_at_beginning(3)llist.insert_at_beginning(2)llist.insert_at_beginning(1)llist.print_list()Options1 2 31 3 22 3 13 2 1

insert a node at the middle of the singly linked list- DATA STRUCTURESWrite a program to insert a node at the middle of the singly linked list.Note :The linked list should be 6 7 8 9 before the insertion of a new node.Constraints:.Example:Input FormatInput to get an integer(node).Output FormatDisplay the output as shown in the sample output.Explanation:.Public Test Cases:# INPUT EXPECTED OUTPUT1 56 7 8 9 6 7 5 8 92 616 7 8 9 6 7 61 8 9

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its attribute, insert this node at the desired position and return the head node.A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is empty.Example refers to the first node in the list Insert a node at position with . The new list is Function Description Complete the function insertNodeAtPosition in the editor below. It must return a reference to the head node of your finished list.insertNodeAtPosition has the following parameters:head: a SinglyLinkedListNode pointer to the head of the listdata: an integer value to insert as data in your new nodeposition: an integer position to insert the new node, zero based indexingReturnsSinglyLinkedListNode pointer: a reference to the head of the revised listInput FormatThe first line contains an integer , the number of elements in the linked list.Each of the next lines contains an integer SinglyLinkedListNode[i].data.The next line contains an integer , the data of the node that is to be inserted.The last line contains an integer .Constraints, where is the element of the linked list..Sample Input31613712Sample Output16 13 1 7ExplanationThe initial linked list is . Insert at the position which currently has in it. The updated linked list is .

f reverse_linked_list(head):    prev = None    current = head    while current is not None:        next_node = current.next        current.next = prev        prev = current        current = next_node    head = prev    return headDeletes the last node of the linked listInserts a new node at the beginning of the linked listReverses the linked listSorts the linked list in ascending order

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.