Using C to simulate object-oriented implementations such as Java's LinkedList collection (wonderful)

Source: Internet
Author: User

We all know that C is a process-oriented language, how to simulate object-oriented?

Do not forget the structure of this thing, said and the class is still somewhat similar

First we have to understand what is called a linked list, just like a line of people lined up one after another, you find the first one to know the next

If you are not sure, you can go and see the Java LinkedList Collection

And then we'll bring you the exact same set of LinkedList.

First of all, in order to understand, first use Java to write and then change into C version

A linked list is a node attached together, so we first create an entity class node ( where generics are used, and if you have questions you can see my other article about generics )

This value represents the values of this node, since we are doing a doubly linked list, we have to say that we have our own previous (up) and next (next) two properties

In order to simulate the linkedlist we look at using his grammar

Since LinkedList has these methods, we also create a collection of our own called Mylinkedlist

First you have to have three member variables to record

We also use generics, and then here are the concrete implementations of each method

Delete It's like a row of people in a column, you're ready to go you say to the people in front of you: "I'm going to go, your back is not me, it's behind me."

And then said to the back: "Your front is not me, is my front this", in fact, is to assign 2 values

Having said so much, I haven't talked about C, and then the important moment for Java to C:

Note the contrast, you will find a little face to the opposite, because C does not have generics, so we simulate the collection can only load int type of data

The first is the node class, but C has no class, but there are structs, just do it;

See the similarities and differences? The Initnode method is actually equal to the constructor of the class, where malloc () is the meaning of opening up memory space in C

The only drawback is that C does not encapsulate this feature, the construction method is written out, and there is no private modifier to control access

Next is the code of Mylinkedlist, there is a method in the structure of C here!

Since some words are keywords that cannot be used as function names, there are some differences from Java versions, but it's easy to see

We can put a pointer to the function in the structure, so we can call the method through this struct, Java version: List.save (1) version C: List->save (1)

Because we have just said, C has no concept of encapsulation, so the method can only be written out of the structure, and because of this, there is no such thing.

So our member variables

Can only be written as a global variable of C

This is a flaw, and the list *list we define can be used as this, and no is used to record

Then there is our construction method, because members in the struct cannot initialize, only one equivalent to the constructor method is written.

is actually to open up space and assign the corresponding method pointer to the struct member.

The following provides the added method implementation, the other self-control Java version extrapolate

Finally, with the Java version and the C version of the call method, you will find, exactly the same, just C no generics

Java Edition:

Version C:

Using C to simulate object-oriented implementations such as Java's LinkedList collection (wonderful)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.