How do linear data structures work
Linear data structures
Linear data structures
If data structures are used, it is usually a matter of managing any number of objects in a suitable data structure, e.g. all students in a school. In such a case it makes no sense to model all properties of a student as primitive data types and to create a separate field or similar for each property. Therefore, at the beginning of this chapter we first explain how objects can be used as data record stores. Then the linear data structures field, list, stack and queue are dealt with.
Objects vs. primitive data types
Objects as data storage
In this section we would like to model a student administration. We are designing a separate class for this Student and first model the required properties as attributes. If necessary, there are also methods for changing and querying the attributes.
In this example, the two attributes age and name have been defined. Both must be transferred to the when creating a student object (cf. constructor), both can also be changed afterwards using the set methods and queried at any time using the get methods.
Objects of a class that, as in this example, only function as data storage and have no or almost no program logic of their own, are called in Java Beans ("Coffee Beans").
The only data structure in Java that can manage primitive data types in addition to objects is the field. All other data structures (e.g. list, stack, queue, etc.) only allow objects to be saved.
But what should be done if it is sufficient for an application to only store numbers (e.g. ISBN numbers or shoe sizes)? Do you then have to laboriously design your own class as described in the student example?
For these rare cases there are so-called in Java Wrapper classes built-in.
|primitive data type||Wrapper class||Service method|
Example: A primitive int value 17 is "boxed" in an object of the Integer class.
In order to then read it out of the object again (unboxing), a corresponding service method is available for each wrapper class (see table).
The obvious data structure for managing objects is the field. It's built in to Java and is pretty easy to use.
Use of a field
At this point the example of a student administration should be taken up again. A corresponding class of pupils has already been developed in the previous section (see Objects as data storage).
The creation and use of a field via student objects is analogous to the use of a field via primitive data types (see also section Fields).
As can be seen in the source text, the student objects are first created and then placed in the appropriate positions as usual in a field. The method is available() shows an example of the passage through a field.
- The field is built into Java and can be used without additional effort.
- Access is possible to every field position. This enables a fast binary search with logarithmic effort to be implemented again.
- The field is static, i.e. its size must be determined when it is created. As a result, it could either be much too large (wasted memory) or much too small (no subsequent insertion possible).
The disadvantage of static size is countered by dynamic data structures such as the list (see section List). The search tree represents the ideal combination of fast search and dynamics (see section Search tree).
The list is a linear data structure that is traversed sequentially (i.e. from front to back). Each element of the list only has access to its successor, which results in a linked structure.
The list is dynamic because new elements can be added to the chain at any position. A search on a list has a linear duration, since only a complete run from front to back is possible.
The high school class Cunning is generic, i.e. the class of the objects that are to be managed in the list is already determined when they are created. It is of course possible (even if only rarely makes sense) to also manage objects from subclasses of this class.
Build a list
In the following example, when the list is generated, it is specified that objects of the class String should be managed. With the help of the method append the strings are appended to the end of the list, which changes the order Babsi - Franzi - Susi results.
Go through a list
When going through a list (e.g. to carry out a search) there is a jump to the beginning of the list using the method toFirst () required. Then the request helps hasAccess () to ask whether a list element is still available or by continuing through the list with the method next () the end of the list has already been reached.
If the string list from the previous section is used as a basis, the output of the loop is: Babsi -> Franz -> Susi -> END!
Modify a list
For the next example, let's imagine a music collection in the objects of the class title to get managed. A rating between 1 and 5 is given for each title object, which is determined by the method getRating () can be requested.
The loop then goes through the list of all title objects and deletes with the method remove () those titles that have received a rating of 1. In addition, the number of deleted objects is shown in the variable counter counted in.
Insert into a sorted list
Sorting a list can sometimes also be useful. In our example of the music collection, the title objects could be sorted according to the rating. So that the sorting of the list is not lost, the user of the music collection must be provided with his own method for the sorted insertion of a new title.
The loop is run through until a title is found that has a higher or the same rating as the new title. At this point, the associated title object with the method insert inserted before the current list object.
Stack and Queue
As a special case of the list, the stack and queue are very similar, which is why they are often discussed together. The queue is a FIFO data structure (first in, first out) - the objects are managed in the order in which they were inserted into the data structure. Direct access is only available to the foremost element of the snake (the head), it is always inserted at the back (at the end of the snake). The stack, on the other hand, is referred to as a LIFO data structure (last in, first out). Here, new objects are always placed on top of the stack; direct access is also only available to the top object of the stack.
Build a stack
As with a list, when creating a stack, you first have to specify the class of the objects that are to be managed in the stack. In the following example three strings are placed on the stack. The LIFO structure results in the sequence seen from top to bottom on the stack Hans - Maria - Manfred.
Go through a stack
The stack can be easily traversed from top to bottom with a loop.
The stack is reduced in the process, i.e. access to the objects is lost. If this is undesirable, the individual objects must be cached in a different data structure during the run.
Build a queue
As with a list, when creating a queue, you must first specify the class of the objects that are to be managed in the queue. In the example below, three strings are inserted into the queue. The sequence results from the FIFO structure in the queue Manfred - Maria - Hans.
Run through a queue
A snake can easily be traversed from front to back with a loop.
The queue is dismantled, i.e. access to the objects is lost. If this is undesirable, the individual objects must be cached in a different data structure during the run.
- Is eating chicken unhealthy for every meal?
- Why am I cold after peeing?
- What is the importance of mathematics in business life
- Why are information systems important
- What does CPL Affiliate Marketing mean
- Malaysia Like Chinese girls Malay men
- What English accent should Indians learn
- Why do people constantly degrade others online
- Which is the best business WordPress theme
- What does real poverty mean?
- What does Fuori mean in Italian
- Is it hard to understand
- How long have you been reading Quora?
- What is a healthy mind-body approach
- What do boys expect from their girlfriends
- Why do people look different
- What do cleaning services cost in general
- Legibility hurts publishers
- What is a search engine
- How can I make money in fashion
- What are the Greeks called in the Bible?
- Why did Poseidon raped Medusa
- Will man ever be smarter than God?
- Do dogs have a favorite person?
- What after installing Wordpress
- Why Does Your Business Need Online Marketing
- Where can you find WordPress plugins
- Singaporean Chinese people eat dogs
- Which is Delhi's best restaurant
- What is the best web scraping software
- Where do you write code
- Before feminism, women had better lives
- What happened to the Austrian Nazis after World War II
- What are the behavioral statements for anorexia