Programming language C (and XV). Applications II

In this chapter we will define a set of general purpose functions that can be stored in a library and used from any application.

The set of functions that we define will form a new type of data for the handling of the tracks, and if an application wants to use them, it can do so through this new type of data as if it were functions of the standard library C. Let's start by defining the tracks and their functions.

A list will be defined by two demonstrators, one to indicate the first component and the other for the last. Each of the components in the list has a pointer to indicate the component next to the saliva, the component information index and priority, as it is possible to enter the components in priority order. Program 1 shows the contents of the lista_def.h file, where the list is made and the component is defined.

Program 1. Definitions lista_def.h.

The list functions that the applications will need will then be designed and defined.

From the design point of view, you must specify what type of functions they will be and what parameters they will have. To begin with, we will need the function of creating an empty list, which has as only parameter the created, the breeze index. To know if it is a simple list, we will have a boolean function with a single parameter, the empty function. As the lists add the components and delete them, the main functions will be to add them at the end (regardless of priority) or add them in order of preference, and delete the first component or remove the first component from a given priority. Associating, inserting, first and leaving these four functions will correspond to the following names and characteristics:

  • Being functions of joining and inserting, they will have two parameters, list and component (by demonstrators) and will not return value.
  • Since the first and the exit functions remove a component from the list, they will return the index of that component, showing both parameters as unique.
  • In the exit function you have to define another parameter on the saliva, that is, the priority, since for this you have to remove the first component of the smooth.
Program 2. Prototypes of functions.

The designed functions will be reflected in the lista_fun.h file, since in the applications this file will be used to create functions on lists.

Program 3. creates the function definition.
Program 4. definition of the empty function.
Program 5. associate function definition.
Program 6. insert function definition.
Program 7. first definition of the function.
Figure . insert effect.
Program 8. insert function definition.

The following programs have started the definition of functions 3 to 8, all of them being included in a file or in separate files. At the beginning of each file, the #include lista_def.h.

Figure 1 reflects the normal state, i.e. the element inserted is not the first or the last.

The inverse functions of tying and inserting are the first and exit, as instead of tying an element remove it.

Once these functions are compiled, it is convenient to store them in a library to facilitate the linking procedure when referencing from applications. The functions of the previous chapter were not located in a library because they were written for a specific application, but the purpose of the functions of this chapter is to offer a new type of data for any application, so the standard procedure is to be located in a library.

Each object module storage operating system in libraries has a command, as discussed in chapter 11.

If the library is complete, let's see how the mentioned functions saved in the library can be used.

We will explain it by example. Let's think that in the database of the previous chapter we want to list the books by date of publication. As indicated in Program 9, we will define a list and its components will be placed in the dynamic memory.

Each of the components or elements of the List is composed of the enclosed demonstrator, the date of publication as a priority and the index for the corresponding book. Once the list is completed by running all the books, the components will be removed and printed in order.

Note that the list and elements, which so far had no memory assigned, have been defined in the main program, one in static memory and one in dynamic memory. On the other hand, the functions associated with the lists are not managed, since the lista_fun.h file is included.

Linking the program will refer to the library to which the lists belong.

Programme 9. Use of list functions for book classification.
Babesleak
Eusko Jaurlaritzako Industria, Merkataritza eta Turismo Saila