現在對C++學習了一段時間
自己寫了兩個版本的Stack:
Java版本:
源代碼Stack
package org;
public class Stack
public static class Link
protected Object data;
protected Link next;
public Link(Object data
this
this
}
}
private Link head = null;
public void push(Object data)
head = new Link(data
}
public Object peek()
return head
}
public Object pop()
if (head == null)
return null;
Object o = head
head = head
return o;
}
} 測試代碼StackTest
package org;
import junit
public class StackTest extends TestCase
public void test
Stack s = new Stack();
assertEquals(null
s
s
assertEquals(
assertEquals(
assertEquals(
assertEquals(null
}
public void test
Stack s = new Stack();
assertEquals(null
s
s
assertEquals(
assertEquals(
assertEquals(
assertEquals(null
}
}
C++版本:
源代碼:
Stack
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
class Stack
struct Link
Link* next;
void* data;
Link(void* dat
}*head;
public :
Stack() : head(
void push(void* data)
head = new Link(data
}
void* pop()
if (head ==
return
void* object = head
Link* oldHead = head;
head = oldHead
delete oldHead;
return object;
}
void* peek()
return head ? head
}
};
int main()
ifstream in(
Stack text;
string line;
while(getline(in
text
string* s;
while((s = (string*)text
cout << *s << endl;
delete s;
}
}
From:http://tw.wingwit.com/Article/program/net/201311/11550.html