Page 1 of 1

need help in C++, stack, queue and Array in game

PostPosted: Fri Apr 17, 2015 3:12 am
by Masky94
i have an idea to make a simple game from using stack queue and array or at least two of them, but i still need expert to tell me how to really code them into game, is there any tutorials?

for queue, i have an idea like building army/unit, that must have a queue to build them one by one, but i need an example of the code.

for stack, i have an idea about using power-up, that last-in first-out theory, also need an example of the coding.

appreciated your expert's suggestion if you willing to help, thank you

Re: need help in C++, stack, queue and Array in game

PostPosted: Fri Apr 17, 2015 11:05 am
by koala
Well, in C, which is pretty much like in C++, you can code stack and queue using array.

stack:
Code: Select all
#define MAX 100

int stack[MAX];
int top = 0; // first empty place

// add an element
int push (int x) {
    if (top == MAX) return 0; // stack is full, operation failed
    stack[top++] = x;
    return 1; // operation succeeded
}

// take an element
int pop () {
    int x;
    if (top == 0) return -1; // stack is empty, operation failed (return -1, or any other negative number,
                                      // or 0, if stack contains natural numbers)
    x = stack[--top];
    return x;
}

// read the top, without removing the element
int read() {
    int x;
    if (top == 0) return -1; // stack is empty, operation failed
    x = stack[top];
    return x;
}

// is stack empty
int empty() {
    if (top == 0) return 1;
    else     return 0;
}

// is stack full
int full() {
    if (top == MAX) return 1;
    else     return 0;
}


You can code these five functions to be unique for all stacks, if you have more of them. You just need one more argument, which stack you are referring to.
Code: Select all
#define MAX 100

typedef struct stack {
    int elements[MAX];
    int top;
} Stack;

Stack stack1, stack2, stack3, stack4, stack5;
stack1.top = 0; stack2.top = 0; stack3.top = 0; stack4.top = 0; stack5.top = 0;

// add an element
int push (stack1, int x) {
    if (stack1.top == MAX) return 0; // stack is full, operation failed
    stack1.elements[stack1.top++] = x;
    return 1; // operation succeeded
}

// take an element
int pop (stack2) {
    int x;
    if (stack2.top == 0) return -1; // stack is empty, operation failed (return -1, or any other negative number,
                                      // or 0, if stack contains natural numbers)
    x = stack2.elements[--stack2.top];
    return x;
}

// read the top, without removing the element
int read(stack3) {
    int x;
    if (stack3.top == 0) return -1; // stack is empty, operation failed
    x = stack3.elements[stack3.top];
    return x;
}

// is stack empty
int empty(stack4) {
    if (stack3.top == 0) return 1;
    else     return 0;
}

// is stack full
int full(stack5) {
    if (stack4.top == MAX) return 1;
    else     return 0;
}


queue:
Code: Select all
#define MAX 100

int queue[MAX];
int top = 0; // here elements exit queue
int bottom = 0; // here elements enter queue
int N = 0; // number of elements

// insert an element
int insert(int x) {
    if (N == MAX) return 0; // queue is full, operation failed
    bottom = (bottom + 1) % MAX; // set bottom to next empty element
    queue[bottom] = x;
    N++;
    return 1; // operation succeeded
}

// take an element
int take() {
    int x;
    if (N == 0) return -1; // queue is empty, operation failed
    top = (top + 1) % MAX;
    x = queue[top];
    N--;
    return x;
}

// is queue empty
int empty() {
    if (N == 0) return 1;
    else     return 0;
}

// is queue full
int full() {
    if (N == MAX) return 1;
    else     return 0;
}

You can also make these functions universal, to work with all queues of this format.

Re: need help in C++, stack, queue and Array in game

PostPosted: Sat Apr 18, 2015 7:03 am
by Masky94
Thanks for the help, i will look into it and try to implement them into the game

Re: need help in C++, stack, queue and Array in game

PostPosted: Sun May 03, 2015 2:28 am
by bat78
A common standard is 0 to indicate success and 1 or above - an error (i.e the function returns either the number of errors or a specific error code)

Re: need help in C++, stack, queue and Array in game

PostPosted: Sun May 03, 2015 5:44 am
by koala
bat78 wrote:A common standard is 0 to indicate success and 1 or above - an error (i.e the function returns either the number of errors or a specific error code)
Yes, bat78 is right. +1