#include <iostream>
using namespace std;
template <class T>
class Stack;
template <class T>
class SNode{
T _data;
SNode<T>* _next;
SNode(T data, SNode<T>* next){
_data = data;
_next = next;
}
friend class Stack<T>;
};
template <class T>
class Stack{
SNode<T>* _top;
public:
Stack();
void push(T data);
T pop();
bool isEmpty()const;
virtual ~Stack();
};
template <class T>
Stack<T>::Stack(){
_top = (SNode<T>*)0;
}
template <class T>
void Stack<T>::push(T data){
SNode<T>* temp = new SNode<T>(data,_top);
_top = temp;
}
template <class T>
T Stack<T>::pop(){
T val = _top->_data;
SNode<T>* toDel = _top;
_top = _top->_next;
delete toDel;
return val;
}
template <class T>
bool Stack<T>::isEmpty()const{
return !_top;
}
template <class T>
Stack<T>::~Stack(){
SNode<T>* toDel;
while(_top){
toDel = _top;
_top = _top->_next;
delete toDel;
}
}
int main(){
Stack<int> S;
int i;
for(i=100;i<2000;i+=10){
S.push(i);
}
while(!S.isEmpty()){
cout<<S.pop()<<" ";
}
cout<<endl<<"*******************************************"<<endl;
for(i=100;i<2000;i+=10){
S.push(i);
}
for(i=0;i<10;i++){
cout<<S.pop()<<" ";
}
cout<<endl<<"*******************************************"<<endl;
return 0;
}
Tuesday, 20 March 2012
Monday, 19 March 2012
Template
template<class type>
class Tqnode{
type _date;
Tqnode<type>* _next;
Tqnode<type>* _prev;
Tqnode(type data,
Tqnode<type>* next = (Tqnode<type> *) 0, Tqnode<type>* prev = (Tqnode<type>*)0){
_data = data;
_next = next;
_prev = prev;
}
};
class Tqnode{
type _date;
Tqnode<type>* _next;
Tqnode<type>* _prev;
Tqnode(type data,
Tqnode<type>* next = (Tqnode<type> *) 0, Tqnode<type>* prev = (Tqnode<type>*)0){
_data = data;
_next = next;
_prev = prev;
}
};
bit print...
void printBits(unsigned int V){
for(int i=sizeof(unsigned int)*8-1;i>=0;!((i+1)%4) && putchar(' '),putchar('0' + !!(V & (1<<i))), (!i) && putchar('\n'),i--);
}
void SetBitPattern(unsigned int& val, const char* bitPattern, int startBitIndex){
int i = -1;
while(bitPattern[++i]);
while(i--){
if(bitPattern[i]-'0'){
val = val | 1 << startBitIndex;
}
else{
val = val & ~(1 << startBitIndex);
}
startBitIndex++;
}
}
for(int i=sizeof(unsigned int)*8-1;i>=0;!((i+1)%4) && putchar(' '),putchar('0' + !!(V & (1<<i))), (!i) && putchar('\n'),i--);
}
void SetBitPattern(unsigned int& val, const char* bitPattern, int startBitIndex){
int i = -1;
while(bitPattern[++i]);
while(i--){
if(bitPattern[i]-'0'){
val = val | 1 << startBitIndex;
}
else{
val = val & ~(1 << startBitIndex);
}
startBitIndex++;
}
}
important : set bit pattern
Thursday, 15 March 2012
the output question
#include <iostream>
using namespace std;
void hanoi(int n, char p1, char p2, char p3){
static int row = 0;
if(n==1){
cout<<(++row)<<" "<<p1<<"--------->"<<p3<<endl;
}
else{
hanoi(n-1, p1, p3, p2);
cout<<(++row)<<" "<<p1<<"--------->"<<p3<<endl;
hanoi(n-1, p2, p1, p3);
}
}
int main(){
hanoi(4, 'A', 'B', 'C');
return 0;
}
I cannot figure out the second output........why the follow output is 2 A----->c....
my answer is C----->B
using namespace std;
void hanoi(int n, char p1, char p2, char p3){
static int row = 0;
if(n==1){
cout<<(++row)<<" "<<p1<<"--------->"<<p3<<endl;
}
else{
hanoi(n-1, p1, p3, p2);
cout<<(++row)<<" "<<p1<<"--------->"<<p3<<endl;
hanoi(n-1, p2, p1, p3);
}
}
int main(){
hanoi(4, 'A', 'B', 'C');
return 0;
}
I cannot figure out the second output........why the follow output is 2 A----->c....
my answer is C----->B
Tuesday, 13 March 2012
problem from prnBits
void prnBits(unsigned int val){
for(int i = sizeof(unsigned int)*8-1; i>=0;printf("%d", !!(val & (1<<i--))));
}
I’m confused about the "sizeof (unsigned int) * 8 -1".........Why should multiplied by 8....
for(int i = sizeof(unsigned int)*8-1; i>=0;printf("%d", !!(val & (1<<i--))));
}
I’m confused about the "sizeof (unsigned int) * 8 -1".........Why should multiplied by 8....
Monday, 12 March 2012
How is works.....Walkthrough w4
const char * w4(float val, char ch){
static char bar[81];
int i;
for(i = 0; i<val && i<80;i++){
bar[i] = ch;
}
bar[i] = 0;
return bar;
}
static char bar[81];
int i;
for(i = 0; i<val && i<80;i++){
bar[i] = ch;
}
bar[i] = 0;
return bar;
}
Subscribe to:
Posts (Atom)