class Node{
int val;
Node next;
Node(int val){
this.val=val;
this.next=null;
}
public String toString(){
return String.format("Node(%d)",val);
}
}
public class MyLinkedList{
private static Node pushFront(Node head,int val){
Node node=new Node(val);
node.next=head;
return node;
}
private static Node pushBack(Node head,int val){
Node node=new Node(val);
if(head==null){
return node;
}
else{
Node last=head;
while(last.next!=null){
last=last.next;
}
last.next=node;
return head;
}
}
private static Node popFront(Node head) {
if(head==null){
System.err.println("空链表无法删除!");
return null;
}
return head.next;
}
private static Node popBack(Node head) {
if(head==null){
System.err.println("空链表无法删除!");
return null;
}
if(head.next==null){
return null;
}
else{
Node secondToLast=head;
while(secondToLast.next!=null){
secondToLast=secondToLast.next;
}
secondToLast.next=null;
return head;
}
}
private static void print(Node head){
System.out.print("链表为;");
for(Node cur=head;cur!=null;cur=cur.next){
System.out.print(cur+"--->");
}
System.out.println("null");
}
public static void main(String[] args){
Node head=null;
head=pushFront(head,5);
head=pushFront(head,7);
head=pushFront(head,2);
print(head);
head=pushBack(head,37);
head=pushBack(head,55);
head=pushBack(head,21);
print(head);
head=popFront(head);
head=popFront(head);
print(head);
head=popBack(head);
head=popBack(head);
print(head);
}
}