Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7458557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:27:48+00:00 2026-05-29T13:27:48+00:00

So I’m trying to implement the following; // Destructive Abstract data type ilist struct

  • 0

So I’m trying to implement the following;

  // Destructive Abstract data type ilist

   struct ilist_ADT;
   typedef struct ilist_ADT *ilist;
   // prototype for secret implementation
   // do not rely on ilist being a pointer

   ilist iempty();
   //    returns an empty ilist

   int iempty_huh(ilist il);
   //    returns 1 (true) if il is empty
   //    returns 0 (false) if il is not empty

   int ifirst(ilist il);
   //    returns the first element in il
   //    il must not be empty

   ilist icons_destroy(int in, ilist il);
   // returns an ilist with in added as the first element of il
   // references to il cease to be valid ilists
   // the result must eventually be consumed by one of:
   //     icons_destroy, irest_destroy, idelete

   ilist irest_destroy(ilist il);
   // modifies il to remove the first element, and returns the modified ilist
   // frees the memory associated with the first element
   // references to il cease to be valid ilists
   // the result (if non-empty) must eventually be consumed by one of:
   //     icons_destroy, irest_destroy, idelete

   ilist icopy(ilist il);
   // returns a new copy of il that continues to be a valid
   // ilist with the same elements even when il is destroyed
   // the result must eventually be consumed by one of:
   //     icons_destroy, irest_destroy, idelete

   int ilength(ilist il);
   // computes the number of elements in il

   void idelete(ilist il);
   //    frees the storage for ilist
   //    all further references to il become invalid
   //    NOTE: every ilist created by icons_destroy or
   //          irest_destroy or icopy  must eventually be destroyed
   //          by being consumed by icons_destroy or
   //          irest_destroy or idelete

I am focusing on icons first, and i have a non-destructive icons such as:

ilist icons(int in, ilist il) {      
   ilist r = malloc(sizeof(struct ilist_ADT));
   r->first = in;
   r->rest = il;
   r->length = 1 + ilength(il);  
   return r;

}

How exactly would i make this to suit the implementation? in other words how do i make it destructive?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T13:27:48+00:00Added an answer on May 29, 2026 at 1:27 pm

    “Destructive” means that the arguments to a function (or the receiver in an oop’s method) is allowed to be modified. A non-destructive function means that it does not modify its arguments but instead returns a modified copy. The advantage of allowing a destructive function is, that you dont have to create that copy and thus you typically need fewer mallocs (in this example, however the number is the same).

    In the implementation of icons you provided you can see that con’ing an entry keeps the original ilist il a valid unmodified list!: Anyone using ilist il will not notice that you did anything (this only works with singly-linked lists of course).

    A destructive implementation first allows you to modify the arguments, but it also implies that you are supposed to modify the arguments in a meaningful way: You should modify it such that someone who still has a reference to the original ilist il should see your modification. You can do this like so:

    // observation: the names are not the best, i would call it destructiveICons
    //   (and ->first should be called ->value)
    ilist icons_destroy(int in, ilist il) {  
        ilist second = malloc(sizeof(struct ilist_ADT));
        second->length = il->length
        second->first = il->first
        second->rest = il->rest;
        il->length += 1;
        il->rest = second;
        il->first = in;
        return il;
    }
    

    Now someone else who holds a ref to ilist il will see the new-first element followed by the old-previously-first element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.