DynamicList Node +i: int +next Node\" -head Node +DynamicList) *-DynamicList() *
ID: 3852483 • Letter: D
Question
DynamicList Node +i: int +next Node" -head Node +DynamicList) *-DynamicList() *clear) void inserti i: int) : int append(i: int): int remove(i: int): int *peeki: int&): int isFull): bool isEmpty(): bool getLength): int find(i : int): int +print)void Summary For this assignment, use the above UML diagram to implement a class named DynamicList. DynamicList implements a LinkedList using a pointer-based implementation. You may not use containers from the STL. Your implementation must match the above UML for full credit. Place the entire class in it's own header file: DynamicList.h. Submit only this file. I will test your classes by creating a test program that uses the methods for your classes. For full credit, your methods should function as described below Attributes . head A pointer that holds the memory address for the first node of the list. . constructor sets head to NULL. .destructor causes all memory to be freed up clear causes all memory to be freed up insertO inserts the value in parameter i into the list, IN ASCENDING ORDER. Returns 0 if successful 1 otherwise . append adds the value in parameter i to the end of the list. Returms 0 if successful, -1 otherwise . remove removes the first value found in the list matching the value in parameter i. Returns 0 if successful, -1 otherwise isFull returns true if the list is full, false otherwise isEmpty returns true if the list is empty, false otherwise. .findO returns the relative position of a node containing the value matching the value in parameter i if ) found in the list, -1 otherwise. (0 for the first node, 1 for the second, 2 for the third, print- displays the values in the list on a single line, separated by spaces. . peek assigns the value at the front of the list to reference parameter i. Returns 0 if successful, -1 otherwise. Does not alter the list in any way.4 . getLength returns length of list Feel free to use the attached driver.cpp program to get you started on testing your solution. It is not comprehensive, but can be used as a starting point. Notes: insert) and appendoO would normally not be within the same class, as they have incompatible logic. You are including them both here to avoid writing a second class whose code differs only by one method. When I test your class, insert and append will NOT be invoked on the same object without calling clear first. This way, the object will be tested as an ordered List or an Unordered list. When could a method fail? For example, insert could fail if the array is full.Explanation / Answer
-
/* -
* Java Program to Implement Singly Linked List -
*/ -
-
import java.util.Scanner;
-
-
/* Class Node */ -
class Node -
{ -
protected int data;
-
protected Node link;
-
-
/* Constructor */ -
public Node()
-
{ -
link = null;
-
data = 0;
-
} -
/* Constructor */ -
public Node(int d,Node n)
-
{ -
data = d;
-
link = n;
-
} -
/* Function to set link to next Node */ -
public void setLink(Node n)
-
{ -
link = n;
-
} -
/* Function to set data to current Node */ -
public void setData(int d)
-
{ -
data = d;
-
} -
/* Function to get link to next node */ -
public Node getLink()
-
{ -
return link;
-
} -
/* Function to get data from current 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 an element at begining */ -
public void insertAtStart(int val)
-
{ -
Node nptr = new Node(val, null);
-
size++ ;
-
if(start == null)
-
{ -
start = nptr;
-
end = start;
-
} -
else -
{ -
nptr.setLink(start);
-
start = nptr;
-
} -
} -
/* Function to insert an element at end */ -
public void insertAtEnd(int val)
-
{ -
Node nptr = new Node(val,null);
-
size++ ;
-
if(start == null)
-
{ -
start = nptr;
-
end = start;
-
} -
else -
{ -
end.setLink(nptr);
-
end = nptr;
-
} -
} -
/* Function to insert an element at position */ -
public void insertAtPos(int val , int pos)
-
{ -
Node nptr = new Node(val, null);
-
Node ptr = start;
-
pos = pos - 1 ;
-
for (int i = 1; i < size; i++)
-
{ -
if (i == pos)
-
{ -
Node tmp = ptr.getLink() ;
-
ptr.setLink(nptr);
-
nptr.setLink(tmp);
-
break;
-
} -
ptr = ptr.getLink();
-
} -
size++ ;
-
} -
/* Function to delete an element at position */ -
public void deleteAtPos(int pos)
-
{ -
if (pos == 1)
-
{ -
start = start.getLink();
-
size--; -
return ;
-
} -
if (pos == size)
-
{ -
Node s = start;
-
Node t = start;
-
while (s != end)
-
{ -
t = s;
-
s = s.getLink();
-
} -
end = t;
-
end.setLink(null);
-
size --; -
return;
-
} -
Node ptr = start;
-
pos = pos - 1 ;
-
for (int i = 1; i < size - 1; i++)
-
{ -
if (i == pos)
-
{ -
Node tmp = ptr.getLink();
-
tmp = tmp.getLink();
-
ptr.setLink(tmp);
-
break;
-
} -
ptr = ptr.getLink();
-
} -
size-- ;
-
} -
/* Function to display elements */ -
public void display()
-
{ -
System.out.print(" Singly Linked List = ");
-
if (size == 0)
-
{ -
System.out.print("empty ");
-
return;
-
} -
if (start.getLink() == null)
-
{ -
System.out.println(start.getData() );
-
return;
-
} -
Node ptr = start;
-
System.out.print(start.getData()+ "->");
-
ptr = start.getLink();
-
while (ptr.getLink() != null)
-
{ -
System.out.print(ptr.getData()+ "->");
-
ptr = ptr.getLink();
-
} -
System.out.print(ptr.getData()+ " ");
-
} -
} -
-
/* Class SinglyLinkedList */ -
public class SinglyLinkedList
-
{ -
public static void main(String[] args)
-
{ -
Scanner scan = new Scanner(System.in);
-
/* Creating object of class linkedList */ -
linkedList list = new linkedList();
-
System.out.println("Singly Linked List Test ");
-
char ch;
-
/* Perform list operations */ -
do -
{ -
System.out.println(" Singly Linked List Operations ");
-
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 ");
-
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 ");
-
else -
list.deleteAtPos(p);
-
break;
-
case 5 :
-
System.out.println("Empty status = "+ list.isEmpty());
-
break;
-
case 6 :
-
System.out.println("Size = "+ list.getSize() +" ");
-
break;
-
default :
-
System.out.println("Wrong Entry ");
-
break;
-
} -
/* Display List */ -
list.display();
-
System.out.println(" Do you want to continue (Type y or n) ");
-
ch = scan.next().charAt(0);
-
} while (ch == 'Y'|| ch == 'y');
-
} -
}
-
/* -
* Java Program to Implement Singly Linked List -
*/ -
-
import java.util.Scanner;
-
-
/* Class Node */ -
class Node -
{ -
protected int data;
-
protected Node link;
-
-
/* Constructor */ -
public Node()
-
{ -
link = null;
-
data = 0;
-
} -
/* Constructor */ -
public Node(int d,Node n)
-
{ -
data = d;
-
link = n;
-
} -
/* Function to set link to next Node */ -
public void setLink(Node n)
-
{ -
link = n;
-
} -
/* Function to set data to current Node */ -
public void setData(int d)
-
{ -
data = d;
-
} -
/* Function to get link to next node */ -
public Node getLink()
-
{ -
return link;
-
} -
/* Function to get data from current 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 an element at begining */ -
public void insertAtStart(int val)
-
{ -
Node nptr = new Node(val, null);
-
size++ ;
-
if(start == null)
-
{ -
start = nptr;
-
end = start;
-
} -
else -
{ -
nptr.setLink(start);
-
start = nptr;
-
} -
} -
/* Function to insert an element at end */ -
public void insertAtEnd(int val)
-
{ -
Node nptr = new Node(val,null);
-
size++ ;
-
if(start == null)
-
{ -
start = nptr;
-
end = start;
-
} -
else -
{ -
end.setLink(nptr);
-
end = nptr;
-
} -
} -
/* Function to insert an element at position */ -
public void insertAtPos(int val , int pos)
-
{ -
Node nptr = new Node(val, null);
-
Node ptr = start;
-
pos = pos - 1 ;
-
for (int i = 1; i < size; i++)
-
{ -
if (i == pos)
-
{ -
Node tmp = ptr.getLink() ;
-
ptr.setLink(nptr);
-
nptr.setLink(tmp);
-
break;
-
} -
ptr = ptr.getLink();
-
} -
size++ ;
-
} -
/* Function to delete an element at position */ -
public void deleteAtPos(int pos)
-
{ -
if (pos == 1)
-
{ -
start = start.getLink();
-
size--; -
return ;
-
} -
if (pos == size)
-
{ -
Node s = start;
-
Node t = start;
-
while (s != end)
-
{ -
t = s;
-
s = s.getLink();
-
} -
end = t;
-
end.setLink(null);
-
size --; -
return;
-
} -
Node ptr = start;
-
pos = pos - 1 ;
-
for (int i = 1; i < size - 1; i++)
-
{ -
if (i == pos)
-
{ -
Node tmp = ptr.getLink();
-
tmp = tmp.getLink();
-
ptr.setLink(tmp);
-
break;
-
} -
ptr = ptr.getLink();
-
} -
size-- ;
-
} -
/* Function to display elements */ -
public void display()
-
{ -
System.out.print(" Singly Linked List = ");
-
if (size == 0)
-
{ -
System.out.print("empty ");
-
return;
-
} -
if (start.getLink() == null)
-
{ -
System.out.println(start.getData() );
-
return;
-
} -
Node ptr = start;
-
System.out.print(start.getData()+ "->");
-
ptr = start.getLink();
-
while (ptr.getLink() != null)
-
{ -
System.out.print(ptr.getData()+ "->");
-
ptr = ptr.getLink();
-
} -
System.out.print(ptr.getData()+ " ");
-
} -
} -
-
/* Class SinglyLinkedList */ -
public class SinglyLinkedList
-
{ -
public static void main(String[] args)
-
{ -
Scanner scan = new Scanner(System.in);
-
/* Creating object of class linkedList */ -
linkedList list = new linkedList();
-
System.out.println("Singly Linked List Test ");
-
char ch;
-
/* Perform list operations */ -
do -
{ -
System.out.println(" Singly Linked List Operations ");
-
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 ");
-
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 ");
-
else -
list.deleteAtPos(p);
-
break;
-
case 5 :
-
System.out.println("Empty status = "+ list.isEmpty());
-
break;
-
case 6 :
-
System.out.println("Size = "+ list.getSize() +" ");
-
break;
-
default :
-
System.out.println("Wrong Entry ");
-
break;
-
} -
/* Display List */ -
list.display();
-
System.out.println(" Do you want to continue (Type y or n) ");
-
ch = scan.next().charAt(0);
-
} while (ch == 'Y'|| ch == 'y');
-
} -
}
-
/* -
* Java Program to Implement Singly Linked List -
*/ -
-
import java.util.Scanner;
-
-
/* Class Node */ -
class Node -
{ -
protected int data;
-
protected Node link;
-
-
/* Constructor */ -
public Node()
-
{ -
link = null;
-
data = 0;
-
} -
/* Constructor */ -
public Node(int d,Node n)
-
{ -
data = d;
-
link = n;
-
} -
/* Function to set link to next Node */ -
public void setLink(Node n)
-
{ -
link = n;
-
} -
/* Function to set data to current Node */ -
public void setData(int d)
-
{ -
data = d;
-
} -
/* Function to get link to next node */ -
public Node getLink()
-
{ -
return link;
-
} -
/* Function to get data from current 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 an element at begining */ -
public void insertAtStart(int val)
-
{ -
Node nptr = new Node(val, null);
-
size++ ;
-
if(start == null)
-
{ -
start = nptr;
-
end = start;
-
} -
else -
{ -
nptr.setLink(start);
-
start = nptr;
-
} -
} -
/* Function to insert an element at end */ -
public void insertAtEnd(int val)
-
{ -
Node nptr = new Node(val,null);
-
size++ ;
-
if(start == null)
-
{ -
start = nptr;
-
end = start;
-
} -
else -
{ -
end.setLink(nptr);
-
end = nptr;
-
} -
} -
/* Function to insert an element at position */ -
public void insertAtPos(int val , int pos)
-
{ -
Node nptr = new Node(val, null);
-
Node ptr = start;
-
pos = pos - 1 ;
-
for (int i = 1; i < size; i++)
-
{ -
if (i == pos)
-
{ -
Node tmp = ptr.getLink() ;
-
ptr.setLink(nptr);
-
nptr.setLink(tmp);
-
break;
-
} -
ptr = ptr.getLink();
-
} -
size++ ;
-
} -
/* Function to delete an element at position */ -
public void deleteAtPos(int pos)
-
{ -
if (pos == 1)
-
{ -
start = start.getLink();
-
size--; -
return ;
-
} -
if (pos == size)
-
{ -
Node s = start;
-
Node t = start;
-
while (s != end)
-
{ -
t = s;
-
s = s.getLink();
-
} -
end = t;
-
end.setLink(null);
-
size --; -
return;
-
} -
Node ptr = start;
-
pos = pos - 1 ;
-
for (int i = 1; i < size - 1; i++)
-
{ -
if (i == pos)
-
{ -
Node tmp = ptr.getLink();
-
tmp = tmp.getLink();
-
ptr.setLink(tmp);
-
break;
-
} -
ptr = ptr.getLink();
-
} -
size-- ;
-
} -
/* Function to display elements */ -
public void display()
-
{ -
System.out.print(" Singly Linked List = ");
-
if (size == 0)
-
{ -
System.out.print("empty ");
-
return;
-
} -
if (start.getLink() == null)
-
{ -
System.out.println(start.getData() );
-
return;
-
} -
Node ptr = start;
-
System.out.print(start.getData()+ "->");
-
ptr = start.getLink();
-
while (ptr.getLink() != null)
-
{ -
System.out.print(ptr.getData()+ "->");
-
ptr = ptr.getLink();
-
} -
System.out.print(ptr.getData()+ " ");
-
} -
} -
-
/* Class SinglyLinkedList */ -
public class SinglyLinkedList
-
{ -
public static void main(String[] args)
-
{ -
Scanner scan = new Scanner(System.in);
-
/* Creating object of class linkedList */ -
linkedList list = new linkedList();
-
System.out.println("Singly Linked List Test ");
-
char ch;
-
/* Perform list operations */ -
do -
{ -
System.out.println(" Singly Linked List Operations ");
-
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 ");
-
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 ");
-
else -
list.deleteAtPos(p);
-
break;
-
case 5 :
-
System.out.println("Empty status = "+ list.isEmpty());
-
break;
-
case 6 :
-
System.out.println("Size = "+ list.getSize() +" ");
-
break;
-
default :
-
System.out.println("Wrong Entry ");
-
break;
-
} -
/* Display List */ -
list.display();
-
System.out.println(" Do you want to continue (Type y or n) ");
-
ch = scan.next().charAt(0);
-
} while (ch == 'Y'|| ch == 'y');
-
} -
}
-
/* -
* Java Program to Implement Singly Linked List -
*/ -
-
import java.util.Scanner;
-
-
/* Class Node */ -
class Node -
{ -
protected int data;
-
protected Node link;
-
-
/* Constructor */ -
public Node()
-
{ -
link = null;
-
data = 0;
-
} -
/* Constructor */ -
public Node(int d,Node n)
-
{ -
data = d;
-
link = n;
-
} -
/* Function to set link to next Node */ -
public void setLink(Node n)
-
{ -
link = n;
-
} -
/* Function to set data to current Node */ -
public void setData(int d)
-
{ -
data = d;
-
} -
/* Function to get link to next node */ -
public Node getLink()
-
{ -
return link;
-
} -
/* Function to get data from current 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 an element at begining */ -
public void insertAtStart(int val)
-
{ -
Node nptr = new Node(val, null);
-
size++ ;
-
if(start == null)
-
{ -
start = nptr;
-
end = start;
-
} -
else -
{ -
nptr.setLink(start);
-
start = nptr;
-
} -
} -
/* Function to insert an element at end */ -
public void insertAtEnd(int val)
-
{ -
Node nptr = new Node(val,null);
-
size++ ;
-
if(start == null)
-
{ -
start = nptr;
-
end = start;
-
} -
else -
{ -
end.setLink(nptr);
-
end = nptr;
-
} -
} -
/* Function to insert an element at position */ -
public void insertAtPos(int val , int pos)
-
{ -
Node nptr = new Node(val, null);
-
Node ptr = start;
-
pos = pos - 1 ;
-
for (int i = 1; i < size; i++)
-
{ -
if (i == pos)
-
{ -
Node tmp = ptr.getLink() ;
-
ptr.setLink(nptr);
-
nptr.setLink(tmp);
-
break;
-
} -
ptr = ptr.getLink();
-
} -
size++ ;
-
} -
/* Function to delete an element at position */ -
public void deleteAtPos(int pos)
-
{ -
if (pos == 1)
-
{ -
start = start.getLink();
-
size--; -
return ;
-
} -
if (pos == size)
-
{ -
Node s = start;
-
Node t = start;
-
while (s != end)
-
{ -
t = s;
-
s = s.getLink();
-
} -
end = t;
-
end.setLink(null);
-
size --; -
return;
-
} -
Node ptr = start;
-
pos = pos - 1 ;
-
for (int i = 1; i < size - 1; i++)
-
{ -
if (i == pos)
-
{ -
Node tmp = ptr.getLink();
-
tmp = tmp.getLink();
-
ptr.setLink(tmp);
-
break;
-
} -
ptr = ptr.getLink();
-
} -
size-- ;
-
} -
/* Function to display elements */ -
public void display()
-
{ -
System.out.print(" Singly Linked List = ");
-
if (size == 0)
-
{ -
System.out.print("empty ");
-
return;
-
} -
if (start.getLink() == null)
-
{ -
System.out.println(start.getData() );
-
return;
-
} -
Node ptr = start;
-
System.out.print(start.getData()+ "->");
-
ptr = start.getLink();
-
while (ptr.getLink() != null)
-
{ -
System.out.print(ptr.getData()+ "->");
-
ptr = ptr.getLink();
-
} -
System.out.print(ptr.getData()+ " ");
-
} -
} -
-
/* Class SinglyLinkedList */ -
public class SinglyLinkedList
-
{ -
public static void main(String[] args)
-
{ -
Scanner scan = new Scanner(System.in);
-
/* Creating object of class linkedList */ -
linkedList list = new linkedList();
-
System.out.println("Singly Linked List Test ");
-
char ch;
-
/* Perform list operations */ -
do -
{ -
System.out.println(" Singly Linked List Operations ");
-
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 ");
-
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 ");
-
else -
list.deleteAtPos(p);
-
break;
-
case 5 :
-
System.out.println("Empty status = "+ list.isEmpty());
-
break;
-
case 6 :
-
System.out.println("Size = "+ list.getSize() +" ");
-
break;
-
default :
-
System.out.println("Wrong Entry ");
-
break;
-
} -
/* Display List */ -
list.display();
-
System.out.println(" Do you want to continue (Type y or n) ");
-
ch = scan.next().charAt(0);
-
} while (ch == 'Y'|| ch == 'y');
-
} -
}
-
/* -
* Java Program to Implement Singly Linked List -
*/ -
-
import java.util.Scanner;
-
-
/* Class Node */ -
class Node -
{ -
protected int data;
-
protected Node link;
-
-
/* Constructor */ -
public Node()
-
{ -
link = null;
-
data = 0;
-
} -
/* Constructor */ -
public Node(int d,Node n)
-
{ -
data = d;
-
link = n;
-
} -
/* Function to set link to next Node */ -
public void setLink(Node n)
-
{ -
link = n;
-
} -
/* Function to set data to current Node */ -
public void setData(int d)
-
{ -
data = d;
-
} -
/* Function to get link to next node */ -
public Node getLink()
-
{ -
return link;
-
} -
/* Function to get data from current 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 an element at begining */ -
public void insertAtStart(int val)
-
{ -
Node nptr = new Node(val, null);
-
size++ ;
-
if(start == null)
-
{ -
start = nptr;
-
end = start;
-
} -
else -
{ -
nptr.setLink(start);
-
start = nptr;
-
} -
} -
/* Function to insert an element at end */ -
public void insertAtEnd(int val)
-
{ -
Node nptr = new Node(val,null);
-
size++ ;
-
if(start == null)
-
{ -
start = nptr;
-
end = start;
-
} -
else -
{ -
end.setLink(nptr);
-
end = nptr;
-
} -
} -
/* Function to insert an element at position */ -
public void insertAtPos(int val , int pos)
-
{ -
Node nptr = new Node(val, null);
-
Node ptr = start;
-
pos = pos - 1 ;
-
for (int i = 1; i < size; i++)
-
{ -
if (i == pos)
-
{ -
Node tmp = ptr.getLink() ;
-
ptr.setLink(nptr);
-
nptr.setLink(tmp);
-
break;
-
} -
ptr = ptr.getLink();
-
} -
size++ ;
-
} -
/* Function to delete an element at position */ -
public void deleteAtPos(int pos)
-
{ -
if (pos == 1)
-
{ -
start = start.getLink();
-
size--; -
return ;
-
} -
if (pos == size)
-
{ -
Node s = start;
-
Node t = start;
-
while (s != end)
-
{ -
t = s;
-
s = s.getLink();
-
} -
end = t;
-
end.setLink(null);
-
size --; -
return;
-
} -
Node ptr = start;
-
pos = pos - 1 ;
-
for (int i = 1; i < size - 1; i++)
-
{ -
if (i == pos)
-
{ -
Node tmp = ptr.getLink();
-
tmp = tmp.getLink();
-
ptr.setLink(tmp);
-
break;
-
} -
ptr = ptr.getLink();
-
} -
size-- ;
-
} -
/* Function to display elements */ -
public void display()
-
{ -
System.out.print(" Singly Linked List = ");
-
if (size == 0)
-
{ -
System.out.print("empty ");
-
return;
-
} -
if (start.getLink() == null)
-
{ -
System.out.println(start.getData() );
-
return;
-
} -
Node ptr = start;
-
System.out.print(start.getData()+ "->");
-
ptr = start.getLink();
-
while (ptr.getLink() != null)
-
{ -
System.out.print(ptr.getData()+ "->");
-
ptr = ptr.getLink();
-
} -
System.out.print(ptr.getData()+ " ");
-
} -
} -
-
/* Class SinglyLinkedList */ -
public class SinglyLinkedList
-
{ -
public static void main(String[] args)
-
{ -
Scanner scan = new Scanner(System.in);
-
/* Creating object of class linkedList */ -
linkedList list = new linkedList();
-
System.out.println("Singly Linked List Test ");
-
char ch;
-
/* Perform list operations */ -
do -
{ -
System.out.println(" Singly Linked List Operations ");
-
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 ");
-
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 ");
-
else -
list.deleteAtPos(p);
-
break;
-
case 5 :
-
System.out.println("Empty status = "+ list.isEmpty());
-
break;
-
case 6 :
-
System.out.println("Size = "+ list.getSize() +" ");
-
break;
-
default :
-
System.out.println("Wrong Entry ");
-
break;
-
} -
/* Display List */ -
list.display();
-
System.out.println(" Do you want to continue (Type y or n) ");
-
ch = scan.next().charAt(0);
-
} while (ch == 'Y'|| ch == 'y');
-
} -
}
-
/* -
* Java Program to Implement Singly Linked List -
*/ -
-
import java.util.Scanner;
-
-
/* Class Node */ -
class Node -
{ -
protected int data;
-
protected Node link;
-
-
/* Constructor */ -
public Node()
-
{ -
link = null;
-
data = 0;
-
} -
/* Constructor */ -
public Node(int d,Node n)
-
{ -
data = d;
-
link = n;
-
} -
/* Function to set link to next Node */ -
public void setLink(Node n)
-
{ -
link = n;
-
} -
/* Function to set data to current Node */ -
public void setData(int d)
-
{ -
data = d;
-
} -
/* Function to get link to next node */ -
public Node getLink()
-
{ -
return link;
-
} -
/* Function to get data from current 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 an element at begining */ -
public void insertAtStart(int val)
-
{ -
Node nptr = new Node(val, null);
-
size++ ;
-
if(start == null)
-
{ -
start = nptr;
-
end = start;
-
} -
else -
{ -
nptr.setLink(start);
-
start = nptr;
-
} -
} -
/* Function to insert an element at end */ -
public void insertAtEnd(int val)
-
{ -
Node nptr = new Node(val,null);
-
size++ ;
-
if(start == null)
-
{ -
start = nptr;
-
end = start;
-
} -
else -
{ -
end.setLink(nptr);
-
end = nptr;
-
} -
} -
/* Function to insert an element at position */ -
public void insertAtPos(int val , int pos)
-
{ -
Node nptr = new Node(val, null);
-
Node ptr = start;
-
pos = pos - 1 ;
-
for (int i = 1; i < size; i++)
-
{ -
if (i == pos)
-
{ -
Node tmp = ptr.getLink() ;
-
ptr.setLink(nptr);
-
nptr.setLink(tmp);
-
break;
-
} -
ptr = ptr.getLink();
-
} -
size++ ;
-
} -
/* Function to delete an element at position */ -
public void deleteAtPos(int pos)
-
{ -
if (pos == 1)
-
{ -
start = start.getLink();
-
size--; -
return ;
-
} -
if (pos == size)
-
{ -
Node s = start;
-
Node t = start;
-
while (s != end)
-
{ -
t = s;
-
s = s.getLink();
-
} -
end = t;
-
end.setLink(null);
-
size --; -
return;
-
} -
Node ptr = start;
-
pos = pos - 1 ;
-
for (int i = 1; i < size - 1; i++)
-
{ -
if (i == pos)
-
{ -
Node tmp = ptr.getLink();
-
tmp = tmp.getLink();
-
ptr.setLink(tmp);
-
break;
-
} -
ptr = ptr.getLink();
-
} -
size-- ;
-
} -
/* Function to display elements */ -
public void display()
-
{ -
System.out.print(" Singly Linked List = ");
-
if (size == 0)
-
{ -
System.out.print("empty ");
-
return;
-
} -
if (start.getLink() == null)
-
{ -
System.out.println(start.getData() );
-
return;
-
} -
Node ptr = start;
-
System.out.print(start.getData()+ "->");
-
ptr = start.getLink();
-
while (ptr.getLink() != null)
-
{ -
System.out.print(ptr.getData()+ "->");
-
ptr = ptr.getLink();
-
} -
System.out.print(ptr.getData()+ " ");
-
} -
} -
-
/* Class SinglyLinkedList */ -
public class SinglyLinkedList
-
{ -
public static void main(String[] args)
-
{ -
Scanner scan = new Scanner(System.in);
-
/* Creating object of class linkedList */ -
linkedList list = new linkedList();
-
System.out.println("Singly Linked List Test ");
-
char ch;
-
/* Perform list operations */ -
do -
{ -
System.out.println(" Singly Linked List Operations ");
-
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 ");
-
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 ");
-
else -
list.deleteAtPos(p);
-
break;
-
case 5 :
-
System.out.println("Empty status = "+ list.isEmpty());
-
break;
-
case 6 :
-
System.out.println("Size = "+ list.getSize() +" ");
-
break;
-
default :
-
System.out.println("Wrong Entry ");
-
break;
-
} -
/* Display List */ -
list.display();
-
System.out.println(" Do you want to continue (Type y or n) ");
-
ch = scan.next().charAt(0);
-
} while (ch == 'Y'|| ch == 'y');
-
} -
}
-
/* -
* Java Program to Implement Singly Linked List -
*/ -
-
import java.util.Scanner;
-
-
/* Class Node */ -
class Node -
{ -
protected int data;
-
protected Node link;
-
-
/* Constructor */ -
public Node()
-
{ -
link = null;
-
data = 0;
-
} -
/* Constructor */ -
public Node(int d,Node n)
-
{ -
data = d;
-
link = n;
-
} -
/* Function to set link to next Node */ -
public void setLink(Node n)
-
{ -
link = n;
-
} -
/* Function to set data to current Node */ -
public void setData(int d)
-
{ -
data = d;
-
} -
/* Function to get link to next node */ -
public Node getLink()
-
{ -
return link;
-
} -
/* Function to get data from current 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 an element at begining */ -
public void insertAtStart(int val)
-
{ -
Node nptr = new Node(val, null);
-
size++ ;
-
if(start == null)
-
{ -
start = nptr;
-
end = start;
-
} -
else -
{ -
nptr.setLink(start);
-
start = nptr;
-
} -
} -
/* Function to insert an element at end */ -
public void insertAtEnd(int val)
-
{ -
Node nptr = new Node(val,null);
-
size++ ;
-
if(start == null)
-
{ -
start = nptr;
-
end = start;
-
} -
else -
{ -
end.setLink(nptr);
-
end = nptr;
-
} -
} -
/* Function to insert an element at position */ -
public void insertAtPos(int val , int pos)
-
{ -
Node nptr = new Node(val, null);
-
Node ptr = start;
-
pos = pos - 1 ;
-
for (int i = 1; i < size; i++)
-
{ -
if (i == pos)
-
{ -
Node tmp = ptr.getLink() ;
-
ptr.setLink(nptr);
-
nptr.setLink(tmp);
-
break;
-
} -
ptr = ptr.getLink();
-
} -
size++ ;
-
} -
/* Function to delete an element at position */ -
public void deleteAtPos(int pos)
-
{ -
if (pos == 1)
-
{ -
start = start.getLink();
-
size--; -
return ;
-
} -
if (pos == size)
-
{ -
Node s = start;
-
Node t = start;
-
while (s != end)
-
{ -
t = s;
-
s = s.getLink();
-
} -
end = t;
-
end.setLink(null);
-
size --; -
return;
-
} -
Node ptr = start;
-
pos = pos - 1 ;
-
for (int i = 1; i < size - 1; i++)
-
{ -
if (i == pos)
-
{ -
Node tmp = ptr.getLink();
-
tmp = tmp.getLink();
-
ptr.setLink(tmp);
-
break;
-
} -
ptr = ptr.getLink();
-
} -
size-- ;
-
} -
/* Function to display elements */ -
public void display()
-
{ -
System.out.print(" Singly Linked List = ");
-
if (size == 0)
-
{ -
System.out.print("empty ");
-
return;
-
} -
if (start.getLink() == null)
-
{ -
System.out.println(start.getData() );
-
return;
-
} -
Node ptr = start;
-
System.out.print(start.getData()+ "->");
-
ptr = start.getLink();
-
while (ptr.getLink() != null)
-
{ -
System.out.print(ptr.getData()+ "->");
-
ptr = ptr.getLink();
-
} -
System.out.print(ptr.getData()+ " ");
-
} -
} -
-
/* Class SinglyLinkedList */ -
public class SinglyLinkedList
-
{ -
public static void main(String[] args)
-
{ -
Scanner scan = new Scanner(System.in);
-
/* Creating object of class linkedList */ -
linkedList list = new linkedList();
-
System.out.println("Singly Linked List Test ");
-
char ch;
-
/* Perform list operations */ -
do -
{ -
System.out.println(" Singly Linked List Operations ");
-
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 ");
-
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 ");
-
else -
list.deleteAtPos(p);
-
break;
-
case 5 :
-
System.out.println("Empty status = "+ list.isEmpty());
-
break;
-
case 6 :
-
System.out.println("Size = "+ list.getSize() +" ");
-
break;
-
default :
-
System.out.println("Wrong Entry ");
-
break;
-
} -
/* Display List */ -
list.display();
-
System.out.println(" Do you want to continue (Type y or n) ");
-
ch = scan.next().charAt(0);
-
} while (ch == 'Y'|| ch == 'y');
-
} -
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.