Week 7 Laboratory — Memory Management, Linked Lists

As usual, you can use dry run:

prompt$ ~cs1921/bin/dryrun lab07

prompt$ ~cs1921/bin/dryrun lab07 2

Exercises

  1. (Memory management) Write a program called mary.c that concatenates all its command-line arguments, including the name of the executable itself, into a single string on the heap. This string is then printed. Below are some testcases.

     prompt$ ./mary 1 2 3 4 5 6 7 8 9 0
     ./mary1234567890
    

    Notice that there are no spaces.

     prompt$ ./mary had a little lamb, its fleece was white as snow
     ./maryhadalittlelamb,itsfleecewaswhiteassnow
    

    If there are no arguments:

     prompt$ ./mary
     ./mary
    

    You may use the functions in the string library string.h. You may not use any fixed-length arrays.
  2. (Memory management) Write a program called heapsum.c that places all of its numerical command-line arguments on the heap, then sums them, and outputs the result. So for example, we have:

    prompt$ ./heapsum 1 2 3
    6
    

    prompt$ ./heapsum 12 34 56 78 90
    270
    

    prompt$ ./heapsum 100
    100
    

    If there are no arguments, the program does nothing.

    prompt$ ./heapsum
    prompt$
    

    Your program should contain:

    The main() function should call both functions to compute the sum. Note further:

  3. (Linked Lists) Write a program called llbuild.c that builds a linked list from user input. The only functions that you may call are:

    The program:

    A sample interaction is:
    prompt$ ./llbuild
    Enter an integer: 12
    Enter an integer: 34
    Enter an integer: 56
    Enter an integer: quit
    Finished: list is 12->34->56
    

    Note that any non-numeric data 'finishes' the interaction:
    prompt$ ./llbuild
    Enter an integer: 100
    Enter an integer: bye
    Finished: list is 100
    

    A longer interaction:
    prompt$ ./llbuild
    Enter an integer: 421
    Enter an integer: 456732
    Enter an integer: 321
    Enter an integer: 4
    Enter an integer: 86
    Enter an integer: 89342
    Enter an integer: 9
    Enter an integer: #
    Finished: list is 421->456732->321->4->86->89342->9
    

    If the user provides no data, then no list is output:
    prompt$ ./llbuild
    Enter an integer: coffee
    Finished
    

    Note, please ensure:

  4. (Linked Lists) Write a program called llsum.c that uses a linked list to sum a stream of data on stdin. Your program should call functions to build the linked list, to print the list, to add the data stored in the list and finally to free the list.

    Numbers stored in the linked list should be summed with a function of prototype:
    int sumList(NodeT *head);
    
    which returns the sum of the nodes in the list

    Below is shown a number of testcases:

  5. (Linked Lists) Using the program from the previous exercise as starting point, write a program called llord.c that creates a linked list from a stream of data on stdin and indicates whether the linked list is in ascending order or not.

    The order of the data in the linked list should be checked with a function of prototype:
    int isOrderedList(NodeT *head);
    
    which returns 1 if the linked list is ordered, otherwise 0

    You may assume that the data is numerical. Nodes that have the same value are considered to be ordered. Examples of use are shown below.

Submit your work using:


  give  cs1921  lab07  mary.c  heapsum.c  llbuild.c  llsum.c  llord.c