GTK gossip: gslist, glist

Source: Internet
Author: User

Gslist is a link vertex. Its definition is as follows:

Typedef struct {


Gpointer data;


Gslist * next;


} Gslist;



Data is the location information of the vertex information (object), and next is the address information of the next vertex information, with gslist
You can perform operations such as adding, inserting, and deleting links to the vertex, such as using g_slist_append (), g_slist_prepend, use g_slist_sort () for sorting.

The following program is a simple example, using gslist as a heap example:

  • Gslist_demo.c
# Include <glib. h>

Void for_callback (gstring * string, gpointer user_data ){
If (string ){
Printf ("% s/n", string-> Str );
}
}

Int main (INT argc, char * argv []) {
Gstring * string;
Gslist * List;
Int select;
Char input [10];

List = NULL; // There is no starting point

While (true ){
Printf (
"/N please select (-1 end): (1) added to heap worker (2) values except worker end (3) show all content ");
Printf ("/N $ C> ");
Scanf ("% d", & select );

If (select =-1 ){
Break;
}

Switch (select ){
Case 1:
Printf ("/n accept input value :");
Scanf ("% s", & input );
String = g_string_new (input );
List = g_slist_prepend (list, string );
Break;
Case 2:
String = List-> data;
List = g_slist_remove (list, string );
Printf ("/n trim end value removed: % s", string-> Str );
Break;
Case 3:
G_slist_foreach (list, (gfunc) for_callback, null );
Break;
Default:
Printf ("/n select multiple variables! ");
}
}

Printf ("/N ");

G_slist_free (list );

Return 0;
}


The results of a line are as follows:

Please select option (-1 end): (1) Add to heap memory (2) exclude volume end value (3) show all content
$ C> 1

Inbound value: Caterpillar

Please select option (-1 end): (1) Add to heap memory (2) exclude volume end value (3) show all content
$ C> 1

Volume input value: momor

Please select option (-1 end): (1) Add to heap memory (2) exclude volume end value (3) show all content
$ C> 1

Comment input value: Bush

Please select option (-1 end): (1) Add to heap memory (2) exclude volume end value (3) show all content
$ C> 3
Bush
Momor
Caterpillar

Please select option (-1 end): (1) Add to heap memory (2) exclude volume end value (3) show all content
$ C> 2

Remove comment end value: Bush
Please select option (-1 end): (1) Add to heap memory (2) exclude volume end value (3) show all content
$ C>-1


Glist is a forward-to-backward relation. Its definition is as follows:

Typedef struct {


Gpointer data;


Glist * next;


Glist * Prev;


} Glist;


Prev is a function that points to the previous vertex. You can refer to Glist for its combination.
Description file.


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.