Splitting a large file into separate modules in C / C ++, Java and Python

This approach is doomed to failure and usually requires rewriting from scratch.

So, to solve this scenario, we can try to divide the problem into several sub-problems and then try to solve it one by one.

This not only makes our task easier, but also allows us to achieve

// insert a complete linked list

 
# include & lt; stdio.h & gt;
# include & lt; stdlib.h & gt;

 
// ------------ --------------------
// Announcements - START:
// --------------------------------

 

struct Node;

struct Node * create_node ( int data);

void b_insert ( struct Node ** head, int data);

void n_insert ( struct Node ** head, int data, int pos);

void e_insert ( struct Node ** head, int data);

void display ( struct Node * temp);

 
// ------------ --------------------
// Announcements - END:
// --------------------------------

 

int main ()

{

struct Node * head = NULL;

 

int ch, data, pos;

 

printf ( " Linked List: " );

while (1) {

printf ( "1.Insert at Beginning" );

printf ( "2.Insert at Nth Position" );

printf ( "3.Insert At Ending" );

printf ( "4.Display" );

printf ( "0.Exit" );

printf ( "Enter your choice:" );

scanf ( "% d" , & amp; ch);

 

switch (ch) {

  case 1:

  printf ( "Enter the data:" );

scanf ( "% d" , & amp; data);

b_insert (& amp; head, data);

break ;

 

case 2:

  printf ( "Enter the data:" );

scanf ( "% d" , & amp; data);

 

printf ( " Enter the Position: " );

scanf ( "% d" , & amp; pos);

n_insert (& amp; head, data, pos);

break ;

 

case 3:

  printf ( "Enter the data:" );

scanf ( "% d" , & amp; data);

e_insert (& amp; head, data);

break ;

 

case 4:

  display (head);

break ;

 

case 0:

  return 0;

 

default :

  printf ( "Wrong Choice" );

}

}

}

 
// -------------------------- ------
// Definitions - START:
// ---- ----------------------------

 

struct Node {

int data;

struct Node * next;

};

 

struct Node * create_node ( int data)

{

struct Node * temp

= ( struct Node *)

malloc ( sizeof ( struct Node));

temp- & gt; data = data;

temp- & gt; next = NULL;

 

return temp;

}

 

void b_insert ( struct Node ** head, int data)

{

  struct Node * new_node = create_node (data);

 

new_node- & gt; next = * head;

* head = new_node;

}

 

void n_insert ( struct Node ** head, int data, int pos)

{

if (* head == NULL) {

b_insert (head, data);

return ;

}

 

  struct Node * new_node = create_node (data);

 

struct Node * temp = * head;

 

for ( int i = 0; i & lt; pos - 2; ++ i)

temp = temp- & gt; next;

 

new_node- & gt; next = temp- & gt; next;

temp- & gt; next = new_node;

}

 

void e_insert ( struct Node ** head, int data)

{

  if (* head == NULL) {

b_insert (head, data);

return ;

}

 

  struct Node * temp = * head;

 

while (temp- & gt; next! = NULL)

  temp = temp- & gt; next;

 

struct Node * new_node = create_node (data);

temp- & gt; next = new_node;

}

 

void display ( struct Node * temp)

{

printf ( "The elements are:" );

while (temp! = NULL) {

printf ( "% d" , temp- & gt; data);

temp = temp- & gt; next;

}

printf ( " " );

}

 
// --------------------------------
// Definitions - END
// --------------------- -----------

Compiling the code: we can compile the above program:

 gcc linkedlist.c -o linkedlist 

And it works!

The main problems in the above code:
We can already see the main problems with the program, it is not so easy to work with the code either individually or in a group .

If someone wants to work with the above program, then some of the many problems that person is facing:

  1. One has to go through the Full Source File. to improve or improve some functionality.
  2. Cannot be easily reused the program as a basis for other projects.
  3. The code is very cluttered and is not attractive at all, which makes it very difficult to navigate through the code.

In the case of a group project or large programs, the above approach is guaranteed to increase overall costs, energy, and failure rates.

Correct approach:

We see that these lines begin in every C / C ++ program that starts with "#include".
This means to include all functions declared in the "library" header (.h files) and defined possibly in library.c / cpp files.

These lines are preprocessed at compile time.

We can manually try to create such a library for our own purposes.

Important things to remember:

  1. The ".h" files contain only prototype declarations (such as functions, structures) and global variables.
  2. The ".c / .cpp" files contain the actual implementation ( declaration definitions in header files)
  3. When compiling all source files, make sure that multiple definitions of the same function, variable, etc. do not exist for the same project. (VERY IMPORTANT)
  4. Use static functions, to restrict yourself to the file in which they are declared.
  5. Use the extern keyword to use variables referenced by and external files.
  6. When using C ++, be careful with namespaces, always use namespace_name :: function (), to avoid collisions.
  7. By splitting the program into smaller codes:
    After examining the above program, we can see how this large program can be split into suitable small parts and then easily processed.

    The above program has essentially 2 main functions:
    1) Create, insert and save data in nodes.
    2) Displaying nodes

    This way I can split the program accordingly so that:
    1) The main file is & gt; Program driver, Nice Wrapper from Insertion Modules and where we use additional files.
    2) Paste - & gt; The real implementation lies here.

    With the important points mentioned, the program is divided into:

    linkedlist.c - & gt; Contains Driver Program
    insert.c - & gt; Contains Code for insertion

    linkedlist.h - & gt; Contains the necessary Node declarations
    insert.h - & gt; Contains the necessary Node Insertion Declarations

    In each header file we start with:

     #ifndef FILENAME_H #define FILENAME_H Declarations ... #endif 

    The reason we write our declarations between #ifndef, #define and #endif, is to prevent multiple declarations of identifiers such as data types, variables, etc., when one and the same header file is called in a new file belonging to the same project.

    For this sample program:

    insert.h - & gt; Contains the declaration of the insert node as well as the declaration of the node itself.

    It is very important to remember that the compiler can see declarations in the header file, but if you try to write code that includes the declaration elsewhere, this will result in an error, as the compiler compiles each .c file individually before moving on to the link step. ,

    connectedlist.h - & gt; A helper file that contains Node and its Display declarations that must be included for files that use them.

    insert.c - & gt; Include a Node declaration via #include "connectedlist.h", which contains the declaration as well as all other method definitions declared in insert.h.

    connectedlist.c - & gt; Simple Wrapper, containing an infinite loop prompting the user to insert integer data at the required positions, and also containing a method that displays the list.

    And the last thing to keep in mind is that meaningless inclusion files into each other can lead to multiple overrides (s) and will result in an error.

    Taking the above into account, you should carefully divide into suitable routines.

    linkedlist.h

    // connectedlist.h

     
    # ifndef LINKED_LIST_H
    # define LINKED_LIST_H

     

    struct Node {

    int data;

    struct Node * next;

    };

     

    void display ( struct Node * temp);

     
    # endif

    insert.h

    // insert.h

     
    # ifndef INSERT_H
    #define INSERT_H

     

    struct Node;

    struct Node * create_node ( int data);

    void b_insert ( struct Node ** head, int data);

    void n_insert ( struct Node ** head, int data, int pos);

    void e_insert ( struct Node ** head, int data);

     
    # endif

    insert.c

    // insert.c

     
    # include "linkedlist.h"
    // & quot; & quot; so the preprocessor looks
    // to the current directory and
    // standard library files later.

     
    # include & lt; stdlib.h & gt;

     

    struct Node * create_node ( int data)

    {

    struct Node * temp = ( struct Node *) malloc ( sizeof ( struct Node));

    temp- & gt; data = data;

    temp- & gt; next = NULL;

     

    return temp;

    }

     

    void b_insert ( struct Node ** head, int data)

    {

      struct Node * new_node = create_node (data);

     

    new_node- & gt; next = * head;

    * head = new_node;

    }

     

    void n_insert ( struct Node ** head, int data, int pos)

    {

    if (* head == NULL) {

    b_insert (head, data);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       return ;

    }

     

      struct Node * new_node = create_node (data);

     

    struct Node * temp = * head;

     

    for ( int i = 0; i & lt; pos - 2; ++ i)

    temp = temp- & gt; next;

     

    new_node- & gt; next = temp- & gt; next;

    temp- & gt; next = new_node;

    <