Write the following new mutating methods for the integer linked list class we wr
ID: 3528362 • Letter: W
Question
Write the following new mutating methods for the integer linked list class we wrote during lecture: (a) insertInOrder: takes an int as parameter, places it in the first place in belongs in increasing order. For example, if the current list is 2,4 then a 3 would be inserted before the 4; a second 2 would be inserted at the front of the list. This method will not be called unless the list is already in increasing order. (b) remove: takes an int parameter, removes the first Node that contains that value. The value must be present in the list. Returns the value. (c) removeAll: takes an int parameter, removes every Node that contains that value. The value must be present in the list. Returns the value. (d) reverseCopy: takes no parameters, returns a copy of the list in reverse order.Explanation / Answer
follow this #include struct GSList; GSList * g_slist_alloc (void); GSList * g_slist_append (GSList *list, gpointer data); GSList * g_slist_prepend (GSList *list, gpointer data); GSList * g_slist_insert (GSList *list, gpointer data, gint position); GSList * g_slist_insert_before (GSList *slist, GSList *sibling, gpointer data); GSList * g_slist_insert_sorted (GSList *list, gpointer data, GCompareFunc func); GSList * g_slist_remove (GSList *list, gconstpointer data); GSList * g_slist_remove_link (GSList *list, GSList *link_); GSList * g_slist_delete_link (GSList *list, GSList *link_); GSList * g_slist_remove_all (GSList *list, gconstpointer data); void g_slist_free (GSList *list); void g_slist_free_full (GSList *list, GDestroyNotify free_func); void g_slist_free_1 (GSList *list); #define g_slist_free1 guint g_slist_length (GSList *list); GSList * g_slist_copy (GSList *list); GSList * g_slist_reverse (GSList *list); GSList * g_slist_insert_sorted_with_data (GSList *list, gpointer data, GCompareDataFunc func, gpointer user_data); GSList * g_slist_sort (GSList *list, GCompareFunc compare_func); GSList * g_slist_sort_with_data (GSList *list, GCompareDataFunc compare_func, gpointer user_data); GSList * g_slist_concat (GSList *list1, GSList *list2); void g_slist_foreach (GSList *list, GFunc func, gpointer user_data); GSList * g_slist_last (GSList *list); #define g_slist_next (slist) GSList * g_slist_nth (GSList *list, guint n); gpointer g_slist_nth_data (GSList *list, guint n); GSList * g_slist_find (GSList *list, gconstpointer data); GSList * g_slist_find_custom (GSList *list, gconstpointer data, GCompareFunc func); gint g_slist_position (GSList *list, GSList *llink); gint g_slist_index (GSList *list, gconstpointer data); void g_slist_push_allocator (gpointer dummy); void g_slist_pop_allocator (void); Description The GSList structure and its associated functions provide a standard singly-linked list data structure. Each element in the list contains a piece of data, together with a pointer which links to the next element in the list. Using this pointer it is possible to move through the list in one direction only (unlike the Doubly-Linked Lists which allow movement in both directions). The data contained in each element can be either integer values, by using one of the Type Conversion Macros, or simply pointers to any type of data. List elements are allocated from the slice allocator, which is more efficient than allocating elements individually. Note that most of the GSList functions expect to be passed a pointer to the first element in the list. The functions which insert elements return the new start of the list, which may have changed. There is no function to create a GSList. NULL is considered to be the empty list so you simply set a GSList* to NULL. To add elements, use g_slist_append(), g_slist_prepend(), g_slist_insert() and g_slist_insert_sorted(). To remove elements, use g_slist_remove(). To find elements in the list use g_slist_last(), g_slist_next(), g_slist_nth(), g_slist_nth_data(), g_slist_find() and g_slist_find_custom(). To find the index of an element use g_slist_position() and g_slist_index(). To call a function for each element in the list use g_slist_foreach(). To free the entire list, use g_slist_free(). Details struct GSList struct GSList { gpointer data; GSList *next; }; The GSList struct is used for each element in the singly-linked list. gpointer data; holds the element's data, which can be a pointer to any kind of data, or any integer value using the Type Conversion Macros. GSList *next; contains the link to the next element in the list. g_slist_alloc () GSList * g_slist_alloc (void); Allocates space for one GSList element. It is called by the g_slist_append(), g_slist_prepend(), g_slist_insert() and g_slist_insert_sorted() functions and so is rarely used on its own. Returns : a pointer to the newly-allocated GSList element. g_slist_append () GSList * g_slist_append (GSList *list, gpointer data); Adds a new element on to the end of the list. Note The return value is the new start of the list, which may have changed, so make sure you store the new value. Note Note that g_slist_append() has to traverse the entire list to find the end, which is inefficient when adding multiple elements. A common idiom to avoid the inefficiency is to prepend the elements and reverse the list when all elements have been added. 1 2 3 4 5 6 7 8 9 10 /* Notice that these are initialized to the empty list. */ GSList *list = NULL, *number_list = NULL; /* This is a list of strings. */ list = g_slist_append (list, "first"); list = g_slist_append (list, "second"); /* This is a list of integers. */ number_list = g_slist_append (number_list, GINT_TO_POINTER (27)); number_list = g_slist_append (number_list, GINT_TO_POINTER (14)); list : a GSList data : the data for the new element Returns : the new start of the GSList g_slist_prepend () GSList * g_slist_prepend (GSList *list, gpointer data); Adds a new element on to the start of the list. Note The return value is the new start of the list, which may have changed, so make sure you store the new value. 1 2 3 4 /* Notice that it is initialized to the empty list. */ GSList *list = NULL; list = g_slist_prepend (list, "last"); list = g_slist_prepend (list, "first"); list : a GSList data : the data for the new element Returns : the new start of the GSList g_slist_insert () GSList * g_slist_insert (GSList *list, gpointer data, gint position); Inserts a new element into the list at the given position. list : a GSList data : the data for the new element position : the position to insert the element. If this is negative, or is larger than the number of elements in the list, the new element is added on to the end of the list. Returns : the new start of the GSList g_slist_insert_before () GSList * g_slist_insert_before (GSList *slist, GSList *sibling, gpointer data); Inserts a node before sibling containing data. slist : a GSList sibling : node to insert data before data : data to put in the newly-inserted node Returns : the new head of the list. g_slist_insert_sorted () GSList * g_slist_insert_sorted (GSList *list, gpointer data, GCompareFunc func); Inserts a new element into the list, using the given comparison function to determine its position. list : a GSList data : the data for the new element func : the function to compare elements in the list. It should return a number > 0 if the first parameter comes after the second parameter in the sort order. Returns : the new start of the GSList
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.