5.1.10+Explain+the+use+of+arrays+as+static+stacks+and+queues

Arrays are used in queues and stacks as a way of saving the data. Arrays are random access so you can fetch an object directly but arrays have are static data structures so you must first create an array with a specified amount of 'places'.

Stacks


In order to be able to input new data and know which one to process you must maintain a variable that will be the equal to the index number of the last item input in the array, let's call this variable 'top'. To signify an empty array you should initialize the int top as -1.

With int top it becomes very easy to check for an empty stack or a full one, as well as add an object or data as well as popping off an item. E.g. int top=top++; array[top]=newObject; E.g. int top=top-1;
 * Empty stack**: does top=-1, if yes than stack is empty
 * Full stack**: does top equal the length of the array - 1 (remember the index number starts at zero so must deduct 1 from array length) if yes then stack is full.
 * Add object**: First you should check if stack is full, if it is not then carry on. All you have to do to add an object is simply increment int top and add the object to the array in position top.
 * Popping an item:** First check for an empty stack, if is not empty then carry on. All you have to do is reduce int top to the index of the item before the popped one.

=Queues=



Linear Queue
We must keep an integer to be able to tell where the tail of the queue is, let us call this integer 'tail', this will be equal to the index number of the last item in the queue. To signify an empty array you should initialize the int tail as -1.

With int tail it becomes very easy to check for an empty queue or full queue as well as enqueueing and dequeueing items. **Empty** **queue:** simply cheack if tail=-1. If so then queue is empty. **Full queue:** check if tail equals the array size. So tail=queue.length-1 **Enqueue:** Firstly check for full queue. If queue is not full carry on. Increment tail by 1 and add the new item in the index number or tail. E.g. tail=tail++; queue[tail]=newObject; E.g. for(int x=0;x=1;x--) { queue[x]=queue[x-1]; } queue[0]=move; return ' ';
 * Dequeue: **Firstly check for an empty queue. It queue is not empty then carry on. It becomes tricky to dequeue an item as you need to shuffle all the items forward after the item that has been processed. To do this you would need to run a for loop that increments a variable that is equal to the index number of the array place in front of the one you want to move forward. At the end initialize int tail.
 * Moving an item:** Firstly check for an empty queue, if queue is not empty then carry on. To do this you will need to run a for loop to go through the queue and searching for the item (if item is not found break from loop). Then you need to store this item, and shift everything in front of it back one place. And finally you add the item back to the front.

Circular Queue
A circular queue becomes a little more tricky as we need to account for both the tail of the queue and the head. It is also important to know that a circular queue is just an array that we conceptually think of as circular. To start both variables should be set to -1.

**Full queue:** Because the head may be further along the array than the tail our test for a full queue will be that if tail+1=head than the queue is full. You must also include if the tail is at the end of the array and the head at the beginning (an easy way to do this is if head==(tail - (queue.length-1)) ) **Enqueue:** Firstly check if queue is full. You must make sure that if head=-1 you make head=0. Since we will be adding to the end of the queue we will be using our int tail, but tail might be at the end of the array so you must make an if function. If tail is at the end of the array, make tail=0 and add the item at the index number tail. If tail is not at the end of the array simply increment tail by 1 and add the item at the index number tail. **Dequeue:** Firstly check for an empty queue. Set up an if function to see if the head is at the end of the array if it is then make head=0, else simply increment head by 1.
 * Empty queue:** To check for an empty queue see if head and tail =-1.

media type="youtube" key="g9su-lnW2Ks" height="315" width="420"

rss url="https://groups.diigo.com/group/comp_sci/rss/tag/5.1.10" link="true" number="10"