数据结构-单链表实现队列

本文探讨了如何使用数据结构中的单链表来实现队列,重点在于队列的先进先出(FIFO)特性。队列的操作在链表的两端进行,插入操作通常在队尾,而删除操作则在队头执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

队列的特点是先进先出,与堆栈不同的是,队列的操作在两端分别进行!


package 数据结构;

public class LinkQueue
{
	/*
	 * 使用单链表实现队列
	 */
	
	private Link rear ;//指向队尾节点(新增)
	private Link front ;//指向队头节点(删除)
	
	//添加
	public void add ( int data )
	{
		Link newLink = new Link ( data ) ;
		
		//如果front为空,说明队列为空,所以队列的首尾节点
		if ( isEmpty () )
		{
			front = newLink ;
			rear = newLink ;
			return ;
		}
		
		rear.next = newLink ;
		rear = newLink; 
	}
	
	//删除
	public Link delete ()
	{
		if ( isEmpty () )    
		{
			System.out.println ( "队列为空" );
			return null;
		}
		
		Link current = front ;
		front = front.next ;
		
		return current ;
	}
	
	//判断是否为空
	public boolean isEmpty ()
	{
		return front == null ;
	}
	
	//遍历
	public void display ()
	{
		Link current = front ;//遍历时用的索引
		
		if ( current == null )
		{
			System.out.println ( "队列为空" );
			return ;
		}
		
		while ( current != null )
		{
			System.out.print ( current.data + " " );
			current = current.next ;
		}
		System.out.println ();
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值