What is Collection?
The action or process of collection someone or something. A simplest way to use the collection is to use an array to hold the item.
What is an Array?
An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.
Array consist of Index and Element as shown Figure 1
Figure 1 Array Foo
For example, an array containing 5 integer values of type int called foo could be represented as:
int foo [5]={12,15,20,25,100};
Array foo has length five and has four indexes.
Initialization of an array
By default, regular arrays of local scope (for example, those declared within a function) are left uninitialized. This means that none of its elements are set to any particular value; their contents are undetermined at the point the array is declared.
But the elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial values in braces {}. For example:
int foo [5] = { 16, 2, 77, 40, 12071 };
This statement declares an array that can be represented like this:
Assessing and Assigning a value
The values of any of the elements in an array can be accessed just like the value of a regular variable of the same type. The syntax is:
name [index]
int FirstValueIs= foo [0]; // it has index 0
int SecondValueIs= foo [1]; it has index 1
In this way we can get each desired value of array.
Assigning a new value.
foo [2] = 75;
Access to an element of the array and assign a new value to it, foo [2] is accessed and new value 75 is assigned to it.
Some other valid operations with arrays:
1 | foo[0] = newValue; foo[newValue] = 75; beta = foo [alpha+2]; foo[foo[alpha]] = foo[2] + 5; |
**Try to play with your code produce some errors and remove those, every-day learn a new thing. Enjoy your code don’t make it boring. ** Note
For example:
1 | // arrays example #include <iostream> using namespace std;
int foo [] = {16, 2, 77, 40, 12071}; int n, result=0;
int main () { for ( n=0 ; n<5 ; ++n ) { result += foo[n]; } cout << result; return 0; }
///how to get the size of an array ///sizeof(foo)/sizeof(*foo) |
Multi dimension array
Multidimensional arrays can be described as "arrays of arrays". For example, a bi-dimensional array can be imagined as a two-dimensional table made of elements, all of them of a same uniform data type.
jimmy represents a bi-dimensional array of 3 per 5 elements of type int. The C++ syntax for this is:
int jimmy [3][5];
And for example, the way to reference the second element vertically and fourth horizontally in an expression would be: jimmy [1][3]
multidimensional array |
#define WIDTH 5 #define HEIGHT 3
int jimmy [HEIGHT][WIDTH]; int n, m;
int main () { for (n=0; n<HEIGHT; n++) for (m=0; m<WIDTH; m++) { jimmy[n][m]=(n+1)*(m+1); } } |
Stacks
Another way of storing data is in a stack. A stack is generally implemented with only two principle operations (Push data and Pop data).
Push: Add a new item in stack.
Pop: Extracts the most recently pushed item from the stack
Some other methods are:
top: Returns the item at the top without removing it
isempty: Determines whether the stack has anything in it.
A common model of a stack is a plate or coin stacker. Plates are "pushed" onto to the top and "popped" off the top. Stacks follows the Last-In-First-Out (LIFO) mechanism.
Link List
Linked List is a linear data structure and it is very common data structure which consists of group of nodes in a sequence which is divided in two parts. Each node consists of its own data and the address of the next node and forms a chain.
Link − each link of a linked list can store a data called an element.
Next − each link of a linked list contains a link to the next link called Next.
Linked List − A Linked List contains the connection link to the first link called First.
Node – Each node contains the piece of list data and the location of next node.
Head node – The first node in the link list is the head node.
Basic Representation of Link List:
Figure 2 Basic Representation
We can the further explanation graphically as well
Figure 3 Detailed explanation
Basic Operation
Following are the basic operations supported by a list.
Insertion − Adds an element at the beginning of the list.
Deletion − Deletes an element at the beginning of the list.
Display − Displays the complete list.
Search − Searches an element using the given key.
Delete − Deletes an element using the given key.
Insertion in Link List
Adding a new node in linked list is a more than one step activity. We shall learn this with diagrams here. First, create a node using the same structure and find the location where it has to be inserted.
1) Create new node
Figure 4 New node created
2) Check whether list is empty or not.
(head==null)
3) If it is Empty then
Set newNode next=null
head = newNode
4) If it is notEmpty then
newNode next=head
head = newNode
Figure 5 consider we have linked node
Imagine that we are inserting a node B (NewNode), between A (LeftNode) and C (RightNode). Then point B.next to C.
NewNode.next −> RightNode;
It should look like this.
Figure 6 adding a new node
Now, the next node at the left should point to the new node
LeftNode.next −> NewNode;
Figure 7 rearranging the nodes
This will put the new node in the middle of the two. The new list should look like this.
Figure 8 Final list after attachment
Deletion Operation
Deletion is also a more than one step process. We shall learn with pictorial representation. First, locate the target node to be removed, by using searching algorithms.
The left (previous) node of the target node now should point to the next node of the target node.
LeftNode.next −> TargetNode.next;
This will remove the link that was pointing to the target node. Now, using the following code, we will remove what the target node is pointing at.
TargetNode.next −> NULL;
We need to use the deleted node. We can keep that in memory otherwise we can simply deallocate memory and wipe off the target node completely.