查看: 1759| 回复: 4
跳转到指定楼层
上一主题 下一主题
收起左侧

[CS61B_Spring 2015] DISCUSSION 6

全局:
公开课
学校名称: UC BERKLEY
Unit号: 6
开课时间: 2015-06-01
课程全名: CS61B DATA STRUCTURES
平台: 其他

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
RT~本次是要用两个stack 实现一个queue

上一篇:[CS61B_Spring 2015] HW03
下一篇:[CS61B_Spring 2015] LAB6 HUGLIFE
🔗
 楼主| karte_polo 2015-7-4 23:42:00 | 只看该作者
全局:
Stack 方面我写了一个mystack类,有POP PUSH 和 IS_EMPTY三个方法,功能和作业里一开头ADT里的STACK 一模一样


public class Squeue {
        mystack main;
        mystack Buffer;
        boolean adding;
        public Squeue(){
                main = new mystack();
                Buffer = new mystack();
                adding = true;
        }
        public void enqueue(int item){
                if (!adding){
                        swap(Buffer,main);
                        adding = true;
                }
                main.push(item);
        }
        private void swap(mystack a,mystack b){
                while (!a.is_empty()){
                        b.push(a.pop());
                }
        }
        public int dequeue(){
                if (adding){
                        swap(main,Buffer);
                        adding =false;
                }
                int r = Buffer.pop();
                return r;
        }
        public static void main(String[] args){
       //Simple Tests
                Squeue s = new Squeue();
                s.enqueue(1);
                s.enqueue(2);
                s.enqueue(3);
                s.enqueue(4);
                s.enqueue(5);
                System.out.println(s.dequeue());
                System.out.println(s.dequeue());
        }

       
       

}
回复

使用道具 举报

全局:
真的好的      。
回复

使用道具 举报

🔗
Casualet 2015-8-9 22:04:41 | 只看该作者
全局:
1:null

2:
  2-1:stack
  2-2:map
  2-3:set
3:
  3-1:use two maps to store k-v and v-k pairs respectively.
      for the method numLessThan(K), just use int count to recore the result, and get the value by iterating through the map.
  3-2:use two priority queues, qu1 and qu2, and a count variable to record the number of items in the current ADT. when we need getMedian():
   if(n%2!=0), we dequeue count/2 items to qu2, and count/2+1 is what we want. we then dequeue the remaining count/2 items to qu2, and then move items in qu2 back to qu1; The procedure is similar for n%2==0;


4:
public class SQueue{
  Stack s1;
  Stack s2;

  public SQueue(){
     s1=new Stack();
     s2=new Stack();
  }
  public void enqueue(int item){
       while(!s2.isEmpty()){
           s1.push(s2.pop());      
       }
       s1.push(item);
       while(!s1.isEmpty()){
           s2.push(s1.pop());  
       }
  }
  public int dequeue(){
     return s2.pop();
  }
  public static void main(String args[]){
      SQueue test= new SQueue();

      test.enqueue(1);
      test.enqueue(2);
      test.enqueue(3);
      test.enqueue(4);
      test.enqueue(5);
      System.out.println(test.dequeue());
      System.out.println(test.dequeue());
      System.out.println(test.dequeue());
      System.out.println(test.dequeue());
      System.out.println(test.dequeue());
  }
}

//离最新进度越来越近了。。。。
回复

使用道具 举报

🔗
HNAKXR 2016-2-12 12:55:50 | 只看该作者
全局:
1 Assorted ADTs
No questions.

2 Solving Problems with ADTs
Stack
Set
Map

3 More Complicated ADTs
Write this ADT based on Map. Create two maps: K->V and V->K. For numLessThan, invoke keys(), sort the keys and find the number of keys less than K.
Use a List. Sort before getMedian(). Using two priority queues, one for less than median, and the other for larger than median.

4 ADTing in Circles
  1. import java.util.Stack;

  2. public class SQueue {
  3.         private Stack<Integer> s1;

  4.         public SQueue() {
  5.                 s1 = new Stack<Integer>();
  6.         }

  7.         public void enqueue(int item) {
  8.                 Stack<Integer> s2 = new Stack<Integer>();
  9.                 while (!s1.isEmpty()) {
  10.                         s2.push(s1.pop());
  11.                 }
  12.                 s1.push(item);
  13.                 while (!s2.isEmpty()) {
  14.                         s1.push(s2.pop());
  15.                 }
  16.         }

  17.         public int dequeue() {
  18.                 return s1.pop();
  19.         }
  20. }
复制代码

评分

参与人数 1大米 +20 收起 理由
zzwcsong + 20

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表