Algorithms and programming
1.1 stack
The other function of the stack is to ensure logical organization of the variables for sequential retrieval. This function is very important since it allows the programmer to arrange how the instructions will be executed in the program to achieve the expected results. The process of retrieval is enabled by a register in the microprocessor of the machine called stack pointer.
The third function of the stack is to maintain retrieval order of the local variables and parameters. This ensures that the program does not take long to execute since the required parameters are found in one memory section and the retrieval is maintained is a certain order.
Looking at a stack in java, it is mostly used to store local variables and functions parameters. In this language, the stack helps in object oriented programming, which is one of the most powerful features of this language. In this language, it operates in first in – last out basis and operates in push and pop methods. It is contained in the class java.util and allows linked lists.
Algorithm
a. Declare the stack class and any links it might have.
b. To input data into the stack, use the push operation.
c. To output the data from the stack, use the pop operation.
d. Throw any exceptions to ensure that the program does not crash due to undefined data or lack of the required data for retrieval.
e. Ensure to return a zero after program execution to ensure it ends successfully.
For example:
Public class StackInheritance extends Lists;
Public StackInheritance()
{
Super (“stack”);
}
Public void push(Object, object)
{
InsertAtFront (object);
}
Public void pop(Object, object) throws EmptyListException
{
Return removeFromFront();
1.2 Queue functions
Moving on to queue functions, these functions help in delaying and ensuring that the sequence of program execution are followed. This function is simply the opposite of stack in modes of operation. This means that the functions works on first come first served basis. In this mode of operation, the first data to be queued is the first to be manipulated. This data structure is very important when it come to data manipulation that follows a certain procedure of manipulation and is strict of the execution sequence. A good example of the use of this data structure is in most computer system. Most computer have one microprocessor thus for the microprocessor to accomplish all the tasks, a certain order must be adhered to. This ensures that the microprocessor is not malfunctioned in the process of operation. Another example is in shared printing resources. The printer has to queue the printing jobs and print according to their arrival. In this part, we are to look at the structure of a class queue in java programming language for a better understanding.
Algorithm
a. Declare the class and any lists that it will be linked to.
b. To create a queue, use the queueList function which is already present in java.util package.
c. To insert any item, use enqueue statement while the queue function is still active.
d. The statement insertatback is used to add items into the queue.
e. To remove an item from the queue, use the dequeue statement. Remember to include exception handling to catch any data out of the declared range.
f. Return a zero to the main program after executing the function so as to end the execution successfully.
Example
Public class Queue
{
Private List queueList;
Public Queue ()
{
queueList = new List(“queue”);
}
//To add and remove objects in the queue,
Public void enqueue (Object, object)
{
QueueList.InsertAtBack (object);
}
Public Object dequeue () throws EmptyListException
{
Return QueueList.removeFromFront ();
Looking at the queue algorithm and simple class, it is clear that any object inserted first is still the first to be processed. This is opposed to the mode of operation of the Stack data structure making the two to be objects of concern to java language programmers.
2.1 Recursion.
In most programs, a section of a program might require to be repeated a number of times so as to compute and display the expected results. This is mostly in mathematical concepts and since computer programs are written from a mathematical perspective, the idea of recursion is very important. Recursion is mostly employed when a method or function requires to be executed a number of times while the main is still active. This means that the method or function calling the recursive part of the program (the method or function might as well call itself) must remain active during the whole process of recursion. A good example of this application is the factorial mathematics employed in binomial expansions. The original calling function or method must remain active all through the recursive period of the called subroutine or method or even function.
Compared to iteration, the two operates on the same principal of control statement. The only difference is that iteration relies of repetitive control statements while recursion relies on selection control statements. This translates in a better handling capability in recursion compared to iteration. However, recursion comes at a cost of being more complex in structure compared to iteration.
One of the most famous illustrations of recursion in programming is towers of Hanoi. In this classical problem, the priests in the temple were to move golden caskets which were arranged from the largest at the base to the smallest at the top. The constraints were only one disk would be moved at a time and no larger disk was to be placed on a smaller disk. In solving the problem, the priests had to use three pegs where one was for temporary storage. The idea was to move the smallest to one peg, the next to the other peg. After that, move the smaller disk to be on top of the second to remove disk. This continued with the interchange of disk pegs until the base disk was moved to a location of permanent storage. In simple words:
1. Move n – 1 disks from peg 1 to peg 2, using peg 3 as a temporary holding area.
2. Move the last disk (the largest) from peg 1 to peg 3.
3. Move n – 1 disks from peg 2 to peg 3, using peg 1 as a temporary holding area.
2.2 linked lists
Turning our focus to linked lists, this part of java language offers reference of a list’s property to another part of the program be it another list or stack or even a queue. From a programmer’s perspective, a linked list is a collection of self referential objects called nodes connected by reference links. The lists must be linked to other program parts for them to have enough information and enable their execution. They are mostly used in arrays and become full only when the dynamic memory allocation is full. This is mostly applied in java arrays which might require variable memory allocation sizes. The following diagram shows a graphical representation of linked lists.
The algorithm for linked lists is:
1. Declare the class of the linked lists.
2. Open a new link in the declared class
3. Add a new object to the linked list as a new node.
4. To link to the next list, create a node that is common in both lists and use it as a connection element.
5. Remember to return zero after program execution to ensure that the program is robust. Exceptions should also be added to increase robustness.