循环单链表,双向链表,顺序栈

循环单链表

循环单链表是单链表的另一种形式,其结构特点链表中最后一个结点的指针域不再是结束标记,而是指向整个链表的第一个结点,从而使链表形成一个环。和单链表相同,循环链表也有带头结点结构和不带头结点结构两种,带头结点的循环单链表实现插入和删除操作较为方便。

package practice;


class TestClink{
	
	class Entry{//内部类
		int data;
		Entry next;
		public Entry(){//无参数的构造方法
			this.data = -1;
			next = null;
		}
		public Entry(int data){//有参数的构造方法
			this.data = data;
			next = null;
		}
	}
	private Entry head = null;
	public TestClink(){
		this.head = new Entry();
		this.head.next  = this.head;
	}
	 //头插法
	public void insertHead (int val){
		Entry entry = new Entry(val);
		entry.next = this.head.next;
		this.head.next = entry; 
	}
	//尾插法
	public void insertTile (int val){
		Entry entry = new Entry(val);
		Entry cur = this.head;
		while(cur.next != head){//遍历找到尾巴
			cur = cur.next;
		}
		entry.next = cur.next;
		cur.next = entry;
	}

	//删除循环单链表中所有的val值
	public void deteleVal(int val){
		Entry cur = this.head.next;
		Entry prev = this.head;//前驱
		while(cur != this.head){
			if(cur.data == val){
				prev.next = cur.next;
				cur = prev.next;
			}else{
				prev = cur;
				cur = cur.next;
			}
		}
	}
	//链表长度
	public int getLength(){
		Entry cur = this.head;
		int count=0;
		while(cur.next != this.head){
			count++;
			cur = cur.next;
		}
		return count;
	}
	//是否为空
	public boolean isEmpty(){
		Entry cur = this.head;
		if(cur.next != this.head){
		return false;
		}
		return true;
	}
	//打印函数
	public void show(){
		Entry cur = this.head.next;
		while(cur != this.head){
			System.out.print(cur.data+" ");
			cur = cur.next;
		}
		System.out.println();
	}
	
}
public class circle {

	public static void main(String[] args) {
		TestClink t1 = new TestClink();
		for(int i = 0;i<9;i++){
			t1.insertHead(i);
		}
		t1.deteleVal(1);
		t1.show();
		System.out.println("链表长度为:"+t1.getLength());
	}

}

双向链表

双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。

package practice;

class DoubleLink{
	
class Entry{
	int data;
	Entry next;//后继
	Entry prio;//前驱
	public Entry(){
		this.data = -1;
		this.next = null;
		this.prio = null;
	}
	public Entry(int data){
		this.data = data;
		this.next = null;
		this.prio = null;
	}
}
	private Entry head = null;
	public DoubleLink(){
		this.head = new Entry();
	}
	//show
	public void show(){
		Entry cur = new Entry();
		cur = this.head.next;
		while(cur != null){
			System.out.print(cur.data+" ");
			cur = cur.next;
		}
		System.out.println();
	}
	//头插法
	public void insertHead(int val){
		Entry entry = new Entry(val);
		entry.next = this.head.next;
		entry.prio = this.head;
		this.head.next = entry;
		if(entry.next!=null){
		entry.next.prio = entry;
		}
	}
	//尾插法
	public void insertTail(int val){
		Entry entry = new Entry(val);
		Entry cur = this.head;
		while(cur.next != null){
			cur = cur.next;
		}
		cur.next = entry;
		entry.prio = cur;
}
	//删除值为val的所有结点
	public void deleteEntry(int val){
		Entry cur = this.head.next;
		while(cur != null){
			if(cur.data == val){
				cur.prio.next = cur.next;
				if(cur.next != null){//删除最后一个结点的时候cur.next为null
					cur.next.prio = cur.prio;
				}
			}
			cur = cur.next;
		}
	}
	
	//链表长度
	public int getLength(){
		Entry cur = this.head.next;
		int len = 0;
		while(cur != null){
			len++;
			cur = cur.next;
		}
		return len;
	}
}

public class DoubleCircle {

	public static void main(String[] args) {
		DoubleLink t1 = new DoubleLink();
		t1.insertHead(1);
		t1.insertHead(10);
		t1.insertTail(20);
		t1.show();
		t1.deleteEntry(1);
		t1.show();
		System.out.println("链表长度为:"+t1.getLength());
	}

}

顺序栈(先进后出)

栈(stack)又名堆栈,它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。

package practice;

class Stacka{
	int top;
	int [] elem;
	
	public Stacka(){
		this(10);
	}
	public Stacka(int size){
		this.elem = new int[size];
		this.top = 0;
	}
	//栈是否为满
	public boolean isFull(){
		if(this.top==this.elem.length){
			return true;
		}
		return false;
	}
	//栈是否为空
	public boolean isEmpty(){
		if(this.top==0){
			return true;
		}
		return false;
	}
	
	//入栈
	public boolean push(int val){
		if(isFull()){
			return false;
		}
		this.elem[this.top++] = val;
		return true;
	}
	//出栈
	public void pop(){
		if(isEmpty()){
			return;
		}
		--this.top;
	}
	//得到栈顶元素
	public int getTop(){
		if(isEmpty()){
			return -1;
		}
		return this.elem[this.top-1];//不能进行--this.top;
	}
	//show
	public void show(){
		for(int i = 0;i<this.top;i++){
			System.out.print(this.elem[i]+" ");
		}
		System.out.println();
	}
	
}
public class Stack {

	public static void main(String[] args) {
		Stacka s1 = new Stacka();
		for(int i = 0;i<10;i++){
			s1.push(i);
		}
		s1.show();
		s1.pop();
		s1.show();
		System.out.println(s1.getTop());

	}

}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值