009-算法面试必备-基础数据结构(链表,栈,队列)

今天来三个基础数据结构的学习吧。链表,栈,队列。

主要是看看怎么实现的。思想很重要。

想说的是,对于非计算机专业同学,需要自学数据结构,我曾经在链表,栈,队列这几章中来回的徘徊。

不是我搞不懂这些数据结构,是我进行不下去了,直到有一天,我脚踏实地的写出来,才算弄明白。

实现一个链表

//实现一个链表
class Node <E>{
	Node next = null;
	E data ;
	public Node(E data){
		this.data = data;
	}
}
//一个.java文件中只有一个public class文件
public class MyLinkedList <E> {
	Node head = null;    //整个链表的头结点
	public void addNode(E d){
		Node newNode = new Node(d);
		if(head == null){
			head = newNode;
			return;
		}
		//遍历链表的头结点
		Node temp = head;
		while(temp.next != null){
			temp = temp.next;
		}
		temp.next = newNode;
	}
	//删除链表的第几个节点
	public boolean deleteNode(int index){
		if(index < 1 || index >length()){
			return false;
		}
		//删除链表第一个元素
		if(index == 1){
			head = head.next;
			return true;
		}
		int i = 2;
		Node preNode = head;
		Node curNode = preNode.next;
		while(curNode != null){
			if(i == index){
				preNode.next = curNode.next;   //删除节点
				return true;
			}
			preNode = curNode;               //遍历节点
			curNode = curNode.next;
			i++;
		}
		return true;			
	}
	public int length(){
		int length = 0;
		Node temp = head;
		while( temp != null){
			length++;
			temp = temp.next;
		}
		return length;
	}
	public void printList(){
		Node<E> temp = head;
		while( temp != null){
			System.out.println(temp.data);
			temp = temp.next;
		}
	}
	public static void  main(String[] args){
		MyLinkedList<Integer> list = new MyLinkedList<Integer>();
		list.addNode(1);
		list.addNode(2);
		list.addNode(3);
		list.printList();
		
	}
}

实现一个栈

class Node<E> {
	Node<E> next = null;
	E data;
	public Node(E data){
		this.data = data;
	}
}
//使用链表实现栈,需要使用一个指针就可以了
public class ArrayStack<E> {
	Node<E> top = null;
	public boolean isEmpty(){
		return top == null;
	}
	//入栈
	public void push(E data){
		Node<E> newNode = new Node<E>(data);
		newNode.next = top;  //对于栈来说,后入先出LIFO
		top = newNode;
	}
	//出栈
	public E pop(){
		if(this.isEmpty()){
			return null;
		}
		E data = top.data;
		top = top.next;
		return data;
	}
	//返回最节点上的值
	public E peek(){
		if(isEmpty()){
			return null;
		}
		return top.data;
	}
	public static void main(String[] args){
		ArrayStack<Integer> st = new ArrayStack<Integer>();
		st.push(1);
		st.push(2);
		st.push(3);
		System.out.println(st.pop());
		System.out.println(st.pop());
		System.out.println(st.pop());
	}
}

实现一个队列

//利用链表实现队列
//队列的思想是:先进先出
//记住这种队列的实现
class Node<E>{
	Node<E> next = null;
	E data;
	public Node(E data){
		this.data = data;
	}
}
//先进先出,使用两个指针来表示,否则,在pop的时候,需要遍历,增加程序的复杂度
public class ArrayQueue<E> {
	private Node<E> head = null;
	private Node<E> tail = null;
	
	public boolean isEmpty(){
		return head == tail;
	}
	public void put(E data){  //入队的时候,尾巴增长
		Node<E> newNode = new Node<E> (data);
		if(head == null && tail == null){ //队列为空
			head = tail = newNode;
		}else{
			tail.next = newNode;
			tail = newNode;
		}
	}
	public E pop(){       //出队的时候,从头开始出队
		if(this.isEmpty()){
			return null;
		}
		E data = head.data;
		head = head.next;
		return data;
	}
	public int size(){
		Node<E> tmp = head;
		int n = 0;
		while(tmp != null){
			n++;
			tmp = tmp.next;
		}
		return n;
	}
	
	public static void main(String[] args){
		ArrayQueue<Integer> q = new ArrayQueue<Integer>();
		q.put(1);
		q.put(2);
		q.put(3);
		System.out.println("队列的长度" + q.size());
		System.out.println("队列首元素" + q.pop());
		System.out.println("队列的长度" + q.size());
		System.out.println("队列的首元素" + q.pop());
	}	
}

一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值