Submit code in C++, Java or the language of your choice for the following. Use t
ID: 3861162 • Letter: S
Question
Submit code in C++, Java or the language of your choice for the following. Use the code examples from the lectures for defining and creating nodes, then add the required operations. Design and Implement a single linked list with the following operations, lectures single linked list 01 and 02 have example code/pseudocode for each operation. a- Create "n" nodes b- Delete from the middle c- Insert in the middle Design and Implement a double linked list with the following operations. Your code should be based on code and pseudocode provided in the lecture "double linked list" a- Create "n" nodes b- Delete from the end c- Insert at the beginningExplanation / Answer
code in java
1.
public class Dummy{
static node head = null;
public static void createNOdes(int n){
if(n !=0){
if(head == null){
head = new node(n);
head.next = null;
}
node prev = head;
for(int i=0;i<=n-1;i++){
node newnode = new node(n);
prev.next = newnode;
prev = newnode;
}
}
}
public static void deleteMiddle(){
int count = 0;
node temp = head;
while(temp ! = null){
count++;
temp =temp.next;
}
if(count == 0){
return ;
}
else if(count == 1)
head = null;
else{
int i=0;
temp = head;
node prev = temp;
while(temp!=null && i<count/2){
prev = temp;
temp = temp.next;
}
prev.next = temp.next;
}
return;
}
public static node inertMiddle(int num){
int count = 0;
Node temp = head;
while(temp ! = null){
count++;
temp =temp.next;
}
if(count == 0){
head = new node(num);
}
else if(count == 1)
{
node n = new node(num);
n.next = head;
head = n;
}
else{
int i=0;
temp = head;
node prev = temp;
while(temp!=null && i<count/2){
prev = temp;
temp = temp.next;
}
node n = new node(num);
prev.next = n;
n.next = temp;
}
return head;
}
}
class node{
int data;
node next;
node(int n){
this.data = n;
this.next = null;
}
}
2. doubly linked list
public class Dummy{
static node head = null;
public static void createNOdes(int n){
if(n !=0){
if(head == null){
head = new node(n);
head.next = null;
head.prev = null;
}
node tmp = head;
for(int i=0;i<=n-1;i++){
node newnode = new node(n);
tmp.next = newnode;
newnode.prev = tmp;
}
}
}
public static void deleteMiddle(){
int count = 0;
node temp = head;
while(temp ! = null){
count++;
temp =temp.next;
}
if(count == 0){
return ;
}
else if(count == 1)
head = null;
else{
int i=0;
temp = head;
node tmp = temp;
while(temp!=null && i<count/2){
tmp = temp;
temp = temp.next;
}
tmp.next = temp.next;
temp.prev = tmp;
}
return;
}
public static node inertMiddle(int num){
int count = 0;
Node temp = head;
while(temp ! = null){
count++;
temp =temp.next;
}
if(count == 0){
head = new node(num);
}
else if(count == 1)
{
node n = new node(num);
n.next = head;
head.prev = n;
head = n;
}
else{
int i=0;
temp = head;
node tmp = temp;
while(temp!=null && i<count/2){
tmp = temp;
temp = temp.next;
}
node n = new node(num);
tmp.next = n;
n.next = temp;
temp.prev = n;
n.prev = tmp;
}
return head;
}
}
class node{
int data;
node next;
node prev;
node(int n){
this.data = n;
this.next = null;
this.prev = null;
}
}