Table of Contents

Corning Community College

CSCS2320 Data Structures

~~TOC~~

Project: Singly-Linked List of Nodes (SLL0)

Errata

This section will document any updates applied to the project since original release:

Objective

We've made it to lists! Time to start applying nodes to more elaborate manipulations, all while contained within our new (singly-linked) list implementation!

Reference

If you haven't done so yet, you absolutely, positively, MUST watch this video: http://www.youtube.com/watch?v=5VnDaHBi8dM

Background

This connected node concept will be with us for the rest of the semester.

The trick will be how we are connecting and manipulating these nodes which defines the theme of a particular activity.

As you encountered in the introductory projects, we've been connecting nodes into sequential list organizations (what we refer to, conceptually, as a linked list).

There are, however, many distinctions of linked lists… so we tend to add on a lot of qualifiers to better identify the particular type of linked list we are implementing/using.

As our nodes up to this point have only contained a single Node pointer for connection (the “there” pointer), we have what are called “singly-linked nodes”.

Therefore, generically, we are creating a “Singly-Linked List”; but we should also be specific about what we are creating a singly-linked list of– so the full description will be “List of Singly-Linked Nodes”.

Confusing naming terminology? Oh, just wait… we've barely gotten started.

organizational unit

We've already experienced lists.. in concept they are nothing more than ensuring nodes are connected together and manipulated in predefined ways (think about insertion, for instance).

In practice, though, we will make our lives easier by introducing an organizational unit to aid us in managing our linked lists. It is important to note, that what we are going to do, while common practice, does not in and of itself make a linked list… all we are doing is making our access of the linked list easier… that's it (and you're welcome).

Just as the node struct is an organizational unit for the data and style of connecting (nodes would be useless if we had no need to put something of value in them… nor would they be of use if we couldn't connect them in various ways).

So, we're just going to add another layer on top– we're going to create a List struct, which will house some of the important managerial/administrative data about our list. Specifically, in the current context, the Node pointers referencing our list's starting and ending locations (so we'll have one container variable managing the details of our list, which will make associative manipulation all the easier, if only at the cost of a little more typing).

I give you, our list struct:

struct list {
    struct node *initial;
    struct node *closing;
};

All this does, as I said, is establish a container to better manage what has (up to this point) been individually managed variables. Hopefully you've been seeing how valuable the node container has been for managing node-related details– this list container will do the same for the details we need to manage as a result of manipulating nodes.

To make our lives easier, lets toss in a typedef as well:

typedef struct list List;

Now, we apply similar operations to our List container as we would individual Nodes… when we insert(), for instance, we will be passing our List container around, which will reduce the number of parameters needed.

To address these aspects, we just access the struct elements as we have all along (as always, be mindful of a particular struct's contents):

    List *myList = mklist();
    Node *tmp = mknode(7);
 
    myList = insert(myList, myList -> initial, tmp); // insert tmp before initial node of myList

And we can access the value contained in the list's initial node as follows: myList → initial → info

We do need to be mindful of the organization we are creating- as part of implementing this, we'll have various list management functions at our disposal (insert, append, sort, getpos, obtain, etc.)… so we should restrict our list manipulation to those functions (reduce the number of cooks in the kitchen). This is another aspect of relinquishing our default tight fist of control… it may take some getting used to, but as you start seeing the bigger picture, you'll see what a big favor we're doing for ourselves here.

Procedure for bootstrapping

Obtain

On Lab46, change into your sln1 directory (I'll use ~/src/data/sln1/ in my examples):

lab46:~$ cd src
lab46:~/src$ cd data
lab46:~/src/data$ cd sln1
lab46:~/src/data/sln1$ 

NOTE: At this point you should be completed with the sln1 project.

If you are done, and a make help shows an upgrade-sll0 option being available:

lab46:~/src/data/sln1$ make help
****************[ Data Structures Node Implementation ]*****************
** make                     - build everything (libs, units, apps)    **
** make debug               - build everything with debug symbols     **
** make check               - check implementation for validity       **
**                                                                    **
** make libs                - build all supporting libraries          **
** make libs-debug          - build all libraries with debug symbols  **
** make apps                - build unit tests                        **
** make apps-debug          - build unit tests with debugging symbols **
** make units               - build unit tests                        **
** make units-debug         - build unit tests with debugging symbols **
**                                                                    **
** make save                - create a backup archive                 **
** make submit              - submit assignment (based on dirname)    **
**                                                                    **
** make update              - check for and apply updates             **
** make reupdate            - re-apply last revision                  **
** make reupdate-all        - re-apply all revisions                  **
**                                                                    **
** make upgrade-sll0        - upgrade to next project (sll0)          **
** make clean               - clean; remove all objects/compiled code **
** make help                - this information                        **
************************************************************************
lab46:~/src/data/sln1$ 

We can proceed with bootstrapping the sll0 project:

lab46:~/src/data/sln1$ make upgrade-sll0
Cleaning out object code    ... OK


Project backup process commencing

Taking snapshot of current project (sln1)      ... OK
Compressing snapshot of sln1 project archive   ... OK
Setting secure permissions on sln1 archive     ... OK

Project backup process complete

Commencing upgrade process from sln1 to sll0

Creating project sll0 directory tree           ... OK
Copying your code to sll0 project directory    ... OK
Copying new sll0 files into project directory  ... OK
Synchronizing sll0 project revision level      ... OK
Establishing sane file permissions for sll0    ... OK

Upgrade Complete! You may now switch to the ../sll0 directory

lab46:~/src/data/sln1$ 

As you can see from the last line: You may now switch to the ~/src/data/sll0 directory

So let's do it:

lab46:~/src/data/sln1$ cd ../sll0
lab46:~/src/data/sll0$ 

Overview

You'll see various files and directories located here (one regular file, Makefile, and 6 directories). The directory structure (note, not all these directories may yet be present) for the project is as follows:

Operating

The project is driven by a fleet of optimized Makefiles, which will facilitate the compiling process for us.

Each Makefile plays a unique role (the closer the Makefile is to the source code, the more specialized it becomes).

The base-level Makefile is used to enact whole-project actions, such as initiating a compile, cleaning the project directory tree of compiled and object code, submitting projects, or applying bug-fixes or upgrading to other projects.

Running make help will give you a list of available options:

lab46:~/src/data/sll0$ make help
****************[ Data Structures List Implementation ]*****************
** make                     - build everything (libs, units, apps)    **
** make debug               - build everything with debug symbols     **
** make check               - check implementation for validity       **
**                                                                    **
** make libs                - build all supporting libraries          **
** make libs-debug          - build all libraries with debug symbols  **
** make apps                - build unit tests                        **
** make apps-debug          - build unit tests with debugging symbols **
** make units               - build unit tests                        **
** make units-debug         - build unit tests with debugging symbols **
**                                                                    **
** make use-test-reference  - use working implementation object files **
** make use-your-own-code   - use your own implementation code        **
**                                                                    **
** make save                - create a backup archive                 **
** make submit              - submit assignment (based on dirname)    **
**                                                                    **
** make update              - check for and apply updates             **
** make reupdate            - re-apply last revision                  **
** make reupdate-all        - re-apply all revisions                  **
**                                                                    **
** make clean               - clean; remove all objects/compiled code **
** make help                - this information                        **
************************************************************************
lab46:~/src/data/sll0$ 

In general, you will likely make the most frequent use of these options:

  1. make: just go and try to build everything
  2. make debug: build with debugging support
  3. make clean: clean out everything so we can do a full compile

Most of what you do will be some combination of those 3 options.

Project Submission

When you are done with the project and are ready to submit it, you simply run make submit:

lab46:~/src/data/sll0$ make submit
...

Bugfixes and Updates

Sometimes, a typo or other issue will be uncovered in the provided code you have. I will endeavor to release updates which will enable you to bring your code up-to-date with my copy.

When a new update is available, you will start seeing the following appear as you go about using make:

lab46:~/src/data/sll0$ make
*********************************************************
*** NEW UPDATE AVAILABLE: Type 'make update' to apply ***
*********************************************************
...
lab46:~/src/data/sll0$ 

When this occurs, you may want to perform a backup (and/or commit/push any changes to your repository)– certain files may be replaced, and you do not want to lose any modifications you have made:

lab46:~/src/data/sll0$ make save
Archiving the project ...
Compressing the archive ...
Setting Permissions ...
lab46:~/src/data/sll0$ 

Once you have done that, go ahead and apply the update (note this output is from the node0 project update, so you will not see the exact same output):

lab46:~/src/data/sll0$ make update
Update 1 COMMENCING
Update 1 CHANGE SUMMARY: Fixed base and other Makefile typos
    Please reference errata section on project page for more information
Update 1 COMPLETE
Updated from revision 0 to revision 1
lab46:~/src/data/sll0$ 

At this point your code is up to date (obviously the above output will reflect whatever the current revision is).

error reporting

To facilitate debugging and correction of errors and warnings in your code at compile time, such compiler messages will be redirected to a text file called errors in the base of the project directory.

You can view this file to ascertain what errors existed in the last build of the project.

With each new project build, this file is overwritten, so you always have the most up-to-date version of compile-time information.

Project Overview

header file

In inc/, you will find a new file: list.h

Take a look inside, and ask any questions to get clarification:

1
#ifndef _LIST_H
#define _LIST_H
 
#include "node.h"                       // list relies on node to work
 
struct list {
    Node *initial;                      // pointer to start of list
    Node *closing;                      // pointer to end of list
};
typedef struct list List;               // because we deserve nice things
 
List *mklist(void  );                   // create/allocate new list struct
List *insert (List *, Node *, Node *);  // add node before given node
void  displayf(List *, int);            // display list from beginning to final node
int   getpos(List *, Node *);           // retrieve position from given node
Node *setpos(List *, int   );           // seek to indicated node in list
 
#endif

This is your API for the list library. In order to use the list library three things need to happen:

list library

In src/list/, you will find 4 C files (containing a total of 5 functions):

Skeletons of functions has been provided. Fill in the gaps, and implement the functions that have been left blank.

Take a look at the code there. These are the files that contain functions which will be compiled and archived into the list library (liblist.a) we will be using in this and future projects.

Figure out what is going on, make sure you understand it.

NOTE: None of these files denote an entire runnable program. These are merely standalone functions. The various programs under the app/ and unit/ directories will use these functions in addition to their application logic to create complete executable programs.

You will also notice there are function prototypes for these list library functions in the list.h header file, located in the inc/ subdirectory, which you'll notice all the related programs you'll be playing with in this project are #includeing.

List library unit tests

In unit/list/, you will find 4 files (along with a Makefile):

These are complete runnable programs (when compiled, and linked against the list library, which is all handled for you by the Makefile system in place).

Of particular importance, I want you to take a close look at:

Building the code

You've made changes to a file in src/list/, and are ready to see your results. What do we do?

First, let's have a second terminal open and logged into lab46. Put it in the base directory of the sll0 project:

lab46:~$ cd src/data/sll0
lab46:~/src/data/sll0$  

Keep your other terminal in the ~/src/data/sll0/src/list/ directory, so you can more effectively make changes.

cleaning things out

If you've already ran make to build everything a few times, you may want to clean things out and do a fresh compile (never hurts, and might actually fix some problems):

lab46:~/src/data/sll0$ make clean
...
lab46:~/src/data/sll0$ 

compile project

Next, compile the whole project

lab46:~/src/data/sll0$ make
...

Our binaries

Compiled executables go in the bin directory, so if we change into there and take a look around we see the various executables built (both new with this project as well as migrated code from previous projects).

lab46:~/src/data/sll0$ cd bin
lab46:~/src/data/sll0/bin$ ls
...
lab46:~/src/data/sll0/bin$ 

Run the program

To run unit-insert, we'd do the following (specify a relative path to the executable):

lab46:~/src/data/sll0/bin$ ./unit-insert

The program will now run, and do whatever it was programmed to do.

Reference Implementation

As the layers and complexities rise, narrowing down the source of errors becomes increasingly important.

If unit-insert isn't working, is it because of a problem there, in your insert() function, or in one of the node functions it calls, such as mknode()?

To aid you in your development efforts, you now have the ability to import a working implementation of previous project functions into your current project for the purposes of testing/debugging purposes.

Using the test reference implementation

You'll notice that, upon running make help in the base-level Makefile, the following new options appear (about halfway in the middle):

**                                                                    **
** make use-test-reference  - use working implementation object files **
** make use-your-own-code   - use your node implementation code       **
**                                                                    **

In order to make use of it, you'll need to run make use-test-reference from the base of your sll0 project directory, as follows:

lab46:~/src/data/sll0$ make use-test-reference
...
NODE reference implementation in place, run 'make' to build everything.
lab46:~/src/data/sll0$ 

You'll see that final message indicating everything is in place (it automatically runs a make clean for you), and then you can go ahead and build everything with it:

lab46:~/src/data/sll0$ make
...

Debugging: When using the test reference implementation, you will not be able to debug the contents of the test reference implementation functions (the files provided do not have debugging symbols added), so you'll need to take care not to step into these functions (it would be just like stepping into printf(). You can still compile the project with debugging support and debug (as usual) those compiled functions (ie the stack functions).

Reverting back to using your code

If you were trying out the reference implementation to verify queue functionality, and wanted to revert back to your own code, it is as simple as:

lab46:~/src/data/sll0$ make use-your-own-code
Local node implementation restored, run 'make clean; make' to build everything.
lab46:~/src/data/sll0$ 

Just to be clear: the reference implementation is not some magic shortcut getting you out of doing this project; it merely gives you a glimpse into how things are working, or should be working, provided your node library is complete and fully functional.

Expected Results

To assist you in verifying a correct implementation, a fully working implementation of the node library and list library (up to this point) should resemble the following:

list library (so far)

Here is what you should get for all the functions completed so far in the list library:

lab46:~/src/data/sll0$ make check
====================================================
=    Verifying Singly-Linked List Functionality    =
====================================================
  [mklist] Total:   5, Matches:   5, Mismatches:   0
  [insert] Total:  11, Matches:  11, Mismatches:   0
[displayf] Total:  10, Matches:  10, Mismatches:   0
  [getpos] Total:   8, Matches:   8, Mismatches:   0
  [setpos] Total:   9, Matches:   9, Mismatches:   0
====================================================
 [RESULTS] Total:  43, Matches:  43, Mismatches:   0
====================================================
lab46:~/src/data/sll0$ 

Submission

Project Submission

When you are done with the project and are ready to submit it, you simply run make submit:

lab46:~/src/data/PROJECT$ make submit
...

Submission Criteria

To be successful in this project, the following criteria must be met:

  • Project must be submit on time, by the posted deadline.
    • Early submissions will earn 1 bonus point per full day in advance of the deadline.
      • Bonus eligibility requires an honest attempt at performing the project (no blank efforts accepted)
    • Late submissions will lose 33% credit per day, with the submission window closing on the 3rd day following the deadline.
      • To clarify: if a project is due on Wednesday (before its end), it would then be 33% off on Thursday, 66% off on Friday, and worth 0% once it becomes Saturday.
      • Certain projects may not have a late grace period, and the due date is the absolute end of things.
  • all requested functions must be implemented in the related library
  • all requested functionality must conform to stated requirements (either on this project page or in comment banner in source code files themselves).
  • Output generated must conform to any provided requirements and specifications (be it in writing or sample output)
    • output obviously must also be correct based on input.
  • Processing must be correct based on input given and output requested
  • Project header files are NOT to be altered. During evaluation the stock header files will be copied in, which could lead to compile-time problems.
  • Code must compile cleanly.
    • Each source file must compile cleanly (worth 3 total points):
      • 3/3: no compiler warnings, notes or errors.
      • 2/3: one of warning or note present during compile
      • 1/3: two of warning or note present during compile
      • 0/3: compiler errors present (code doesn't compile)
  • Code must be nicely and consistently indented (you may use the indent tool)
    • You are free to use your own coding style, but you must be consistent
    • Avoid unnecessary blank lines (some are good for readability, but do not go overboard- double-spacing your code will get points deducted).
    • Indentation will be rated on the following scale (worth 3 total points):
      • 3/3: Aesthetically pleasing, pristine indentation, easy to read, organized
      • 2/3: Mostly consistent indentation, but some distractions (superfluous or lacking blank lines, or some sort of “busy” ness to the code)
      • 1/3: Some indentation issues, difficult to read
      • 0/3: Lack of consistent indentation (didn't appear to try)
  • Unless fundamentally required, none of your code should perform any inventory or manual counting. Basing your algorithms off such fixed numbers complicates things, and is demonstrative of a more controlling nature.
  • Code must be commented
    • Any “to be implemented” comments MUST be removed
      • these “to be implemented” comments, if still present at evaluation time, will result in points being deducted.
    • Commenting will be rated on the following scale (worth 3 total points):
      • 3/3: Aesthetically pleasing (comments aligned or generally not distracting), easy to read, organized
      • 2/3: Mostly consistent, some distractions or gaps in comments (not explaining important things)
      • 1/3: Light commenting effort, not much time or energy appears to have been put in.
      • 0/3: No original comments
      • should I deserve nice things, my terminal is usually 90 characters wide. So if you'd like to format your code not to exceed 90 character wide terminals (and avoid line wrapping comments), at least as reasonably as possible, those are two sure-fire ways of making a good impression on me with respect to code presentation and comments.
    • Sufficient comments explaining the point of provided logic MUST be present
  • Code must be appropriately modified
    • Appropriate modifications will be rated on the following scale (worth 3 total points):
      • 3/3: Complete attention to detail, original-looking implementation- also is not unnecessarily reinventing existing functionality
      • 2/3: Lacking some details (like variable initializations), but otherwise complete (still conforms, or conforms mostly to specifications), and reinvents some wheels
      • 1/3: Incomplete implementation (typically lacking some obvious details/does not conform to specifications)
      • 0/3: Incomplete implementation to the point of non-functionality (or was not started at all)
    • Implementation must be accurate with respect to the spirit/purpose of the project (if the focus is on exploring a certain algorithm to produce results, but you avoid the algorithm yet still produce the same results– that's what I'm talking about here).. worth 3 total points:
      • 3/3: Implementation is in line with spirit of project
      • 2/3: Some avoidance/shortcuts taken (note this does not mean optimization– you can optimize all you want, so long as it doesn't violate the spirit of the project).
      • 1/3: Generally avoiding the spirit of the project (new, different things, resorting to old and familiar, despite it being against the directions)
      • 0/3: entirely avoiding.
    • Error checking must be adequately and appropriately performed, according to the following scale (worth 3 total points):
      • 3/3: Full and proper error checking performed for all reasonable cases, including queries for external resources and data.
      • 2/3: Enough error checking performed to pass basic project requirements and work for most operational cases.
      • 1/3: Minimal error checking, code is fragile (code may not work in full accordance with project requirements)
      • 0/3: No error checking (code likely does not work in accordance with project requirements)
  • Any and all non-void functions written must have exactly 1 return statement
    • points will be lost for solutions containing multiple return statements in a function.
  • Absolutely, positively NO (as in ZERO) use of goto statements.
    • points will most definitely be lest for solutions employing such things.
  • No custom global variables! The header files provide all you need.
    • Do NOT edit the header files.
  • Track/version the source code in a repository
  • Filling out any submit-time questionnaires
  • Submit a copy of your source code to me using the submit tool (make submit will do this) by the deadline.