(ARTICLE) Stacks and Queues

(ARTICLE) Stacks and Queues 

Stack is abstract data type where elements inserted into stack using push model until entire stack is full. Elements/values can be removed using pop operation until entire stack is empty. This is a data type where last inserted elements are removed first. Stacks can be implemented in multiple ways, since stacks are considered abstract there is no implementation of stack data type in C or C++ (java does have a stack data type). There are many applications of this stack in day to day life. All the operations which do not have conditions like time frame or costs are executed using stack data structure.

Toy Problem for Stacks: Let us implement stacks in C. We need to create user defined data type called stack. Also we need to create operations involving around this stack. We might as well create stacks using arrays but how do we remove a memory location? So this task implementing stacks using Linked Lists (Structure). Create linked list structure. Let us call the linked list as stack which can be represented with following structure:

Create functions called push and pop. Notice when we insert an element into stack (top element) we are basically inserting values in first node. When we pop an element from stack we are removing element in the first node. Please be careful about memory allocation and handling the first node. Task on Stack Data Structure: Write a C program to calculate total sales and report transactions done by a coke vending machine. Soft drink (coke) tins are added into the vending machine one on top of the other. Each tin contains batch number, tin number, price for each tin. Money is accumulated when users buy the coke tins. Usually the tins on top are dispensed. Machine would no longer accept currency when stack is empty. 

typedef struct StackDataStructure

{

 int value;

 struct StackDataStructure

 *nextStackElement;

 }

Stack;

1. Print out price, batch number and tin number when a can is popped out of machine.
2. Push method would add values of batch and tin number to the stack.
3. Calculate total sales.
[READ MORE..]

Google