Tuesday 25 August 2015

Pointers

To understand the basic level of pointers we will be using C language. I'll be sharing some examples from HarvardX school of Computer Sciences & Stanford cs library.

The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, great power comes with great responsibility and this power is enough to screw things up spectacularly as simple errors can corrupt the state of your program in ways that make debugging difficult. The goal of these slides is to make sure that you understand exactly what you're doing when you use pointers in your code.
Here is an video example of  Binky clay telling about pointers.

FIRST OF ALL WHY DO WE USE POINTERS?

Yea one may ask what is the need of the pointers? Well when it comes to the concept of data structures using Linked List and Binary Search Trees. Pointers made it possible to implement them because without them there would be no Linked list or Binary Trees. Another reason of using pointers is we get access to our variables for sharing purpose quite easily, like without pointers we may have to work around to copy back and forth the data but because of pointers we don't need this.

USAGE

Next we are thinking how do we declare a pointer? See the syntax below
syntax: int* a;You have just declared an integer type pointer of variable a.



Referencing 

After declaration the very first thing you need to do is referencing your pointer in order to use it, Unfortunately compiler or langues won't help you in that thats the task you have to perform by your hands.
So next question raises in you mind must be how do we reference it? right..
You can do it like
syntax: a = malloc(sizeof(int)); 
Note:
it gives a new place in the memory to the variable a

You can make a reference for already declared variable as a = &variable as well because there maybe a chance if you try to refer it and there is lack of space in the memory. 


De-Referencing

You have successfully referenced your variable and you are ready to de-reference it. We can use the * operator to de-referencesyntax: a* = 40;Remember here you can de-reference only similar type otherwise it would be a run time error.
Code and data for your program are stored in random-access memory (RAM), which is basically a huge array of 1 byte (8 bits) blocks. Each block is associated with a hexadecimal number that represents its memory address.
Just as ints are variables that store integers and floats are variables that store floating-point values, a pointer is a variable that stores memory addresses. In the appliance (a 32-bit operating), a memory address is 4 bytes, so it makes sense that a pointer is also 4 bytes

Saturday 1 August 2015

Working of Stack (Explained)

Stack is the simplest Data Structure with the policy of LIFO (Last In First Out), remember stack of trays ? Stack of coins placed above on each? Or you may think about it when you are Undoing and Redoing something on computer, Yes it works according to stack as well. This was the pretty much theory you need to know for stack now lets get implement it in programming.
What Ingredients you need to have to make a perfect working stack ?
Here are the followings:

  • You need to PUSH any object in stack
  • You need to POP any object from stack
  • You may check the current POSITION 
  • For PUSH you need to know if it isn't FULL so you'll be making it as well
  • For POP you need to know if it isn't EMPTY.
  • Then the last you need to built a MENU for taking user input again and again
First make the functions is_Empty and is_Full so that Push and Pop functions can check the error and not get caught in exception.
So for making the function is_Full we need to define variables and stuff to get know how bigger is stack and at what condition stack will be called full. right?



We'll be using Class here which is named "Stack". So in the body we have declared top to locate the current position of stack , size to ask user how much bigger stack user wants. Then in constructor "Stack()" assigned the values to the pointer array for dynamic input in the program.


Here we go straight after making Constructor we defined prototypes of our functions we'll be defining outside of the class (Prototypes are just a promise to function that declare them here but we'll define them later in the program). Our functions will be returning the True or False (e.g if stack is empty ? Yes or No ?) except current_place( ) because it has to return the integer with the current place. 

We'll be defining these functions outside the class. For that you have to do it as " Type NameOfClass ScopeResolutionOperator FunctionName(your declared prototype) " in code picture you can see it as " bool Stack :: is_full( ) ".
Till now we got set our Stack program to ask user how much bigger stack should work for you. What'll be function you'll be defining in this program. Next we want to start defining our functions to work with.



Simply put "is_Empty( )" & "is_Full( )" inside the if condition to check whether there's any exception or not. While the function "Current_Place( )" will simply tell you what's in your stack and if your stack is empty or not. How easy is that?



Function for displaying the menu list for the user to select the option and perform their operations.