package SuanFa.ListNode;
import java.util.Stack;
public class LN {
public static void main(String[] args) {
SingleLinkedList proList=new SingleLinkedList();
ProNode p1 = new ProNode(1, "可乐");
ProNode p2 = new ProNode(2, "薯片");
ProNode p3 = new ProNode(3, "蛋糕");
ProNode p4 = new ProNode(4, "奶茶");
ProNode p5 = new ProNode(5, "雪糕");
proList.add(p1);
proList.add(p2);
proList.add(p3);
proList.add(p4);
proList.add(p5);
proList.list();
System.out.println("查找倒数第K个节点");
proList.findLastK(3);
System.out.println("有效的节点个数");
System.out.println(proList.listLength());
System.out.println("头插法反转之后");
proList.reverseList().list();
System.out.println("从尾到头输出,方法一:递归");
proList.reversePrint(proList.head);
System.out.println("从尾到头输出,方法二:栈");
proList.reversePrint2();
}
}
class SingleLinkedList{
ProNode head=new ProNode(0,"");
public ProNode getHead(){
return head;
}
public void add(ProNode p){
ProNode tmpNode=head;
while (tmpNode.next!=null){
tmpNode=tmpNode.next;
}
tmpNode.next=p;
}
public void list(){
if(head.next==null){
System.out.println("这是个空列表");
return;
}
ProNode tmpNode = head.next;
while (tmpNode!=null){
System.out.println(tmpNode);
tmpNode=tmpNode.next;
}
}
//求有效节点的个数
public int listLength(){
ProNode tmpNode=head.next;
int length=0;
while (tmpNode!=null){
length++;
tmpNode=tmpNode.next;
}
return length;
}
//查找倒数第K个节点
public ProNode findLastK(int k){
ProNode tmpNode1=head.next;
ProNode tmpNode2=head.next;
if(head.next==null){
System.out.println("这是空的");
return null;
}
while (k>0){
tmpNode1=tmpNode1.next;
k--;
}
while (tmpNode1!=null){
tmpNode1=tmpNode1.next;
tmpNode2=tmpNode2.next;
}
System.out.println(tmpNode2);
return tmpNode2;
}
//反转链表,采用头插法
//还有别的办法,还没有看
public SingleLinkedList reverseList(){
SingleLinkedList newList = new SingleLinkedList();
newList.head=new ProNode(-1,"");
ProNode dummyNode=newList.head; //新链表的倒数第二个节点
ProNode curNode=head.next;//旧链表的第一个节点
ProNode nexNode;
while (curNode!=null) {
nexNode = curNode.next;//旧链表的第二个节点;
curNode.next=dummyNode.next;
dummyNode.next=curNode;
curNode=nexNode;
}
return newList;
}
//从尾到头打印单链表
//方法一,递归
public void reversePrint(ProNode head){
if(head!=null) {
if (head.next != null) {
reversePrint(head.next);
}
System.out.println(head);
}
}
//从尾到头打印单链表
//方法二:栈
public void reversePrint2(){
Stack<ProNode> stack = new Stack<>();
ProNode tmpNode=head.next;
while (tmpNode!=null){
stack.push(tmpNode);
tmpNode=tmpNode.next;
}
while (!stack.empty()) {
System.out.println(stack.pop());
}
}
//删除指定的元素的节点
public void removeNode(int val){
ProNode tmp=head;
// ProNode tNext=head.next;
while (tmp.next != null) {
if (tmp.next.no == val) {
tmp.next = tmp.next.next;
return;
} else {
tmp = tmp.next;
}
}
return ;
}
}
class ProNode {
int no;
String pname;
ProNode next;
public ProNode(int no,String pname) {
this.no = no;
this.pname=pname;
}
@Override
public String toString() {
return "ProNode{" + "no=" + no + ", pname=" + pname + "}";
}
}
ps:用linkedlist实现栈:
LinkedList list = new LinkedList();
list.addFirst("a");
list.addFirst("b");
list.addFirst("c");
list.addFirst("d");
//d c b a
//相当于push
list.removeFirst();
//c b a
//相当于pop
//遍历
Iterator it2=list.iterator();
while(it2.hasNext()){
System.out.print(it2.next()+" ");
}
ps:
// list.add("a");
// list.add("b");
// list.add("c");
// list.add("d");
// //a b c b
// list.removeLast();
感觉也能实现栈,没深究
// add和addLast一样
// list.remove(); //底层调用removeFirst;
反转单链表:
```java
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
```