Modified the stack list in the textbook closer to gamer point of view:
//Queue List
//First in First out FIFO
#include <iostream>
using namespace std;
class monster {
int damage;
public:
monster(int a=0):damage(a){}
int pop(){
return damage;
}
};
struct wave {
monster data;//creates a monster object when you make a wave object
wave* _next;//points to the address of a wave object
wave(const monster& a, wave* next):data(a), _next(next){}//constructor accepts a reference of a monster object and pointer that points to a wave object.
//will also initiate monster object 'a' and cast it to monster object 'data', will do the same with _next with next
};
class game {
wave* _start;
wave* _end;
public:
game(): _start(NULL), _end(NULL){}
~game (){
wave* current;
while (current = _start){
_start = _start->_next;
delete current;
}
}//~game()
void add(int d){
wave* p = new wave(d,0);//second parameter is 0 because in a Queue when you're adding it should be in the end, hence the _next value should be null
if (_start)
_end->_next=p;//if there is a _start the _end will have the new address
else
_start=p;//if there are no _start meaning this is the first one will assign the _start address to the new wave object
_end = p;//regardless if there was a value for _start or not, assign the _end to the new object
}
monster rem(){//returns a monster object
monster data;
if (_start){
wave* current=_start;
data = _start->data;//assign the _start's monster object(data, inside wave class) with the new monster object (created in this function)
_start=_start->_next;//assign the _start address to the _next _start address;
delete current;
if (!_start)//if the address of _start is NULL change the _end to NUll as well
_end=NULL;
}
return data;
}
bool empty(){return _start == NULL;}
};
int main(){
game d;//_start and _end has Null address
//Push Data onto the queue
d.add(3);
d.add(30);
d.add(10);
d.add(50);
//remove the first node
d.rem();
//pop off data off the queue
while (!d.empty())
cout << d.rem().pop() << endl;
return 0;
}
Nothing but personal opinion of whats going on with my life and some codings :D
Sunday, April 1, 2012
Monday, March 26, 2012
A simpler version of a Stack list with comments:
//STACK LIST
//RULE:
//LIFO (Last In, First Out), opposite of a Queue List, FIFO (First In First Out)
#include <iostream>
using namespace std;
template< class T >
class Stack {
private:
int MAX;//control how big a stack can be
int top;//the top of the stack = the data that will be giong out
T* items;//item points to a T address
public:
Stack(int size){//set the size of the stack
MAX = size;
top = -1;//-1 because when user create the object there will be no "top" data, think of this as a rack
items = new T[MAX];//set the number of item the stack can have
}
~Stack(){ delete [] items; }// deletes the entire array/stack
void push(T c){
if(full()){//check if the full function is true
cout << "Stack Full!" << endl;
//return 1;
}
items[++top] = c;//if the stack istn full will put the "data" in the first array[++top]
}
T pop(){
if(empty()){
cout << "Stack Empty!" << endl;
//return 1;
}
return items[top--];//reason why you have top= -1 in the constructor;
//reduce redundant variable in this function. without top=-1, will need to make a variable that holds the item data
}
int empty(){ return top == -1; }
int full(){ return top+1 == MAX; }
};
int main(){
Stack<char> st(10);
//the letters 'A' - 'J'
for(int i = 65; i < 75; i++)
st.push(i);//as the I increment, so does the top
//remove all the data
for(int j = 0; j < 11; j++)
cout << st.pop() << endl;//as the J increment top in the the object decrements hence getting the "stack" link list (taking from the last one in first)
return 0;
}
//STACK LIST
//RULE:
//LIFO (Last In, First Out), opposite of a Queue List, FIFO (First In First Out)
#include <iostream>
using namespace std;
template< class T >
class Stack {
private:
int MAX;//control how big a stack can be
int top;//the top of the stack = the data that will be giong out
T* items;//item points to a T address
public:
Stack(int size){//set the size of the stack
MAX = size;
top = -1;//-1 because when user create the object there will be no "top" data, think of this as a rack
items = new T[MAX];//set the number of item the stack can have
}
~Stack(){ delete [] items; }// deletes the entire array/stack
void push(T c){
if(full()){//check if the full function is true
cout << "Stack Full!" << endl;
//return 1;
}
items[++top] = c;//if the stack istn full will put the "data" in the first array[++top]
}
T pop(){
if(empty()){
cout << "Stack Empty!" << endl;
//return 1;
}
return items[top--];//reason why you have top= -1 in the constructor;
//reduce redundant variable in this function. without top=-1, will need to make a variable that holds the item data
}
int empty(){ return top == -1; }
int full(){ return top+1 == MAX; }
};
int main(){
Stack<char> st(10);
//the letters 'A' - 'J'
for(int i = 65; i < 75; i++)
st.push(i);//as the I increment, so does the top
//remove all the data
for(int j = 0; j < 11; j++)
cout << st.pop() << endl;//as the J increment top in the the object decrements hence getting the "stack" link list (taking from the last one in first)
return 0;
}
Tuesday, March 20, 2012
Wednesday, March 14, 2012
March 12, Lab
printing bitrate
#include <iostream>
using namespace std;
char* bit(unsigned int);
char isOn(unsigned int, unsigned int);
int main(){
int test=10;
cout << sizeof(int) << endl;
cout << sizeof(test) << endl;
cout << bit(test) << endl;
return 0;
}
char* bit(unsigned int i){
static char binary[31]; //int is 4 bytes meaning 32 bits
int num;
int c;
for(c=sizeof(i)*8-1,num=0;c>=0,num < sizeof(i)*8;c--,num++)
binary[num]=(isOn(i,c));//num increments the array of binary while c looks at every bitrate one by one
return binary;
}
char isOn(unsigned int num, unsigned int bitNo){
unsigned int m=1 << bitNo;
if ((num & m) != 0)
return '1';
else
return '0';
}
printing bitrate
#include <iostream>
using namespace std;
char* bit(unsigned int);
char isOn(unsigned int, unsigned int);
int main(){
int test=10;
cout << sizeof(int) << endl;
cout << sizeof(test) << endl;
cout << bit(test) << endl;
return 0;
}
char* bit(unsigned int i){
static char binary[31]; //int is 4 bytes meaning 32 bits
int num;
int c;
for(c=sizeof(i)*8-1,num=0;c>=0,num < sizeof(i)*8;c--,num++)
binary[num]=(isOn(i,c));//num increments the array of binary while c looks at every bitrate one by one
return binary;
}
char isOn(unsigned int num, unsigned int bitNo){
unsigned int m=1 << bitNo;
if ((num & m) != 0)
return '1';
else
return '0';
}
Monday, March 12, 2012
Wednesday, February 1, 2012
Subscribe to:
Posts (Atom)