ADT基础(一)—— List,Stack,and Queue
ADT基础(一)—— List,Stack,and Queue
1 List 表示
数组:易于search,难于insert和remove
链表:难于search,易于insert和remove
//Node类,LinkedList类
public class LinkedList{
Node head = null;
class Node{ //element和next
object element;
Node next;
Node(object e){
this.element = e;
}
}
//输出链表并获取长度
int getLength(){
int length = 0;
Node tmp = head;
while(tmp != null){
length++;
System.out.println(tmp.data);
tmp = tmp.next;
}
return length;
}
//查询element为e的第一个位置
int getIndex(object e){
int index = -1;
Node tmp = head;
while(tmp!=null){
index++:
if(tmp.element == e){
return index;
}
tmp = tmp.next;
}
return -1;
}
//获取指定位置的element
object getObject(int index){
if(index<0||index >= getLength()) //print fault;
Node tmp = head;
if(head==null) //print fault;
for(int i=0;i<index;i++){
tmp = tmp.next;
}
return tmp.element;
}
//头插法
void addHead(object e){
Node newNode = new Node(e);
newNode.next = head;
head = newNode;
}
//尾插法
void addTail(object e){
Node newNode = new Node(e);
if(head == null) head = newNode;
else{
Node tmp = head;
while(tmp.next != null){
tmp = tmp.next;
}
tmp.next = newNode;
}
}
//随机节点插入法
void insert(int index,object e){
int size = getLength();
if(index>=size||index<0) //print fault;
if(index==0) addHead(e);
else if(index == size-1) addTail(e);
else{
Node pre = head;
Node cur = head.next;
for(int i=0;i<index-1;i++){
pre = pre.next;
cur = cur.next;
}
//pre保存索引上一个节点,cur保存索引值当前节点
Node newNode = new Node(e);
pre.next = newNode;
newNode.next = cur;
}
}
//删除头节点
void deleteHead(){
if(head == null) return;
head = head.next;
}
//删除尾节点
void deleteTail(){
if(head==null) return;
Node btmp = head;
Node tmp = btmp.next;
if(tmp == null){
head = null;
return;
}
while(tmp.next != null){
btmp = tmp;
tmp = tmp.next;
}
btmp.next = null;
}
//随机删除节点
void remove(int index){
int size = getLength();
if(index<0||index>=size) //print fault;
if(index == 0) deleteHead();
else if(index == size-1) deleteTail();
else{
Node pre = head;
for(int i=0;i<index-1;i++){
pre = pre.next;
}
pre.next = pre.next.next;
}
}
}
//由单链表的增加删除可以看出,链表想要对指定索引进行操作(增加,删除),则必须获取该索引的前一个元素。记住这句话,对链表算法题很有用。
2 Stack表示
- 后入先出
//栈的链表实现,栈顶在topOfStack,即head处;
//push和pop都在head处
public class StackLi
{
public StackLi( ){ topOfStack = null; }
public boolean isFull( ){ return false; }
public boolean isEmpty( ){ return topOfStack = = null; }
public void makeEmpty( ){ topOfStack = null; }
public void push( object x){
topOfStack = new ListNode(x,topOfStack);
}
public object top(){
if(topOfStack == null) return null;
return topOfStack.element;
}
public void pop() throws Underflow{
if(topOfStack == null) throw new Underflow();
topOfStack = topOfStack.next;
}
public object topAndPop( ){
if(topOfStack == null) return null;
object res = topOfStack.element;
topOfStack = topOfStack.next;
return res;
}
private ListNode topOfStack;
}
//栈的数组实现,栈顶在topOfStack,即在数组n-1位上(假设压入n个元素);
//push和pop依次向后或向前
public class stackAr{
public StackAr( ){
this(DEFAULT_CAPACITY);
}
public StackAr(int capacity){
theArray = new object[capacity];
topOfStack = -1;
}
public boolean isEmpty( ){ return topOfStack == -1; }
public boolean isFull( ){ return topOfStack == theArray.length –1; }
public void makeEmpty( ){ topOfStack = -1; }
public void push( object x ) throws overflow{
if(topOfStack == theArray.length - 1) throw new Overflow();
topOfStack++;
theArray[topOfstack] = x;
}
public object top( ){
if(topOfStack==-1) return null;
return theArray[topOfStack];
}
public void pop( ) throws Underflow{
if(topOfStack == -1) throw new Undewflow();
theArray[topOfStack] == null;
topOfStack--;
}
public object topAndPop( ){
if(topOfStack==-1) return null;
object res = theArray[topOfStack];
theArray[topOfStack] == null;
topOfStack--;
return res;
}
private object [ ] theArray;
private int topOfStack;
static final int DEFAULT_CAPACITY = 10;
}
3 Queue表示
- 插入与删除在不同端,先入先出
//队列的数组实现,在front位删除,在back位插入 front到back由0到n-1
public class QueueAr
{
public QueueAr(){
this(DEFAULT_CAPACITY);
}
public QueueAr( int capacity){
theArray = new Object[capacity];
currentSize = 0;
front = 0;
back = -1;
}
public boolean isEmpty( ){ return currentsize == 0; }
public boolean isfull( ){ return currentSize == theArray.length; }
public Object getfront( )
public void enqueue( Object x ) throw Overflow{
if(currentSize == theArray.length) throw new Overflow();
back++;
if(back == theArray.length) back = 0; //队列满则新元素回到0位插入
theArray[back] = x;
currentSize++;
}
private Object dequeue( ){
if(currentSize == 0) return null;
curretSize--;
object res = theArray[front];
theArray[front] = null;
front++;
if(front == theArray.length) front = 0; //队列删到尾则回到0删除
return res;
}
private Object [ ] theArray;
private int currentSize;
private int front; //删除
private int back; //插入
static final int DEFAULT_CAPACITY = 10;
}
//队列的链表实现,front在head
public class LinkedQueue
{
public LinkedQueue(){
this.head = null;
this.tail = null;
this.size = 0;
}
public boolean IsEmpty(){return size==0;}
public boolean IsFull(){return false};
public void add(object x){
if(size==0){
head = new Node(x);
tail = head;
size++;
}else{
tail.next = new Node(x);
tail = tail.next;
size++;
}
}
public object delete(){
if(size==0) return null;
object res = head.element;
head = head.next;
if(head == null) tail = null; //head为null,代表已经行进到tail.next,此时为空链表
size--;
return res;
}
private Node head;
private Node tail;
private int size;
};
ADT基础(一)—— List,Stack,and Queue的更多相关文章
- page74-泛型可迭代的基础集合数据类型的API-Bag+Queue+Stack
[泛型可迭代的基础集合数据类型的API] 背包:就是一种不支持从中删除元素的集合数据类型——它的目的就是帮助用例收集元素并迭代遍历所有收集到的元素.(用例也可以检查背包是否为空, 或者获取背包中元素的 ...
- bfs和dfs辨析—基础复习(从stack和queue的角度来理解区别,加深理解,不再模糊)
参考: https://www.cnblogs.com/Tovi/articles/6194815.html https://blog.csdn.net/dangzhangjing97/article ...
- Java容器:Stack,Queue,PriorityQueue和BlockingQueue
Stack Queue PriorityQueue BlockingQueue ArrayBlockingQueue LinkedBlockingQueue PriorityBlockingQueue ...
- ADT基础(二)—— Tree,Heap and Graph
ADT基础(二)-- Tree,Heap and Graph 1 Tree(二叉树) 先根遍历 (若二叉树为空,则退出,否则进行下面操作) 访问根节点 先根遍历左子树 先根遍历右子树 退出 访问顺序为 ...
- Java集合的Stack、Queue、Map的遍历
Java集合的Stack.Queue.Map的遍历 在集合操作中,常常离不开对集合的遍历,对集合遍历一般来说一个foreach就搞定了,但是,对于Stack.Queue.Map类型的遍历,还是有一 ...
- [STL]deque和stack、queue
怎么说呢,deque是一种双向开口的连续线性空间,至少逻辑上看上去是这样.然而事实上却没有那么简单,准确来说deque其实是一种分段连续空间,因此其实现以及各种操作比vector复杂的多. 一.deq ...
- python实现之极简stack和queue
用python实现一个极简的stack和queue,那是so easy的事情了,简洁易懂,适合小白~ 直接上代码吧: node: class LinkNode: def __init__( self, ...
- C++ STL stack和queue
C++ STL中独立的序列式容器只有vector,list,deque三种,stack和queue其实就是使用容器适配器对deque进行了封装,使用了新接口. 使用标准库的栈和队列时,先包含相关的头文 ...
- 剑指offer——stack与queue的互相实现
我们知道,stack和queue是C++中常见的container.下面,我们来探究下如何以stack来实现queue,以及如何用queue来实现stack. 首先,先了解下stack与queue的基 ...
随机推荐
- ness使用-漏扫
1.登录nessus后,会自动弹出目标输入弹框: 输入目标IP,可通过CIDR表示法(192.168.0.0/80),范围(192.168.0.1-192.168.0.255),或逗号分隔(192.1 ...
- 2019牛客暑期多校训练营(第十场)F.Popping Balloons(线段树)
题意:现在给你n个点 现在让你横着划三条线间距为r 然后竖着划三条线间距同样为r 现在让你求经过最多的点数 思路:我们首先建一棵关于y区间的线段树 然后枚举x轴 每次更新重叠的点 然后再更新回去 找一 ...
- ACM-ICPC 2018 徐州赛区网络预赛(8/11)
ACM-ICPC 2018 徐州赛区网络预赛 A.Hard to prepare 枚举第一个选的,接下来的那个不能取前一个的取反 \(DP[i][0]\)表示选和第一个相同的 \(DP[i][1]\) ...
- HDU2065 "红色病毒"问题 【组合数学 二项式定理】
HDU2065 "红色病毒"问题 Description: 医学界发现的新病毒因其蔓延速度和Internet上传播的"红色病毒"不相上下,被称为"红色 ...
- HDU - 2328 Corporate Identity(kmp+暴力)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题意:多组输入,n==0结束.给出n个字符串,求最长公共子串,长度相等则求字典序最小. 题解:(居 ...
- Codeforces Round #648 (Div. 2) C. Rotation Matching
题目链接:https://codeforces.com/contest/1365/problem/C 题意 有两个大小为 $n$ 的排列,可以循环左移或右移任意次,问最多有多少对同一值在同一位置. 题 ...
- 国产网络损伤仪 SandStorm -- 只需要拖拽就能删除链路规则
国产网络损伤仪SandStorm可以模拟出带宽限制.时延.时延抖动.丢包.乱序.重复报文.误码.拥塞等网络状况,在实验室条件下准确可靠地测试出网络应用在真实网络环境中的性能,以帮助应用程序在上线部署前 ...
- Chrony时间同步
chrony 服务器 yum -y install chrony cp /etc/chrony.conf{,.bak} #备份默认配置 cat > /etc/chrony.conf <&l ...
- WOJ1022 Competition of Programming 贪心 WOJ1023 Division dp
title: WOJ1022 Competition of Programming 贪心 date: 2020-03-19 13:43:00 categories: acm tags: [acm,wo ...
- Leetcode(198)-打家劫舍
你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定一个代表每 ...