Aug 31, 2020

Linked Lists in JAVA | Java Language | Coding Winds

 

LINKED LIST IN JAVA

Hello guys we are back with another blog in java and today we are gonna talk about linked list in java. So we are acknowledged with arrays in java, and linked list are quite similar to arrays in java. They are also non primitive data types but unlike arrays they are not saved in a contiguous memory locations where each element of the linked list has the reference or address of the next element in the linked list. Basically actions such as insertion and deletion of elements are much easier to perform in linked list that’s why one can prefer them over arrays.

Now let’s first jump to the basic syntax of how to create a linked list object and then we will discuss about the method used to add values inside the linked list.

The basic syntax to create linked list object is written below.

Syntax:

LinkedList <Type> li = new LinkedList <>();

So in the above syntax we are creating a linked list object named ‘li’ which will be able to store values of whatever type we want.

add() METHOD :  The add method is used to add elements inside the linked list. Suppose we want to add an string element inside our linked list, whose object is named ‘li’, so the syntax we will be

li.add(“Hello”);

 

Now the add method can be also used to store the elements at an specific index. Suppose we want so put our string element at 0th index so the syntax will be written with the index at which the element is to be added, followed by that element after a comma.

 

Example:

li.add(0, “Hello”);

 

Below is an exemplary code giving complete idea of creating a linked list, adding elements to it and finally printing it out.

import java.util.*;

 

public class Linked_List {

 

     public static void main(String[] args) {

        

     LinkedList <String> li= new LinkedList<String>();

    

     li.add("Hello");

     li.add("Readers");

     li.add("Welcome!");

    

     System.out.println(li);

     }

}

 

//OUTPUT: [Hello, Readers, Welcome!]

 

 

Now below is the example of adding an element at an specific index.

import java.util.*;

 

public class Linked_List {

 

     public static void main(String[] args) {

         

     LinkedList <String> li= new LinkedList<String>();

    

     li.add(0,"Welcome!");

     li.add("Hello");

     li.add(1,"Readers");

    

    

     System.out.println(li);

     }

}

 

OUTPUT: [Welcome!, Readers, Hello]

That’s all for today guys, we would discuss more about the Linked list and it’s methods in java. Hope you guys liked it and do let us know your views in the comment section below.

Aug 29, 2020

File Handling in Python | PYTHON LANGUAGE | Coding Winds


Python File Handling

File handling is an important part of any web application. Generally, we divide files in two categories, text file and binary file. Text files are simple text whereas the binary files contain binary data which is only readable by computer.

 

File Opening

To open a file we use open() function. It requires two arguments, first the file path or file name, second which mode it should open.


There are four different methods (modes) for opening a file:

"r" - Read - Default value. Opens a file for reading, error if the file does not exist

"a" - Append - Opens a file for appending, creates the file if it does not exist

"w" - Write - Opens a file for writing, creates the file if it does not exist

 "x" - Create - Creates the specified file, returns an error if the file exists

 

As we know file should be handled as binary or text mode

"t" - Text - Default value. Text mode

"b" - Binary - Binary mode (e.g. images)

 

Example,


We can also neglect “r” and “t” as they are default values.

We can also specify the exact path of the file in as same.


File reading        


To read the whole file at once we use, read().


If you call read() again it will return empty string as it already read the whole file.

readline() can help you to read one line each time from the file.


To read all the lines in a list we use readlines() method.

 

Let us write a program which will take the file name as the input from the user and show the content of the file in the console.

As you see we used close().

Note: - All examples above, I haven’t mentioned about closing of a file, but its important in each and every case.

Always make sure you explicitly close each open file, once its job is done and you have no reason to keep it open. Because

·       There is an upper limit to the number of files a program can open. If you exceed that limit, there is no reliable way of recovery, so the program could crash.

·       Each open file consumes some main-memory for the data-structures associated with it, like file descriptor/handle or file locks etc. So you could essentially end-up wasting lots of memory if you have more files open that are not useful or usable.

·       Open files always stand a chance of corruption and data loss.

Note :- Using with statement will look over the file closing weight.


File writing

Let us open a file then we will write some random text into it by using the write() method. We can also pass the file object to the print function call, so that it writes in the file.