《算法导论》chapter 10

基本的数据结构有4种,栈,队列,链表,有根树,栈和队列都属于动态集合。

 

关于栈(stack)

栈有2种基本操作

  • pop()
  • push()

pop()是弹出,push()压入

栈是一种先进后出的策略(last-in, first-out, LIFO),举个栗子,小明整理箱子,小明的《热恋天堂》是最先放在箱子了,这个时候,他想看《热恋天堂》了,但是书在最下面,于是他只能把其他东西pop(),才能拿到《热恋天堂》。

再拿数组来举个栗子,比如说数组S[0-n],如果想要得到S[0],那其他的元素只能pop()掉其他数组元素。

如果栈的位置不够了,再继续插入就会出现栈上溢出的错误(overflow),对空栈进行pop()会发生栈下溢出,其实这个很好理解,举个栗子(x3),如果你让一个16g的手机存20g的电影,肯定是不可能的,系统也会给出提示告诉你内存不足,同样的,一个土豪有100w,(土豪应该不止这么点 = =),有一天他脑抽了,一口气把钱花完了,然后想去买瓶水,然后发现钱不够了。

 

Java实现的栈(最简单的一种)

 1  public class Stack {
 2 
 3     /**
 4      * top : 栈的长度
 5      * push : 压入元素
 6      * pop : 弹出元素
 7      * 栈的原理是先进后出,(last-in,first-out,LIFO),栈每次弹出都是最近压入的元素
 8      *top指向栈顶
 9      */
10 
11   int S[] = new int[7];
12   int top;
13 
14   boolean Empty()
15   {
16       if (top == 0)
17       {
18           return true;
19       }
20       else
21       {
22           return false;
23       }
24   }
25 
26   boolean push(int x)
27   {
28    if (top>=S.length-1)
29    {
30        System.out.println("Overflow");
31        return true;
32    }
33    else
34    {
35        top = top+1;
36        S[top] = x;
37        return true;
38    }
39   }
40 
41   int pop()
42   {
43       if (top<0)
44       {
45           System.out.println("Underflow");
46           return 0;
47       }
48       else
49       {
50           int x = S[top--];
51           return x;
52       }
53   }
54 }

 

队列

 

队列和栈其实差不多,都是线性表,有两种基本操作,入队,出队,队列的概念是(first-in, first-out FIFO)先进先出,队列相对来说比栈好理解一点(个人感觉),超市结账和队列一样,谁先过来,谁先结账,

队列有队头和队尾,新进来的顾客要结账是在队尾的位置上,要结账的人是在队头,队列也会有上溢出和下溢出,这点和栈一样。

 

Java实现的队列(最简单的一种)

 1 public class Queue {
 2 
 3     /**
 4      * 队列特性是 先进先出(first-in first-out FIFO)
 5      * Insert : 入队
 6      * Delete : 删除
 7      * head  :  队头
 8      * tail :   队尾
 9      * capacity : 数组大小
10      * 队列插入是在队尾插入,出队是第
11      */
12 
13     int [] Q;
14     private int head;
15     private int tail;
16     private int size;
17     private int capacity;
18     public Queue(int capacity)
19     {
20         this.capacity = capacity;
21         tail = this.size = 0;
22         head = capacity-1;
23         Q = new int[this.capacity];
24     }
25 
26     /**
27      * 判断队列还能不能继续入队
28      */
29 
30     boolean isFull(Queue queue)
31     {
32         return (queue.size == queue.capacity);
33     }
34 
35     boolean isEmpty(Queue queue)
36     {
37         return (queue.size == 0);
38     }
39 
40     void  enqueue(int item)
41     {
42         if (isFull(this))
43             return;
44         this.head = (this.tail+1) % this.capacity;
45         this.Q[this.head] = item;
46         this.size = this.size+1;
47         System.out.println(item+ " enqueued to queue");
48     }
49 
50     int deque()
51     {
52         if (isEmpty(this))
53             return Integer.MIN_VALUE;
54         int item = this.Q[this.tail];
55         this.tail = (this.tail+1) % this.capacity;
56         this.size = this.size-1;
57         return item;
58     }
59 
60     int tail()
61     {
62         if (isEmpty(this))
63         {
64                 return Integer.MIN_VALUE;
65         }
66         return this.Q[this.tail];
67     }
68 
69     int head()
70     {
71         if (isEmpty(this))
72         {
73                 return Integer.MIN_VALUE;
74         }
75         return this.Q[this.head];
76     }
77 
78 }

 

posted @ 2018-09-06 13:32  WuCola  阅读(124)  评论(0)    收藏  举报