Thursday, April 15, 2021

Insertion at the beginning of a Linked List


It was all going well until the pointers came in!
Has this statement ever crossed your mind? 

But you should know that the pointer is an important concept to know. A pointer is used to implement a lot many things in C++. Linked List is one of them. Linked List uses pointers for its operation. 

Today we will be looking at how to insert a node at the beginning of a Linked List. 

In the picture below you can see, we want to insert node d at the beginning of the list given. To insert it at the beginning, we followed three steps,

1) create a new node i.e. temp

2) the next of temp should be head now

3) At last, head = temp

Now, the new node temp is inserted at the beginning.

Lets look at the code!

Below is the function for inserting at the beginning. It will take an integer argument which will contain the value to be inserted in the node that is to be inserted at the beginning.

void front(int n)

In the function, at the very first, we will create a new node by the code given below. I will create a new node named temp.

node *temp=new node; 

Now we will assign the data n to the new node temp.

temp->data=n;

After assigning the data to the node now the next to temp should have the address of the current head of the linked list before which we want to insert the new node temp. We will do it by assigning temp->next = head,

temp->next=head;

Now that our node temp is holding the address of its next node, we will assign temp to head. Since now, the temp is the first node of our linked list.

head=temp;

So here we are done with inserting the node temp at the beginning of the linked list. 

So the entire function looks like, 

    void front(int n)
{
node *temp=new node;
temp->data=n;
temp->next=head;
head=temp;
display(head);
}

In the above code, display(head) is another function which is used to display the entire linked list. So lets take a brief look at display function.

Below give is the entire display function. This function takes in pointer as an argument. This pointer will bring the address of the node to be printed.Recursion is used hear where we again pass h->next to the function display until the value of the pointer is equal to NULL.


    void display(node* h)
{
if(h==NULL)
{
cout<<"\tNULL";
}
else
{
cout<<"\t"<<h->data;
display(h->next);
}
}


Now we will look at the entire program to insert a node at the beginning.


#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
}*head,*tail;
class linked_list
{
public:
linked_list()
{
head=NULL;
tail=NULL;
}
void front(int n)
{
node *temp=new node;
temp->data=n;
temp->next=head;
head=temp;
display(head);
}
void display(node* h)
{
if(h==NULL)
{
cout<<"\tNULL";
}
else
{
cout<<"\t"<<h->data;
display(h->next);
}
}
};
int main()
{
linked_list a;
int info,beg;
cout<<"\nEnter a value for the new node....";
cin>>info;
a.front(info);

cout<<"\nEnter the element you want to insert in the beginning : ";
cin>>beg;
a.front(beg);
}

In the above program we have the following,

1) structure node

2) class linked_list

3) a constructor linked_list() inside the class linked_list to initialise the value of head and tail to NULL at the very beginning

4) a function front() to insert at the beginning

5) a function display() to display the linked list



Wednesday, April 14, 2021

Tips to get more work done | Live a Quality Day

Procrastination might be the word that would have brought you here. We all exhibit procrastination but for different reasons. When we go to bed every night, not everyone sleeps happily and satisfactorily. Nowadays, a day in life is full of physical, mental, or emotional strain. It gets difficult to control our minds well in today's time.


So here I will be sharing few tips from my experience and a bit of research. If practiced daily, it can bring magnificent changes in one's life. 


Wake Up early

 

Even if you are a late-night owl, you should give waking up early a chance. If by sleeping late you can manage your time well, then it is good. But if you are unable to head your time right, then it's time to try this out. By doing this, efficiency increases vastly. One is in a highly energized state in the morning, and it's the right time to feed your mind well. You are just a try away!


Meditate

 

It might seem irrelevant, but it's certainly not. Meditation is a thing a person needs the most today. It is this thing that is neglected by most of us these days. Lack of proper awareness leads the way to not meditating despite knowing about the peace it provides. It provides immense benefits be it physical or mental. Just start meditating. Start every day with at least 15 minutes of mindful meditation. In the long run, this habit will prove to be worth.


To-do list for the day (15 minutes )

 

Prepare the list in the order of the most important things first. Doing hard work comes second but doing the right thing at the right time is very important. The actions you do will add in themselves a hundred times the value it has if you do it at the right time.

Write down tasks to be done in a diary. Noting down the list in a physical book would be helpful. You can also use Trello or stickies or some other online apps or websites for the same.


Start with Exercise

 

If you are new to this, you can start off by doing simple yogasanas, and then you can move to other sort of exercises later on. Start practicing at least 20 minutes to 30 minutes in the morning. Doing exercise will energize you for next 24 hours, it will increase your alertness, focus on things and make you active. 

 

Give yourself time to start.

 

You don't need to keep yourself busy all day long but you can give yourself some time to start. By doing this, you can begin well, and you are more likely to complete the tasks you start this way.


Re-set your workplace

 

You need to re-set your workplace and clean it before you start working.

Re-set in a sense, keep things that you will need for the day easily accessible. Things that are not in need should not be on the working desk. Keep things organized. If we look at it other way, even if we might neglect small things that actually counts then ten things kept unorganized, and ten organized makes a huge difference.


Start

 

It's time to start doing the work according to the plan. You don't need to stick to the schedule. Be flexible but make sure you do things. 

Do what is most important first and set deadlines to ensure you don't give a lot of time to a single task of course depending on what the task in.


Do one thing at a time.

 

Here's the golden rule. Whatever the task you are doing, be it of your interest or not, make sure you do it with full of your attention. You never know what helps you when. If you are anyway spending time on something, then make sure you learn it wholly. For ensuring that, it would be good to do one thing at a time. Of course, you can multitask, but it depends on what type of task you are doing.


Tick the tasks done

 

This one is for motivation. By doing this, you will come by a feeling of satisfaction. You will be motivated to do more tasks and complete them.


Give yourself a treat.

 

When you are done with more than half the tasks, give yourself a small treat. The treat could be anything ranging from eating your favorite food to going out for a walk or rest or anything.


Introspect

 

At the end of the day, after you complete all your tasks or maximum tasks of the day, take out some time for yourself and try to introspect. Introspection provides the knowledge of self one becomes self-aware. You become aware of your capabilities, weaknesses, strengths, and many more qualities which will never come before you in conventional life. 


Great if you could prearrange your next day's schedule 

 

 It's a plus thing, great if you could do it, it will prove to be good.


Sleep at a chosen time daily

 

It is okay to sleep at a different times each day, but the variation should not be in hours. It might disturb your biological cycle as well as your mental health. Occasionally it is okay to stay up late at night at random days, but it should not become your daily habit as it might adversely affect you in the long run.

You should know sleep deprivation is also a thing. You might not feel it on the spot, or you may but it is in any way not a good thing.


Consistency is the key.

 

Only if consistency exists, the things mentioned above will be effective. To be consistent is a hard thing, and not everyone can do it. So start small and lead the way through. 


Conclusion

 

Whatever new habit you build up, make sure don't get very strict on you. Be flexible and let it slowly become a part of your life, and don't rush. All that the change needs from you to happen is your determination and consistent action.


In the process, you mustn't just pass the time, all the same, spend and enjoy the time well because all you will go through or will have is present. And remember, it is only in the present you will prepare for the future, and it is only in the present will you live the future.

 

Make sure you live a quality day and a quality life!!!




 

Thursday, December 10, 2020

Binary Search Algorithm

 

Binary search is basically a searching algorithm used to search for a particular element in any given array. There are in fact many searching algorithms available, but what matters more is the efficiency with which an algorithm searches for an element in any given array because we would never want our algorithm to do less work in more time. In other words we can say that time complexity and space complexity tells us all about how efficient an algorithm.

 

Lets get started...

 

First of all lets look at what Binary Search does!

 

Process to search..

 

To search an element in a sorted array, here lets assume array is sorted in ascending order.

 

1. We find the middlemost element of an array lets say a[i] (Here 'a' is an array and 'i' refers to index number).

 

2.  Check whether a[i] is greater then or less than the element to be searched for.

 

    a. If a[i] == val, then we just found out the value....


    b. Else if a[i] > val, then discard the remaining array after the middle element.

 

    c. Else if a[i] < val, then discard the array beforehand the middle element.

 

 

Fig (a)
 

 

Above two points can be considered as the main logic in Binary Search. Now we are good start.

 

Now lets see how it actually works.

 

Starting with our function, we will create a function named Binary Search and declare the variables needed.

 

                int BinarySearch( int n, int a[], int val)   

        

Here, n = number of array elements

          a[] =  given array

          val = value to be searched for in the array

 

Now we will write the body of the function of BinarySearch ().

 

int first=0,mid,last=(n-1),val;

 

 Here we declared two variables named 'first' and 'last' and then we stored the first and the last index value of the array 'a[]' respectively in those variables.

 

How to find the middle element?

 

We will use the formula mid = first + (last-first)/2  
 
  mid=first+(last-first)/2;
 
 
This is because the above formula proves to be more reliable the the formula
mid=(first+last)/2

                           
Now to find the value in the array a[], we will look for three conditions on the basis of the value of middle element
  
//One
    if(val==a[mid])
{
return mid;
}

 

In 'one' we get our middle element.

 

//Two 
    else if(val>a[mid])
{
first=mid+1;
}


In 'two' if value is greater than middle element, we eliminate the leftmost array from the middle element along with middle element.

In fig (a), the array elements 1, 2, 3 and 4 will be eliminated and now on wards our new array elements will be 5, 6, 7 with indices 4, 5, 6.


//Three 
    else
{
last=mid-1;
}



In 'three' if value is less than the middle element, we will eliminate the rightmost array from the middle element along with middle element.

In fig (a), the array elements 4, 5, 6 and 7 will be eliminated and now on wards our new array elements will be 1, 2, 3 with indices 0, 1, 2.

 

Here our while loop gets completed.

 

  while(first<=last)
{
mid=first+(last-first)/2;
if(val==a[mid])
{
return mid;
}
else if(val>a[mid])
{
first=mid+1;
}
else
{
last=mid-1;
}
}

 

Now we will basically return -1 if any of the conditions in the above loop does not come out to be true.

 

Here's the complete function.


int BinarySearch(int a[], int n, int val)
{
int first=0,mid,last=(n-1),val;
while(first<=last)
{
mid=first+(last-first)/2;
if(val==a[mid])
{
return mid;
}
else if(val>a[mid])
{
first=mid+1;
}
else
{
last=mid-1;
}
}
return -1;
}




Thursday, June 18, 2020

An Inside look at QUANTUM COMPUTING


Listening the word 'Quantum Computers' these days makes us think of a substantial significance that it has or will have in the course of time, isn't it?

So lets talk!

 




Definitely I will not start with the definition. But instead, let's go get a glimpse of it through an example. Let's say I have a situation where I have a baby elephant and it wants to meet its mother who is far away. He has around 100 ways to reach its home, now it definitely would want to choose a path that is the shortest one.

 

So let's try to find out the shortest path with our traditional classical computer, what it will do is it will check each path by taking considering one at a time and then finally find the shortest path after a hundred counts. So if it takes one second to check one path, it would take a hundred seconds to find out the shortest path.

 

Now when we try out the same problem with the quantum computer, it will consider all the paths at the same time and find out the shortest one. Hence in just one second, we'll get the results for the shortest path.

 

And this is why we will need quantum computers.

 

Now let's take another example. You should know that there exist some propositions or we can say some mathematical statements that are yet to be proved. Statements like, what values of x and y will prove the statement 2999x+1001y+59=52 to be true. For that, we'll have to try it out with each digit and with all its combinations and find the right one out. But there are some proofs that have the values of x's and y's which goes to hundred's and even thousand's of digits and manually it will definitely get difficult but after a certain level, even classical computer will find it difficult to process it.

 

Whereas with quantum computers, this task is believed to be achieved in minutes or in fact seconds depending on what the problem is! So, now it's certain that this is just amazing!

 

Now let's get the sight of how quantum computers differ from that of a classical computer.

 

Basically a bit is a basic unit of information in classical computers, whereas quantum information is stored in a qubit in quantum computers. A bit has the values either 0 or 1, whereas, qubits has the values 1 or 0 or both.

 

Yes, you read it right, it's both. So how can it be both?

 

Let's see.

When a coin is spinning, the outcome of whether it's heads or tails is unknown and the outcome is only known when it stops spinning. So while it is spinning, it is said to be in a superposition state of heads and tails. This is a similar case with a qubit. A qubit is in a superposition state of 0 and 1. Superposition is a thing that makes quantum computers very effective.

 

On the other side, bits in classical computer follows no principles such as superposition or anything similar, they are simple and in their way, they follow Newtonian Mechanics i.e, for example, things that follow Laws of Motion. Whereas quantum mechanics is for particles that are very small that they no longer behave normal, they get weird at a very small scale. Hence there was a need to study and get an idea of how they behave and conclude some sort of general rules and principles. As a result, we got QUANTUM MECHANICS.

 

So to bring things together,

 

What are all the basic principles used behind quantum computing?

 

Quantum computers make use of quantum mechanical phenomena such as Superposition and Entanglement.

 

We saw a glimpse of Superposition, now let's see what is Entanglement.

 

Again let's see it through an example.

 

So, let's say you have two boxes with a left and a right shoe. Now you don't know which box has what and let's say one of the boxes is on the earth the other one on the mars. Now let's talk about how one object affect the other and about Einstein's Spooky action at a distance. So when we open one box on the earth and find out that its the right shoe, within a second we can say confidently that the box on mars has the left shoe. We just saw information traveling with an unimaginably fast speed from earth to mars because of the shoes which were entangled i.e. linked together irrespective of what distance they are separated, but information traveling will take the same time anyways.

 

So, qubits basically follow the principle of Quantum mechanics called 'Entanglement'.

 

Quantum computers are today available virtually. A lot of companies like IBM, Google, Intel, Microsoft, etc are leading the way in quantum computing. In fact, many companies have made available these incredible quantum computers. For ex: One can access the IBM quantum computer.

 

Thanks for coming here.

 

 

Wednesday, May 8, 2019

Photocopier


The Copying Machine


Chester Carlson was the inventor of photocopying.
Copying machines works on the electrostatic process called Xerography. Photocopiers are used in businesses, studying, in homes etc. Its purpose is to reproduce documents, pictures and other items.

Have you ever wondered about how the photocopier machines, which are so convenient to use,works?

If not, let us have a glimpse at the working of the machine.


 The copying machine works on the basic principles of attraction of positive and negative charges. Here we will see four steps of its working,
 

1]Charging


The machine has in it attached a cylindrical drum called photoreceptor which is light sensitive. In the first place, the photo receptor is charged negatively using a wire with high voltage. The photo receptor is coated with photo conductive material called Selenium. Selenium is a material which behaves as an insulator in dark and as a conductor when exposed to light.

Cadmium sulphide, zinc oxide and organic polymers are also used.


2] Exposure


A bright light is illuminated on the original document which is to be copied. The white surface of this document reflects the white light on the photo receptor. The area of the photo receptor which is exposed to white light from the white paper of the original document is charged positively and the left out dark portion of the photo receptor is charged negatively. This dark portion corresponds to the black portion of the original document which was reflected.

3] Developing


The powder which is used to create image is called toner. The toner ranges about five to ten micrometers in diameter. To create the image on the photo receptor, the toner is transported on it. Now this toner is positively charged so it sticks to the negatively charged portion of the photo receptor which is not exposed to light.
This way the pattern is created on the photo receptor.



 

4]Transferring


Now to create the copy of the image on the paper, the photo receptor with the toner which is sticked to it is brought in contact with the paper. The paper is highly negatively charged in order to subdue the adhesive forces of the powder.


5] Fusing


In transferring, the paper was patterned with the powder so there are chances of some particles of toner to come off the paper. So, in order to prevent this, the paper is passed through a pair of heated rollers by applying high pressure so that it melts the toner.

In this way, toner is permanently bonded to the paper and the copying is completed.


Advantages & Disadvantages


The copies are of good quality and are exact copies of the original document. Nowadays, the photocopiers have become light weight and easier to handle. Buying one copier, you can work in-house. You can go green, as you can also use recycled papers. Recycling of waste toner is also practiced by most manufacturers.

However using photocopier has certain disadvantages too.
There are things that has to be replaced regularly , like the ink, paper and thermal film are some examples.

Health hazards


Photocopiers produce small amounts of ozone gas too. This odorous gas is irritating to eyes and is harmful to the throat and lungs. The main ingredient in the toner is carbon black which of course is not a good thing.

Taking precautions while using a photocopier machine like choosing a low emission power photocopiers, keep your distance, replace your photocopier if it gets too old, maintain your machine well etc can help you keep your health quite well.


 


 


Time

  A brief look at time......  

Popular Posts