public class LinkedListf<E> {
static class node <E>{
E data;
node<E> next;
node(E value) {
data = value;
next = null;
}
}
static node head;
// display the list
static void printList() {
node p = head;
System.out.print("\n[");
//start from the beginning
while(p != null) {
System.out.print(" " + p.data + " ");
p = p.next;
}
System.out.print("]");
}
//insertion at the beginning
void insertatbegin(E data) {
//create a link
node lk = new node(data);
// point it to old first node
lk.next = head;
//point first to new first node
head = lk;
//pointing old node to new node
}
void deletedata(E data){
node del = head;
node prev = null;
if(del != null && del.data == data){
head= del.next;
return;
}
while (del != null && del.data != data){
prev = del;
del= del.next;
}
prev.next=del.next;
}
void insertafter(E index, E data){
node sert = head;
//node prev = null;
node ins = new node(data);
while (sert != null){
if(sert.data == index){
ins.next = sert.next;
sert.next = ins;
break;
}
sert = sert.next;
}
}
public static void main(String args[]) {
int k=0;
insertatbegin(12);
insertatbegin(22);
insertatbegin(30);
deletedata(12);
insertafter(44, 2);
System.out.println("Linked List: ");
// print list
printList();
}
} //issue is im not sure if its correct, and I get a cannot run from a static point because I had to change the functions from static when trying to call the functions in MAIN as is.
pretty vague but that seems like a reasonable intent
