Implement a simple Doubly Linked List using nodes. Functions to include: an insert in a sorted list, and a delete function, including constructor. Please include comments to make the code traceable and understandable
Since you have not mentioned the language of your preference, I am providing the code in Java.
CODE
import java.util.Scanner;
/* Class Node */
class Node
{
protected int data;
protected Node next, prev;
/* Constructor */
public Node()
{
next = null;
prev = null;
data = 0;
}
/* Constructor */
public Node(int d, Node n, Node p)
{
data = d;
next = n;
prev = p;
}
/* Function to set link to next node */
public void setLinkNext(Node n)
{
next = n;
}
/* Function to set link to previous node */
public void setLinkPrev(Node p)
{
prev = p;
}
/* Funtion to get link to next node */
public Node getLinkNext()
{
return next;
}
/* Function to get link to previous node */
public Node getLinkPrev()
{
return prev;
}
/* Function to set data to node */
public void setData(int d)
{
data = d;
}
/* Function to get data from node */
public int getData()
{
return data;
}
}
/* Class linkedList */
class linkedList
{
protected Node start;
protected Node end ;
public int size;
/* Constructor */
public linkedList()
{
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert element at begining */
public void insertAtStart(int val)
{
Node nptr = new Node(val, null, null);
if(start == null)
{
start = nptr;
end = start;
}
else
{
start.setLinkPrev(nptr);
nptr.setLinkNext(start);
start = nptr;
}
size++;
}
/* Function to insert element at end */
public void insertAtEnd(int val)
{
Node nptr = new Node(val, null, null);
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLinkPrev(end);
end.setLinkNext(nptr);
end = nptr;
}
size++;
}
/* Function to insert element at position */
public void insertAtPos(int val , int pos)
{
Node nptr = new Node(val, null, null);
if (pos == 1)
{
insertAtStart(val);
return;
}
Node ptr = start;
for (int i = 2; i <= size; i++)
{
if (i == pos)
{
Node tmp = ptr.getLinkNext();
ptr.setLinkNext(nptr);
nptr.setLinkPrev(ptr);
nptr.setLinkNext(tmp);
tmp.setLinkPrev(nptr);
}
ptr = ptr.getLinkNext();
}
size++ ;
}
/* Function to delete node at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
if (size == 1)
{
start = null;
end = null;
size = 0;
return;
}
start = start.getLinkNext();
start.setLinkPrev(null);
size--;
return ;
}
if (pos == size)
{
end = end.getLinkPrev();
end.setLinkNext(null);
size-- ;
}
Node ptr = start.getLinkNext();
for (int i = 2; i <= size; i++)
{
if (i == pos)
{
Node p = ptr.getLinkPrev();
Node n = ptr.getLinkNext();
p.setLinkNext(n);
n.setLinkPrev(p);
size-- ;
return;
}
ptr = ptr.getLinkNext();
}
}
/* Function to display status of list */
public void display()
{
System.out.print("\nDoubly Linked List = ");
if (size == 0)
{
System.out.print("empty\n");
return;
}
if (start.getLinkNext() == null)
{
System.out.println(start.getData() );
return;
}
Node ptr = start;
System.out.print(start.getData()+ " <-> ");
ptr = start.getLinkNext();
while (ptr.getLinkNext() != null)
{
System.out.print(ptr.getData()+ " <-> ");
ptr = ptr.getLinkNext();
}
System.out.print(ptr.getData()+ "\n");
}
}
/* Class DoublyLinkedList */
public class DoublyLinkedList
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of linkedList */
linkedList list = new linkedList();
System.out.println("Doubly Linked List Test\n");
char ch;
/* Perform list operations */
do
{
System.out.println("\nDoubly Linked List Operations\n");
System.out.println("1. insert at begining");
System.out.println("2. insert at end");
System.out.println("3. insert at position");
System.out.println("4. delete at position");
System.out.println("5. check empty");
System.out.println("6. get size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
list.insertAtStart( scan.nextInt() );
break;
case 2 :
System.out.println("Enter integer element to insert");
list.insertAtEnd( scan.nextInt() );
break;
case 3 :
System.out.println("Enter integer element to insert");
int num = scan.nextInt() ;
System.out.println("Enter position");
int pos = scan.nextInt() ;
if (pos < 1 || pos > list.getSize() )
System.out.println("Invalid position\n");
else
list.insertAtPos(num, pos);
break;
case 4 :
System.out.println("Enter position");
int p = scan.nextInt() ;
if (p < 1 || p > list.getSize() )
System.out.println("Invalid position\n");
else
list.deleteAtPos(p);
break;
case 5 :
System.out.println("Empty status = "+ list.isEmpty());
break;
case 6 :
System.out.println("Size = "+ list.getSize() +" \n");
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
/* Display List */
list.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
OUTPUT
Doubly Linked List Operations 1. insert at begining 2. insert at end 3. insert at position 4. delete at position 5. check empty 6. get size 1 Enter integer element to insert 5 Doubly Linked List = 5 Do you want to continue (Type y or n) y Doubly Linked List Operations 1. insert at begining 2. insert at end 3. insert at position 4. delete at position 5. check empty 6. get size 1 Enter integer element to insert 2 Doubly Linked List = 2 <-> 5 Do you want to continue (Type y or n) y Doubly Linked List Operations 1. insert at begining 2. insert at end 3. insert at position 4. delete at position 5. check empty 6. get size 2 Enter integer element to insert 6 Doubly Linked List = 2 <-> 5 <-> 6 Do you want to continue (Type y or n) y Doubly Linked List Operations 1. insert at begining 2. insert at end 3. insert at position 4. delete at position 5. check empty 6. get size 1 Enter integer element to insert 7 Doubly Linked List = 7 <-> 2 <-> 5 <-> 6 Do you want to continue (Type y or n) y Doubly Linked List Operations 1. insert at begining 2. insert at end 3. insert at position 4. delete at position 5. check empty 6. get size 3 Enter integer element to insert 3 Enter position 3 Doubly Linked List = 7 <-> 2 <-> 3 <-> 5 <-> 6 Do you want to continue (Type y or n) y Doubly Linked List Operations 1. insert at begining 2. insert at end 3. insert at position 4. delete at position 5. check empty 6. get size 4 Enter position 2 Doubly Linked List = 7 <-> 3 <-> 5 <-> 6 Do you want to continue (Type y or n) y Doubly Linked List Operations 1. insert at begining 2. insert at end 3. insert at position 4. delete at position 5. check empty 6. get size 3 Enter integer element to insert 4 Enter position 4 Doubly Linked List = 7 <-> 3 <-> 5 <-> 4 <-> 6 Do you want to continue (Type y or n) y Doubly Linked List Operations 1. insert at begining 2. insert at end 3. insert at position 4. delete at position 5. check empty 6. get size 6 Size = 5 Doubly Linked List = 7 <-> 3 <-> 5 <-> 4 <-> 6 Do you want to continue (Type y or n) y Doubly Linked List Operations 1. insert at begining 2. insert at end 3. insert at position 4. delete at position 5. check empty 6. get size 4 Enter position 1 Doubly Linked List = 3 <-> 5 <-> 4 <-> 6 Do you want to continue (Type y or n) y Doubly Linked List Operations 1. insert at begining 2. insert at end 3. insert at position 4. delete at position 5. check empty 6. get size 4 Enter position 2 Doubly Linked List = 3 <-> 4 <-> 6 Do you want to continue (Type y or n) y Doubly Linked List Operations 1. insert at begining 2. insert at end 3. insert at position 4. delete at position 5. check empty 6. get size 4 Enter position 2 Doubly Linked List = 3 <-> 6 Do you want to continue (Type y or n) y Doubly Linked List Operations 1. insert at begining 2. insert at end 3. insert at position 4. delete at position 5. check empty 6. get size 4 Enter position 1 Doubly Linked List = 6 Do you want to continue (Type y or n) y Doubly Linked List Operations 1. insert at begining 2. insert at end 3. insert at position 4. delete at position 5. check empty 6. get size 4 Enter position 1 Doubly Linked List = empty Do you want to continue (Type y or n) y Doubly Linked List Operations 1. insert at begining 2. insert at end 3. insert at position 4. delete at position 5. check empty 6. get size 5 Empty status = true Doubly Linked List = empty Do you want to continue (Type y or n) n
Implement a simple Doubly Linked List using nodes. Functions to include: an insert in a sorted...
Using C++ language, Design and implement a class representing a doubly linked list. The class must have the following requirements: The linked list and the nodes must be implemented as a C++ templates The list must be generic – it should not implement arithmetic/logic functions. (template class) It must include a destructor and a copy constructor It must include methods to insert at the front and at the back of the list It must include a method to return the...
write any 2 c functions for a linked list and a doubly-linked sorted list create at least two data structures
Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student records that should not be sorted Create a liked list of 15 student record nodes. Each node is a node of one student record from the above unsorted array. The list of student records should be sorted by student ID. (Insert function without sort function to create a linked list.) (If you insert correctly, the...
Write a function to implement linked list consisting of five nodes. Store the data in the nodes in the order given below. Also, write a function to display the data stored in the implemented linked list. in C++ programming George, Paul, Ross, Joe, Elaine, Robert1 Insert three nodes in between second node and third node in the linked list that you implemented in problem 1. Store the following data in the new (inserted) nodes in the following order. (You have...
Write a C++implementation of a doubly linked list class using a template class representation for a node and using pointers.In its public API provide functions to insert,find, delete, get size and get position of an element. Write C++ code to show those functions work as expected. (MUST COMPILE AND RUN)
In C++ Create a data structure doubly linked list, implement the following operations for the doubly linked list: addFirst addLast insertBefore insertAfter delete printList
You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow the format** **Don't worry about the readme.txt** THANK YOU SO SO MUCH! You have to implement the doubly linked list in DList.h and DList.cpp and the queue in the LinkedQueue.h and LinkedQueue.cpp; all as template classes. You have to provide the main.cpp file as we discussed in class to allow the user to interact with your queue using enqueue, dequeue, front, back, size, empty,...
In python - Implement a doubly linked circular linked list of Node objects called CircularDoublyLinkedList. The data of each Node in the list is an integer. You will collect measurements for: An already sorted linked list An already sorted linked list in descending order A linked list containing random data
C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...
Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have two state variables namely data and nextNode. The doubly list class should contain the following methods: Middlelnsert- insert a node somewhere in the middle of the list Startinsert-insert a node at start of the Linked list Endinsert- insert a node at the end of the Linked list Delete-delete a node Traverse-prints all the node's data Reverse-reverses the linked list . . Note: Choose...