SI 204 Spring 2017 / HWs


This is the archived website of SI 204 from the Spring 2017 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

Homework 31: Making linked lists

Name: _____________________________________________ Alpha: ___________________

Describe help received: ________________________________________________________

  • Due before class on Wednesday, April 12
  • This homework contains code to be submitted electronically. Put your code in a folder called hw31 and submit using the 204sub command.
  • This is an electronic homework and you do not need to print out anything to turn in during class.

Assignment

  1. Required reading: Sections 1 (Intro) and 2 (Creating linked lists) from the Unit 9 notes.
  2. Write a program called back3.c that reads in ints from the user until a negative number is entered, storing them into a linked list. Then print out the 3rd-to-last number that was entered. (You can assume that at least 3 numbers will always be entered before a negative.)

    Hint: Store the numbers in your linked list in reverse order by calling add2front repeatedly as you read each number in.

    Hint 2: Don't hesitate to draw a picture of some small examples, like with five or six numbers, to understand and check your code!

    Example runs:

    roche@ubuntu$ ./back3
    1 2 3 4 5 6 7 8 9 -1
    7
    roche@ubuntu$ ./back3
    4 8 2 7 4 9 -1
    7
    roche@ubuntu$ ./back3
    15 16 17 -1
    15