User Tools

Site Tools


blog:fall2015:dshadeck:journal

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
blog:fall2015:dshadeck:journal [2015/10/05 15:12] – [September 28, 2015] dshadeckblog:fall2015:dshadeck:journal [2015/10/05 16:53] (current) – [September 28, 2015] dshadeck
Line 1: Line 1:
-=====HPC0=====+=====HPC 1=====
  
 ====August 31, 2015==== ====August 31, 2015====
Line 425: Line 425:
 </code> </code>
  
 +====October 5, 2015====
 +#include "list.h"
 +<code c>
 +//////////////////////////////////////////////////////////////////////
 +//
 +//   insert() - add a new node to the given list before the node
 +//              at the indicated place. insert() needs to manage
 +//              the necessary connections pointers) to maintain list
 +//              integrity, along with ensuring the start and end
 +//              indicators of the list remain relevant and up-to-date.
 +//
 +//    behavior: on a NULL list- allocate a list and proceed
 +//              on a NULL newNode- return list as is
 +//
 +//        note: you are expected to make use of the getpos() and
 +//              setpos() functions to aid you in accomplishing this
 +//              task. Don't needlessly reinvent the wheel.
 +//
 +//              as with the other functions, you may use no more
 +//              than one return() statement per function.
 +//
 +List *insert(List *myList, Node *place, Node *newNode)
 +{
 +        if(myList == NULL) { //check if list is NULL
 +                myList = mklist(); //make new list
 +        }
 +        if(newNode != NULL) { //checks if newNode is not NULL
 +                if(myList -> first == NULL || myList -> last == NULL) { //checks beginning and end
 +                        myList -> first = newNode;
 +                        myList -> last = newNode;
 +                } else {
 +                        int pos = getpos(myList, place);
 +                        if(pos == 0) {
 +                                myList -> first = newNode;
 +                        } else {
 +                                Node *before = setpos(myList, pos - 1); //set pos
 +                                before -> after = newNode;
 +                        }
 +                        newNode -> after = place;
 +                }
 +        }
 +        //returns
 +        return(myList);
 +}
 +
 +// Author: Dan Shadeck
 +// 09/29/2015
 +// Data Structures
 +</code>
 +
 +<code c>
 +#include <stdio.h>
 +#include "list.h"
 +
 +//////////////////////////////////////////////////////////////////////
 +//
 +// displayf() - display the list in a specified orientation, also
 +//              with or without positional values (as indicated
 +//              by the mode parameter).
 +//
 +//       modes: 0 display the list forward, no positional values
 +//              1 display the list forward, with positional values
 +//     
 +//        note: positional values are indexed starting from 0
 +//     
 +//    behavior: on a NULL list, output "NULL"
 +//              on an EMPTY list, output "-> NULL"
 +//              on error (invalid mode): MOD by 2 and do the result
 +//                 (or the logical equivalent)
 +//
 +//      format: output is to be on a single line, with node values
 +//              space-padded. and separated by "->" sequences, flush
 +//              against the left margin, of the forms:
 +//
 +// for forward:   val0 -> val1 -> val2 -> ... -> valN-1 -> valN -> NULL
 +//
 +//              or, if being displayed with node positions:
 +//
 +// for forward: [0] val0 -> [1] val1 -> ... -> [N-1] valN-1 -> [N] valN -> NULL
 +//
 +//        note: ALL output ends with a newline character
 +//
 +void displayf(List *myList, int mode)
 +{
 +        if(myList == NULL) {
 +                printf("NULL\n");
 +        } else if (mode==0){
 +                Node *tmp = myList -> first;
 +                char ran = 'f';
 +                while (tmp != NULL){
 +                        ran = 't';
 +                        printf ("%hhd -> ", tmp -> info);
 +                        tmp = tmp -> after; // getting the next value
 +                }
 +                if(ran == 't') {
 +                        printf("NULL\n");
 +                } else {
 +                        printf("-> NULL\n");
 +                }
 +        } else if (mode==1){
 +                Node *tmp = myList -> first;
 +                int count = 0;
 +                char ran = 'f';
 +                while (tmp != NULL){
 +                        ran = 't';
 +                        printf ("[%d] %hhd -> ", count, tmp -> info);
 +                        tmp = tmp -> after; //getting the next value
 +                        count++;        //incr count to give us node position 
 +                }
 +                if(ran == 't') {
 +                        printf("NULL\n");
 +                } else {
 +                        printf("-> NULL\n");
 +                }
 +        }
 +}
 +
 +// this is displaying the list with or with out position values
 +
 +// Author: Dan Shadeck
 +// 09/29/2015
 +// Data Structures
 +
 +</code>
 +
 +<code c>
 +//////////////////////////////////////////////////////////////////////
 +//
 +// getpos() - a list library utility function to identify the actual
 +//            position of a given node in an indicated list.
 +//
 +//      note: Indexing of the list starts from 0
 +//
 +//  behavior: on error (NULL list or given node), return negative
 +//            value (-1), on out-of-bounds condition, return -2
 +//
 +//            you are to have only ONE return statement for this
 +//            entire function. Change the existing one as needed.
 +//
 +int getpos(List *myList, Node *given)
 +{
 +    int count = 0;
 +        char broke = 'f';
 +        if(myList == NULL || given == NULL) { //checks if both list and given are NULL
 +                count = -1;
 +        } else {
 +                Node *tmp = myList -> first;
 +        while (tmp != NULL){
 +                if (tmp == given){
 +                broke = 't';
 +                break;
 +                }
 +                tmp = tmp -> after; //getting the next value
 +                count++;    //incr count to give us node position 
 +        }
 +        }
 +    if(broke == 'f' && count != -1) {// checks if loop broke
 +                count = -2;
 +        }
 +        return(count);
 +}
 +
 +//////////////////////////////////////////////////////////////////////
 +//
 +// setpos() - a list library utility function to set a node pointer
 +//            to the node at the indicated position of a list.
 +//
 +//      note: Indexing of the list starts from 0
 +//
 +//  behavior: on error (NULL list or negative/out-of-bounds pos),
 +//            return NULL.
 +//
 +//            you are to have only ONE return statement for this
 +//            entire function. Change the existing one as needed.
 +//
 +Node *setpos(List *myList, int pos)
 +{
 +        //returning node variable
 +        Node *node = NULL;
 +        if(myList != NULL && pos >= 0) { //checking if list is null and pos index
 +                Node *tmp = myList -> first;
 +                int counter = 0;
 +                while(tmp != NULL) {//while loop; checks tmp not NULL
 +                        if(counter == pos) {
 +                                node = tmp;
 +                                break;
 +                        }
 +                        tmp = tmp -> after;
 +                        counter++;
 +                }
 +        }
 +        //returns
 +        return(node);
 +}
 +
 +// Author: Dan Shadeck
 +// 09/29/2015
 +// Data Structures
 +
 +</code>
 +
 +<code c>
 +#include "list.h"
 +
 +//////////////////////////////////////////////////////////////////////
 +//
 +// mklist() - a list library function to allocate and initialize a 
 +//            new list.
 +//
 +//  behavior: on error, return NULL.
 +//
 +//      note: you are to have only ONE return statement for this
 +//            entire function. Change the existing one as needed.
 +//
 +List *mklist(void)
 +{
 +        List *newlist = (List *) malloc(sizeof(List)); //allocating mem 
 +
 +        return(newlist);  
 +
 +}
 +
 +// extra comment just to be clear that this is a list library function to allocate and initialize a new list
 +
 +// Author: Daniel Shadeck
 +// 9/29/2015
 +// Data Structures
 +
 +</code>
 =====Data Communications===== =====Data Communications=====
  
Line 999: Line 1228:
  
 </code> </code>
 +
 +====October 5, 2015====
 +
 +Lets hope that today we have a break through on the chart.
blog/fall2015/dshadeck/journal.1444057977.txt.gz · Last modified: 2015/10/05 15:12 by dshadeck