Stack

Stack
A stack is a data structure that is a lot like a linked list but only the top value of the list is kept track of. Each element of the stack keeps track of the previous element and when you add elements to the stack you simply add it to the top and it becomes the new top element. There are three important methods used in creating a stack, isEmpty, push, and pop. The isEmpty method simply checks to see if there is anything left on the stack. The push method adds a new element to the stack. The pop method removes the top-most element from the stack. A stack in Java is easily implemented with the Stack class or any implementing class of it's interface. In C++ however its best to make your own.

code format="cpp"
 * 1) include
 * 2) include

struct node{ char *info; node *next; node(char *i){ info = i;   } };

node *top;

void push(node *n){ if(top == NULL){ top = n;       return; }   n->next = top; top = n; }

node *pop{ if(top == NULL) return NULL; node *tmp; tmp = top; top = top->next; return tmp; }

bool isEmpty{ return (top == NULL); }

int main{ top = NULL;

push(new node("tyler")); push(new node("matt")); printf("%s\n", pop->info); push(new node("chris")); push(new node("richard")); while(!isEmpty){ printf("%s\n", pop->info); }

system("pause"); return 0; } code code Output: matt richard chris tyler code