Java版本和C++版本简单Stack程序

上一篇 / 下一篇  2007-08-06 11:47:00 / 个人分类:.NET+C

现在对C++学习了一段时间,把C++的特性和Java做比较有很强烈的快感:P

  自己写了两个版本的Stack:

  Java版本:

  源代码Stack.java

以下是引用片段:
  package org;
  public class Stack ...{
  public static class Link ...{
  protected Object data;
  protected Link next;
  public Link(Object data, Link next) ...{
  this.data = data;
  this.next = next;
  }
  }
  private Link head = null;
  public void push(Object data) ...{
  head = new Link(data, head);
  }
  public Object peek() ...{
  return head.data;
  }
  public Object pop() ...{
  if (head == null)
  return null;
  Object o = head.data;
  head = head.next;
  return o;
  }
  } 测试代码StackTest.java
  package org;
  import junit.framework.TestCase;
  public class StackTest extends TestCase ...{
  public void test1() ...{
  Stack s = new Stack();
  assertEquals(null, s.pop());
  s.push("a");
  s.push("b");
  assertEquals("b", s.peek());
  assertEquals("b", s.pop());
  assertEquals("a", s.pop());
  assertEquals(null, s.pop());
  }
  public void test2() ...{
  Stack s = new Stack();
  assertEquals(null, s.pop());
  s.push(new Integer(1));
  s.push(new Integer(2));
  assertEquals(2, ((Integer)s.peek()).intValue());
  assertEquals(2, ((Integer)s.pop()).intValue());
  assertEquals(1, ((Integer)s.pop()).intValue());
  assertEquals(null, s.pop());
  }
  }

 

现在对C++学习了一段时间,把C++的特性和Java做比较有很强烈的快感:P

  自己写了两个版本的Stack:

  Java版本:

  源代码Stack.java

以下是引用片段:
  package org;
  public class Stack ...{
  public static class Link ...{
  protected Object data;
  protected Link next;
  public Link(Object data, Link next) ...{
  this.data = data;
  this.next = next;
  }
  }
  private Link head = null;
  public void push(Object data) ...{
  head = new Link(data, head);
  }
  public Object peek() ...{
  return head.data;
  }
  public Object pop() ...{
  if (head == null)
  return null;
  Object o = head.data;
  head = head.next;
  return o;
  }
  } 测试代码StackTest.java
  package org;
  import junit.framework.TestCase;
  public class StackTest extends TestCase ...{
  public void test1() ...{
  Stack s = new Stack();
  assertEquals(null, s.pop());
  s.push("a");
  s.push("b");
  assertEquals("b", s.peek());
  assertEquals("b", s.pop());
  assertEquals("a", s.pop());
  assertEquals(null, s.pop());
  }
  public void test2() ...{
  Stack s = new Stack();
  assertEquals(null, s.pop());
  s.push(new Integer(1));
  s.push(new Integer(2));
  assertEquals(2, ((Integer)s.peek()).intValue());
  assertEquals(2, ((Integer)s.pop()).intValue());
  assertEquals(1, ((Integer)s.pop()).intValue());
  assertEquals(null, s.pop());
  }
  }


TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

我的栏目

日历

« 2008-08-08  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 3101
  • 日志数: 87
  • 建立时间: 2007-04-09
  • 更新时间: 2008-07-21

RSS订阅

Open Toolbar