数据结构之背包,队列,栈

1:数据抽象

概念:

抽象数据类型,是一种能够对使用者隐藏数据表示的数据类型,抽象数据类型之所以重要,是因为他在程序设计上支持封装。

本节目标:本节将介绍三种抽象类型,用java实现,背包,堆栈,队列等最简单的数据结构。

    1. 背包   背包是一种不支持从中删除元素的集合数据类型。他的目的就是帮助用例手机元素并迭代遍历所有收集到的元素。API图示参考1.1

      import java.util.Iterator;
      import java.util.NoSuchElementException;
      /**    bag数据结构的实现  后进先出 不支持删除元素 */
      public class Bag<Item> implements Iterable<Item> {
      
          private int N;
      
          private Node<Item> first;
      
          // helper linked list class
          private static class Node<Item> {
              private Item item;
      
              private Node<Item> next;
          }
      
          /**
           * Is this bag empty?
           *
           * @return true if this bag is empty; false otherwise
           */
          public boolean isEmpty() {
              return first == null;
          }
      
          /**
           * Returns the number of items in this bag.
           *
           * @return the number of items in this bag
           */
          public int size() {
              return N;
          }
      
          /**
           * Adds the item to this bag.
           *
           * @param item the item to add to this bag
           */
          public void add(Item item) {
              Node<Item> oldfirst = first;
              first = new Node<Item>();
              first.item = item;
              first.next = oldfirst;
              N++;
          }
      
          /**
           * Initializes an empty bag.
           */
          public Bag() {
              N = 0;
              first = null;
          }
      
          @Override
          public Iterator<Item> iterator() {
              return new ListIterator<Item>(first);
          }
      
          private class ListIterator<Item> implements Iterator<Item> {
      
              private Node<Item> current;
      
              public ListIterator(Node<Item> first) {
                  current = first;
              }
      
              @Override
              public boolean hasNext() {
                  return current != null;
              }
      
              @Override
              public Item next() {
                  if (!hasNext())
                      throw new NoSuchElementException();
                  Item item = current.item;
                  current = current.next;
                  return item;
              }
      
              @Override
              public void remove() {
                  throw new UnsupportedOperationException();
              }
          }
      }
    2. 队列 先进先出 可以联想在剧院门前排队等待入门的人

      先进先出的队列结构,FIFO。Code实现

      1. package com.luochuang.demo.stdlib;
        
        import org.w3c.dom.ls.LSException;
        
        import java.util.Iterator;
        import java.util.NoSuchElementException;
        
        public class Queue<Item> implements Iterable<Item> {
        
            private int N;// number of element on queue
        
            private Node<Item> first;// beginning of queue
        
            private Node<Item> last;// end of queue
        
            public class Node<Item> {
                private Node<Item> next;
        
                private Item item;
            }
        
            public Item peek() {
                if (isEmpty()) {
                    throw new NoSuchElementException();
                }
                return first.item;
            }
        
            /**
             * add item to queue
             *
             * @param item the item to add
             */
            public void enQueue(Item item) {
                Node<Item> oldlast = last;
                last = new Node<Item>();
                last.item = item;
                last.next = null;
                if (isEmpty()) {
                    first = last;
                } else {
                    oldlast.next = last;
                }
                N++;
            }
        
            /**
             * Removes and returns the item on this queue that was least recently added.
             *
             * @return the item on this queue that was least recently added
             * @throws java.util.NoSuchElementException if this queue is empty
             */
            public Item deQueue() {
                if (isEmpty()) {
                    throw new NoSuchElementException();
                }
                Item item = first.item;
                first = first.next;
                N--;
                if (isEmpty()) {
                    last = null;
                }
                return item;
            }
        
            /**
             * is this queue empty?
             *
             * @return true if this queue is empty otherwise
             */
            public boolean isEmpty() {
                return first == null;
            }
        
            /**
             * Returns the number of items in this queue.
             *
             * @return the number of items in this queue
             */
            public int size() {
                return N;
            }
        
            /**
             * Initializes an empty queue.
             */
            public Queue() {
                first = null;
                last = null;
                N = 0;
            }
        
            @Override
            public Iterator<Item> iterator() {
                return null;
            }
        
            public class ListIterator implements Iterator<Item> {
        
                private Node<Item> current;
        
                public ListIterator(Node<Item> first) {
                    first = current;
                }
        
                @Override
                public boolean hasNext() {
                    return current == null;
                }
        
                @Override
                public Item next() {
                    if (!hasNext())
                        throw new NoSuchElementException();
                    Item item = current.item;
                    current = current.next;
                    return item;
                }
        
                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }
            }
        }
    3. 下压栈   

      下压栈是一种基于后进先出的队列结构,LIFO。比如点击超级链接中的按钮,当回退之后能重新访问之前的页面,即从栈中弹出。也可以联想桌面上放的一匝便签.
package com.luochuang.demo.stdlib;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class Stack<Item> implements Iterable<Item> {

    private int N;

    private Node<Item> first;

    /**
     * Initializes an empty stack
     */
    public Stack() {
        first = null;
        N = 0;
    }

    /**
     * Adds the item to this stack.
     * @param item the item to add
     */
    public void push(Item item) {
        Node<Item> oldfirst = first;
        first = new Node<Item>();
        first.item = item;
        first.next = oldfirst;
        N++;
    }

    /**
     * Removes and returns the item most recently added to this stack.
     *
     * @return the item most recently added
     * @throws java.util.NoSuchElementException if this stack is empty
     */
    public Item pop() {
        if (isEmpty())
            throw new NoSuchElementException("Stack underflow");
        Item item = first.item; // save item to return
        first = first.next; // delete first node
        N--;
        return item; // return the saved item
    }

    /**
     * Returns (but does not remove) the item most recently added to this stack.
     * @return the item most recently added to this stack
     * @throws java.util.NoSuchElementException if this stack is empty
     */
    public Item peek() {
        if (isEmpty()) throw new NoSuchElementException("Stack underflow");
        return first.item;
    }

    public boolean isEmpty() {
        return first == null;
    }

    public int size() {
        return N;
    }

    private class Node<Item> {
        private Node<Item> next;

        private Item item;
    }

    /**
     * Returns a string representation of this stack.
     * @return the sequence of items in the stack in LIFO order, separated by spaces
     */
    public String toString() {
        StringBuilder s = new StringBuilder();
        for (Item item : this)
            s.append(item + " ");
        return s.toString();
    }

    @Override
    public Iterator<Item> iterator() {
        return new ListIterator(first);
    }

    public class ListIterator implements Iterator<Item> {

        public Node<Item> current;

        public ListIterator(Node<Item> first) {
            first = current;
        }

        @Override
        public boolean hasNext() {
            return current == null;
        }

        @Override
        public Item next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            Item item = current.item;
            current = current.next;
            return item;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
}

数据结构之背包,队列,栈

时间: 2024-08-01 04:07:43

数据结构之背包,队列,栈的相关文章

Java数据结构和算法之栈与队列

二.栈与队列 1.栈的定义 栈(Stack)是限制仅在表的一端进行插入和删除运算的线性表. (1)通常称插入.删除的这一端为栈顶(Top),另一端称为栈底(Bottom). (2)当表中没有元素时称为空栈. (3)栈为后进先出(Last In First Out)的线性表,简称为LIFO表. 栈的修改是按后进先出的原则进行. 每次删除(退栈)的总是当前栈中"最新"的元素,即最后插入(进栈)的元素,而最先插入的是被放在栈的底部,要到最后才能删除. 图1 [示例]元素是以a1,a2,-,a

算法系列(六)数据结构之表队列和栈

在http://blog.csdn.net/robertcpp/article/details/51559333一文中,我们讲了排序,这一章来介绍一下基本数据结构:表.队列.栈和它们的简单实现 一.表ADT 1.数组实现顺序表 通过对数组操作,来直接对表进行增删查改操作,这种线性表查找某个位置的元素花费的时间为O(1),但是插入删除元素花费的时间为O(n),如果对表的操作更多的是访问操作,那么选择这种实现更为合适. 下面是一个简单实现 package com.algorithm.list; im

数据结构实验三《栈和队列》

<数据结构>实验三 栈和队列 一.实验目的 巩固栈和队列数据结构,学会运用栈和队列. 1.回顾栈和队列的逻辑结构和受限操作特点,栈和队列的物理存储结构和常见操作. 2.学习运用栈和队列的知识来解决实际问题. 3.进一步巩固程序调试方法. 4.进一步巩固模板程序设计. 二.实验时间 准备时间为第5周到第6周,具体集中实验时间为6周第2次课.2个学时. 三.实验内容 1.自己选择顺序或链式存储结构,定义一个空栈类,并定义入栈.出栈.取栈元素基本操作.然后在主程序中对给定的N个数据进行验证,输出各个

第三章:1.栈和队列 -- 栈的表示及实现

前言: 栈和队列 是两种重要的线性结构.从数据结构角度来看,栈和队列也是线性表,它的特殊性在于其操作是线性表的子集,是操作受限的线性表,因此可以称作限定性的数据结构. (限定性:如.人为的规定线性表只能从表尾插入和删除结点数据元素,那么这样的线性表就是栈) 目录: 1.栈 2.栈的应用举例 3.栈与递归的实现 4.队列 5.离散事件模型 正文: 栈的定义 栈(stack) 如上所说,就是限定只能在表尾进行插入和删除的线性表.表尾 称为 栈顶(top), 表头 称为 栈底 (bottom),没有数

JavaScript数据结构和算法----队列

前言 队列和栈很像,只是用了不同的原则.队列是遵循先进先出(FIFO)原则的一组有序的的项,队列在尾部添加新元素,从顶部移除元素.最新添加的元素必须必须排队在队列的,末尾.可以想象食堂排队买饭的样子. 一.创建队列 1.创建一种数据结构来保存队列里面的数据,这里选择数组 2.声明一些栈的方法 enqueue(element(s)) : 添加一个或一些元素到队列的末尾 dequeue() : 移除队列第一个的元素(就是排队在最前面的),同时返回被移除的元素. front() : 返回队列第一个的元

5、蛤蟆的数据结构笔记之五链栈实现

5.蛤蟆的数据结构笔记之五链栈实现 本篇名言:"人生就像奕棋,一步失误,全盘皆输." 昨天对栈和队列进行了定义.这次我们来看下如何使用代码来实现链栈和链队列,后续蛤蟆会记录如何将栈应用到实际问题中. 栈一般是顺序结构,但是也可以采用链式存储结构,具体如下实现. 欢迎转载,转载请标明出处: 1.  定义结构体 #define MAX_STACKS10 typedef struct { intkey; /*otherfields */ }element; typedef struct st

浅谈算法和数据结构: 五 优先级队列与堆排序

转载自:http://www.cnblogs.com/yangecnu/p/Introduce-Priority-Queue-And-Heap-Sort.html 浅谈算法和数据结构: 五 优先级队列与堆排序 在很多应用中,我们通常需要按照优先级情况对待处理对象进行处理,比如首先处理优先级最高的对象,然后处理次高的对象.最简单的一个例子就是,在手机上玩游戏的时候,如果有来电,那么系统应该优先处理打进来的电话. 在这种情况下,我们的数据结构应该提供两个最基本的操作,一个是返回最高优先级对象,一个是

数据结构5_链队列

本文用指针形式实现了先进先出的队列,与栈队列类似,需要设计一个队列节点类,一个队列类, 同时队列类为节点类的友元函数:不过与链栈不同的是,链栈仅用单一指针来实现入栈和出栈 而链队列需要有front和rear两个指针(皆为队列节点类指针),front指针负责处理出列,rear处理入列 #include<iostream>using namespace std;class LinkQueue;class QNode   //每个节点的类{    char *data;    //每个节点的数据类型

数据结构6_顺序队列(循环队列)

本文实现了顺序队列,与链队列不同的是,顺序队列需要考虑一个问题, 问题情况如下, 解决办法:循环队列,当rear到分配的数组空间末尾时,转到数组头 但是当q.rear==q.front时,又如何区分一种是空队列,一种是满队列的情况呢 这里有两种方案 本次代码实现了第一种方法,同时设置了一个技术变量length,稍加改动便可实现第二个方法 代码如下: #include<iostream>using namespace std;//该顺序队列为循环队列,解决队尾指针达到最大值,队列中有空闲单元,但