Algorithm - Linear Structures

Linear Data Structures:

  • Stacks
  • Queues
  • Deques
  • Lists

The names given to the ends are not significant. What distinguishes one linear structure from another is the way in which items are added and removed, in particular the location where these additions and removals occur.

What is a Stack?

stack (sometimes called a “push-down stack”) is an ordered collection of items where the addition of new items and the removal of existing items always takes place at the same end(top).  The end opposite the top is known as the "base".

Items stored in the stack that are closer to the base is the longest, the most recently added item is the position to be removed first.(LIFO, last in first out).

The Stack Abstract Data Type

  • stack() create a new stack that is empty. It needs no parameters and returns an empty stack.
  • push(item) adds a new item to the top of the stack. It needs the item and returns nothing.
  • pop() removes the top item from the stack. It nees no parameters and returns the item. The stack is modified.
  • peek()  returns the top item from the stack but does not remove it. It needs no parameters. The stack is not modified.
  • isEmpty() tests to see whether the stack is empty. It needs no paraemters and returns a boolean value.
  • size() returns the number of items on the stack. It needs no parameters and returns an integer.

What is Queue?

A queue is an ordered collection of items where the addition of new items happens at one end, called the “rear,” and the removal of existing items occurs at the other end, commonly called the “front.”  (FIFO, First In First Out)

The Queue Abstract Data Type

  • Queue() creates a new queue that is empty. It needs no parameters and returns an emtpy queue.
  • enqueue(item) adds a new item to the rear of the queue. It needs the item and returns nothing.
  • dequeue() removes the front item from the queue. It needs no parameters and returns the item. The queue is modified.
  • isEmpty() tests to see whether the queue is empty. It needs no parameters and returns a boolean value.
  • size() returns the number of items in the queue. It needs no parameters and returns an integer.

What is Deque(double-ended queue)?

it has two ends, a front and a rear, and the items remain positioned in the collection. What makes a deque different is the unrestrictive nature of adding and removing items.

The Deque Abstract Data Type

  • Deque() create a new deque that is empty. It needs no parameters and returns an empty deque.
  • addFront(item) adds a new item to the front of the deque. It needs the item and returns nothing.
  • addRear(item) adds new item to the rear of the deque. It needs the item and returns nothing.
  • removeFront() removes the front item fron the deque. It needs no parameters and returns the item. The deque is modified.
  • removeRear() removes the rear item fron the deque. It needs no parameters and returns the item. The deque is modified.
  • isEmpty() tests to see whether the deque is empty. It needs no parameters and returns a boolean value.
  • size() returns the number of items in the deque. It needs no parameters and returns an integer.

时间: 2024-07-30 06:41:49

Algorithm - Linear Structures的相关文章

hdu, KMP algorithm, linear string search algorithm, a nice reference provided

reference: Rabin-Karp and Knuth-Morris-Pratt Algorithms By TheLlama– TopCoder Member https://www.topcoder.com/community/data-science/data-science-tutorials/introduction-to-string-searching-algorithms/ // to be improved #include <cstdio> #include <

用python语言讲解数据结构与算法总述(一)

关于数据结构与算法讲解的书籍很多,但是用python语言去实现的不是很多,最近有幸看到一本这样的书籍,由Brad Miller and David Ranum编写的<Problem Solving with Algorithms and Data Structures Using Python>,地址为:http://interactivepython.org/runestone/static/pythonds/index.html是英文的,写的不错,里面代码的实现也很详细,很多有趣的例子,于

ISO 7816-4: Interindustry Commands for Interchange

5. Basic Organizations 5.1 Data structures5.2 Security architecture of the card 5.3 APDU message structure 5.4 Coding conventions for command headers, data fields and response trailers 5.5 Logical channels 5.6 Secure messaging 5.1 Data structures Thi

Computational Methods in Bayesian Analysis

Computational Methods in Bayesian Analysis Computational Methods in Bayesian Analysis [Markov chain Monte Carlo][Gibbs Sampling][The Metropolis-Hastings Algorithm][Random-walk Metropolis-Hastings][Adaptive Metropolis] About the author This notebook w

A Linear Time Majority Vote Algorithm

介绍一种算法,它可以在线性时间和常数空间内,在一个数组内找出出现次数超过一半的某个数字. 要解决这个问题并不难,可以使用排序或哈希,但是这两种算法都不能同时满足时间或空间的要求. 然而,该算法(A Linear Time Majority Vote Algorithm )却可以在同时满足这两个条件的情况下完美地解决问题. 现在将该算法简单描述如下: 对于数组中出现的某个数字设为待定数字,如果它出现则将其出现次数加一,如果没有出现则减一,如果减至零则将当前数字更换为新的待定数字.这样线性遍历之后可

1.1 Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures.

思路: 假设给定字符串用的是ASCII编码,那么总共就只有256个字符,新建一个256个元素的boolean数组, 遍历字符串,将出现的字符在boolean数组所在位置置 1.如果碰到已经置一,表明出现重复字符,返回false. public class IsUniqueChars_1 { public boolean solu(String s) { boolean table[] = new boolean[256]; for (int i = 0; i < s.length(); i++)

Linear Discriminant Analysis Algorithm

线性判别分析算法. 逻辑回归是一种分类算法,传统上仅限于两类分类问题. 如果有两个以上的类,那么线性判别分析算法是首选的线性分类技术.LDA的表示非常直接.它包括数据的统计属性,为每个类计算.对于单个输入变量,这包括: 每个类的平均值. 在所有类中计算的方差. 通过计算每个类的差别值并对具有最大值的类进行预测,可以做出预测. 该技术假定数据具有高斯分布(钟形曲线),因此,最好先从数据中删除异常值. 这是一种简单而强大的分类预测建模问题的方法. 原文地址:https://www.cnblogs.c

Algorithm | hash

A basic requirement is that the function should provide a uniform distribution of hash values. A non-uniform distribution increases the number of collisions and the cost of resolving them. A critical statistic for a hash table is called the load fact

Dungeon Generation Algorithm

http://www.gamasutra.com/blogs/AAdonaac/20150903/252889/Procedural_Dungeon_Generation_Algorithm.php This post explains a technique for generating randomized dungeons that was first described by TinyKeepDev here. I'll go over it in a little more detai