Linked list using either Java or C. 1. Write a program that creates a linked lis
ID: 3755509 • Letter: L
Question
Linked list using either Java or C.
1. Write a program that creates a linked list and loads it with the numbers 0 to 9. Create the functions, init, makeNode, and findTail. In order to make sure everything is working, the contents of the list will have to be displayed. Make a showList function and call it after the list has been loaded by the for loop 3. Add the capability to add a node between two nodes. Make sure and ask the user which node they want to place the new node after, and the value of the node to be created. You will need to make a function that finds the node the user requested. This node will be sent to insertAfter function. 4. Add the capability to delete a node. Fashion your solution in a manner similar to what was done in exercise 3.Explanation / Answer
import java.util.Scanner;
//node structure
class Node
{
protected int data;
protected Node link;
// constructor
public Node()
{
link = null;
data = 0;
}
public Node(int d,Node n)
{
data = d;
link = n;
}
public void setLink(Node n)
{
link = n;
}
public void setData(int d)
{
data = d;
}
public Node getLink()
{
return link;
}
public int getData()
{
return data;
}
}
//linked list class
class linkedList
{
protected Node head;
protected Node tail ;
public int size ;
//constructor
public linkedList()
{
head = null;
tail = null;
size = 0;
}
//method to get head elemet
int getHead()
{
if( head!=null)
return head.getData();
return -1;//if head not exist
}
//method to get tail
int getTail()
{
if( tail!=null)
return tail.getData();
return -1;//if head not exist
}
public boolean isEmpty()
{
return head == null;
}
public int getSize()
{
return size;
}
//methods to add
public void InsertAtstarting(int val)
{
Node nptr = new Node(val, null);
size++ ;
if(head == null)
{
head = nptr;
tail = head;
}
else
{
nptr.setLink(head);
head = nptr;
}
}
public void InsertAtending(int val)
{
Node nptr = new Node(val,null);
size++ ;
if(head == null)
{
head = nptr;
tail = head;
}
else
{
tail.setLink(nptr);
tail = nptr;
}
}
public void InsertAtposition(int val , int pos)
{
Node nptr = new Node(val, null);
Node ptr = head;
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++ ;
}
//method to delete
public void deleteAtposition(int pos)
{
if (pos == 1)
{
head = head.getLink();
size--;
return ;
}
if (pos == size)
{
Node s = head;
Node t = head;
while (s != tail)
{
t = s;
s = s.getLink();
}
tail = t;
tail.setLink(null);
size --;
return;
}
Node ptr = head;
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-- ;
}
public void Show()
{
System.out.print(" Singly Linked List = ");
if (size == 0)
{
System.out.print("empty ");
return;
}
if (head.getLink() == null)
{
System.out.println(head.getData() );
return;
}
Node ptr = head;
System.out.print(head.getData()+ "->");
ptr = head.getLink();
while (ptr.getLink() != null)
{
System.out.print(ptr.getData()+ "->");
ptr = ptr.getLink();
}
System.out.print(ptr.getData()+ " ");
}
}
public class SingleLinkedList
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
linkedList list = new linkedList();
System.out.println("Linked List ");
char ch;
do
{//displaying menu
System.out.println(" Linked List Operations ");
System.out.println("1. insert at begining");
System.out.println("2. insert at tail");
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");
System.out.println("7. Display Head");
System.out.println("8. Display Tail");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
list.InsertAtstarting( scan.nextInt() );
break;
case 2 :
System.out.println("Enter integer element to insert");
list.InsertAtending( 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.InsertAtposition(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.deleteAtposition(p);
break;
case 5 :
System.out.println("Empty status = "+ list.isEmpty());
break;
case 6 :
System.out.println("Size = "+ list.getSize() +" ");
break;
case 7 :
System.out.println("Head = "+ list.getHead()+" ");
break;
case 8 :
System.out.println("Tail = "+ list.getTail() +" ");
break;
default :
System.out.println("Wrong Entry ");
break;
}
list.Show();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
output:
run:
Linked List
Linked List Operations
1. insert at begining
2. insert at tail
3. insert at position
4. delete at position
5. check empty
6. get size
7. Display Head
8. Display Tail
Enter option:
1
Enter integer element to insert
0
Singly Linked List = 0
Do you want to continue (Type y or n)
y
Linked List Operations
1. insert at begining
2. insert at tail
3. insert at position
4. delete at position
5. check empty
6. get size
7. Display Head
8. Display Tail
Enter option:
1
Enter integer element to insert
1
Singly Linked List = 1->0
Do you want to continue (Type y or n)
y
Linked List Operations
1. insert at begining
2. insert at tail
3. insert at position
4. delete at position
5. check empty
6. get size
7. Display Head
8. Display Tail
Enter option:
1
Enter integer element to insert
2
Singly Linked List = 2->1->0
Do you want to continue (Type y or n)
y
Linked List Operations
1. insert at begining
2. insert at tail
3. insert at position
4. delete at position
5. check empty
6. get size
7. Display Head
8. Display Tail
Enter option:
1
Enter integer element to insert
5
Singly Linked List = 5->2->1->0
Do you want to continue (Type y or n)
y
Linked List Operations
1. insert at begining
2. insert at tail
3. insert at position
4. delete at position
5. check empty
6. get size
7. Display Head
8. Display Tail
Enter option:
1
Enter integer element to insert
8
Singly Linked List = 8->5->2->1->0
Do you want to continue (Type y or n)
y
Linked List Operations
1. insert at begining
2. insert at tail
3. insert at position
4. delete at position
5. check empty
6. get size
7. Display Head
8. Display Tail
Enter option:
7
Head = 8
Singly Linked List = 8->5->2->1->0
Do you want to continue (Type y or n)
y
Linked List Operations
1. insert at begining
2. insert at tail
3. insert at position
4. delete at position
5. check empty
6. get size
7. Display Head
8. Display Tail
Enter option:
8
Tail = 0
Singly Linked List = 8->5->2->1->0
Do you want to continue (Type y or n)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.