Memory in lists

This is a discussion on Memory in lists within the C Programming forums, part of the General Programming Boards category; So basically I'm struggling with question 5 in this assignment, http://www.student.cs.uwaterloo.ca/~...ssignments/a5/ We are basically given functions that act like Scheme ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Memory in lists

    So basically I'm struggling with question 5 in this assignment,http://www.student.cs.uwaterloo.ca/~...ssignments/a5/We are basically given functions that act like Scheme list functions and in question 5 I am supposed to create a function called iappend, which appends two lists. This is my code and it seems to work.
    Code:
    ilist iappend(ilist il1, ilist il2) {
        ilist acc = iempty();
        ilist acc1 = iempty();
        ilist a;
        while (!iempty_huh(il1)) {
            acc1 = icons(ifirst(il1), acc1);
            il1 = irest(il1);
        }
        while (!iempty_huh(il2)) {
            acc1 = icons(ifirst(il2), acc1);
            il2 = irest(il2);
        }
        a = acc1;
        while (!iempty_huh(acc1)) {
            acc = icons(ifirst(acc1), acc);
            acc1 = irest(acc1);
        }
        idelete(a);
        return acc;
    }
    but they want this code to be more efficient, can somewone help me code, so i won't make a temporary list ? I need to code it in such a way that i won't use idelete .... any suggestions would help , Thanks !

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Find the end of the first list, and the start of the second list. Make the last node in the first list point to the first node in the second list. Done.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Use exactly the same algorithm you'd use with a string of beads or popcorn, at your kitchen table. Simple is fast.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Sorry, im new with pointer's, how exactly would i find the end of the first list and point it to the second? also im suppose to "build-a-newlist" so i need to somehow put the inputs in a variable such as my acc

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Quote Originally Posted by Adak View Post
    Use exactly the same algorithm you'd use with a string of beads or popcorn, at your kitchen table. Simple is fast.
    i don't quite understand your analogy ? lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lists, memory, pointers?
    By savageseb in forum C Programming
    Replies: 3
    Last Post: 06-24-2010, 05:59 PM
  2. Help - Linked Lists/Structures/Memory Allocation
    By MetallicaX in forum C Programming
    Replies: 4
    Last Post: 03-18-2009, 01:18 AM
  3. Linked Lists and Memory
    By John_L in forum C Programming
    Replies: 1
    Last Post: 10-27-2007, 01:27 PM
  4. Memory allocation in linked lists
    By Panserbjorn in forum C Programming
    Replies: 6
    Last Post: 10-27-2007, 12:02 AM
  5. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 04:50 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21