Question

In: Computer Science

Write PSEUDOCODE to insert a node at position 2 in a doubly-linked list (assume position follows...

Write PSEUDOCODE to insert a node at position 2 in a doubly-linked list (assume position follows classic indexing from 0 to item_count - 1)

Solutions

Expert Solution

// Approach: To insert a given data at a specified position, the below algorithm is to be followed:

  • Traverse the Linked list upto (position-1) nodes.
  • Once all the (position-1) nodes are traversed, allocate memory and the given data to the new node.
  • Point the next pointer of the new node to the next of current node.
  • Point the next pointer of current node to the new node.

// Java program for insertion in a single linked

// list at a specified position

class GFG { // A linked list Node

                static class Node {

                                public int data;

                                public Node nextNode;

                                // inserting the required data

                                public Node(int data) {

                                                this.data = data;

                                }

                }

                // function to create and return a Node

                static Node GetNode(int data) {

                                return new Node(data);

                }

                // function to insert a Node at required position

                static Node InsertPos(Node headNode, int position, int data) {

                                Node head = headNode;

                                if (position < 1)

                                                System.out.print("Invalid position");

                                // if position is 1 then new node is

                                // set in fornt of head node

                                // head node is changing.

                                if (position == 1) {

                                                Node newNode = new Node(data);

                                                newNode.nextNode = headNode;

                                                head = newNode;

                                } else

{

                                                while (position-- != 0) {

                                                                if (position == 1) {

                                                                                // adding Node at required position

                                                                                Node newNode = GetNode(data);

                                                                                // Making the new Node to point to

                                                                                // the old Node at the same position

                                                                                newNode.nextNode = headNode.nextNode;

                                                                                // Replacing current with new Node

                                                                                // to the old Node to point to the new Node

                                                                                headNode.nextNode = newNode;

                                                                                break;

                                                                }

                                                                headNode = headNode.nextNode;

                                                }

                                                if (position != 1)

                                                                System.out.print("Position out of range");

                                }

                                return head;

                }

                static void PrintList(Node node) {

                                while (node != null) {

                                                System.out.print(node.data);

                                                node = node.nextNode;

                                                if (node != null)

                                                                System.out.print(",");

                                }

                                System.out.println();

                }

                // Driver code

                public static void main(String[] args) {

                                Node head = GetNode(3);

                                head.nextNode = GetNode(5);

                                head.nextNode.nextNode = GetNode(8);

                                head.nextNode.nextNode.nextNode = GetNode(10);

                                System.out.print("Linked list before insertion: ");

                                PrintList(head);

                                int data = 12, pos = 3;

                                head = InsertPos(head, pos, data);

                                System.out.print("Linked list after" + " insertion of 12 at position 3: ");

                                PrintList(head);

                                // front of the linked list

                                data = 1; pos = 1;

                                head = InsertPos(head, pos, data);

                                System.out.print("Linked list after" + "insertion of 1 at position 1: ");

                                PrintList(head);

                               

                }

}

Input: 3->5->8->10, data = 2, position = 2
Output: 3->2->5->8->10

Related Solutions

Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """...
Python class DLLNode: """ Class representing a node in the doubly linked list implemented below. """ def __init__(self, value, next=None, prev=None): """ Constructor @attribute value: the value to give this node @attribute next: the next node for this node @attribute prev: the previous node for this node """ self.__next = next self.__prev = prev self.__value = value def __repr__(self): return str(self.__value) def __str__(self): return str(self.__value) def get_value(self): """ Getter for value :return: the value of the node """ return self.__value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
Write a pseudocode function that interchanges two adjacent items of: (a) singly linked lists (b) doubly...
Write a pseudocode function that interchanges two adjacent items of: (a) singly linked lists (b) doubly linked lists if you can make it clean and short that be nice
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT