User Tools

Site Tools


user:nbrimme1:portfolio:oct17

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
user:nbrimme1:portfolio:oct17 [2013/10/26 22:52] nbrimme1user:nbrimme1:portfolio:oct17 [2013/12/14 20:17] (current) nbrimme1
Line 1: Line 1:
 +====October 17, 2013====
 +Matt finished the make file for our stack program. Matt's code is included at the bottom of this entry. Information on Make files can be found [[http://lab46.corning-cc.edu/documentation/makefile|here]]. To use Matt's Make file:
  
 +__**On a lab46 terminal:**__
 +<cli>$ cd /var/public/data/fall2013/linkedlistimp</cli>
 +**Then copy the contents of the directory:**
 +  * **To copy into your ~/src/datas/ directory:**
 +<cli>$ make copy</cli>
 +  * **To copy into a different directory:**
 +<cli>$ cp -a ~/src/dir</cli>
 +
 +__**In Matt's Make file:**__
 +  * Variables are at the top.
 +  * Then there are "Labels" that run a set of "rules".
 +  * Written in "MAKEFILE" and runs set of Bash commands.
 +  * The **/inc** directory contains only headers.
 +  * The **/lib** directory contains only libraries.
 +  * The **/src** directory contains only the source code. //**Note:** The parts rule equals the running of the shell command. This means Matt's Make file will only work on a Unix system.//
 +  * **ar:** Sets up the archiver.
 +  * **cc:** Sets up the compiler.
 +  * **src:** Lists  all .c files in the src directory.
 +  * **obj:** Turns every .c file into a .o object file.
 +  * **lib:** Finds the current directory the script is in.
 +  * **all:** Compiles src, handles objects, handles libraries.
 +  * **debug rule:** Checks to see if the source code was compiled with debug symbols.
 +  * **lib rule:** Checks library information.
 +  * **clean rule:** Removes all .o object files.
 +  * **testing dir:** Makes the libraries, renames the executable from the source code, then builds the final executable.
 +
 +__**To use Matt's Make file:**__
 +  - <cli>$ make</cli>
 +  - <cli>$ ls lib</cli> Shows the newly made .lib files.
 +  - <cli>$ make clean</cli> Removes all the object files.
 +  - <cli>$ make debug</cli> Builds the source code with debugging symbols. Not required but good practice; can also use "make".
 +  - **The terminal output for Matt's Make file:** <cli>lab46:~/src/datas/linkedlistimp$ make
 +make[1]: Entering directory `/home/nbrimme1/src/datas/linkedlistimp/src'
 +make[2]: Entering directory `/home/nbrimme1/src/datas/linkedlistimp/src/list'
 +[L]                        ... SUCCESS
 +make[2]: Leaving directory `/home/nbrimme1/src/datas/linkedlistimp/src/list'
 +
 +make[2]: Entering directory `/home/nbrimme1/src/datas/linkedlistimp/src/node'
 +[B]   cp.c                 ... OK
 +[B]   mk.c                 ... OK
 +[B]   rm.c                 ... OK
 +[L]                        ... SUCCESS
 +make[2]: Leaving directory `/home/nbrimme1/src/datas/linkedlistimp/src/node'
 +
 +make[2]: Entering directory `/home/nbrimme1/src/datas/linkedlistimp/src/stack'
 +[B]   peek.c               ... peek.c: In function 'peek':
 +peek.c:6: warning: control reaches end of non-void function
 +OK
 +[B]   pop.c                ... OK
 +[B]   push.c               ... OK
 +[B]   stackops.c           ... OK
 +[L]                        ... SUCCESS
 +make[2]: Leaving directory `/home/nbrimme1/src/datas/linkedlistimp/src/stack'
 +
 +
 +make[1]: Leaving directory `/home/nbrimme1/src/datas/linkedlistimp/src'
 +
 +
 +lab46:~/src/datas/linkedlistimp$ ls lib
 +liblist.a  libnode.a  libstack.a
 +lab46:~/src/datas/linkedlistimp$ make clean
 +make[1]: Entering directory `/home/nbrimme1/src/datas/linkedlistimp/src'
 +make[2]: Entering directory `/home/nbrimme1/src/datas/linkedlistimp/src/list'
 +rm -f *.o  core
 +make[2]: Leaving directory `/home/nbrimme1/src/datas/linkedlistimp/src/list'
 +make[2]: Entering directory `/home/nbrimme1/src/datas/linkedlistimp/src/node'
 +rm -f *.o cp.o mk.o rm.o core
 +make[2]: Leaving directory `/home/nbrimme1/src/datas/linkedlistimp/src/node'
 +make[2]: Entering directory `/home/nbrimme1/src/datas/linkedlistimp/src/stack'
 +rm -f *.o peek.o pop.o push.o stackops.o core
 +make[2]: Leaving directory `/home/nbrimme1/src/datas/linkedlistimp/src/stack'
 +make[1]: Leaving directory `/home/nbrimme1/src/datas/linkedlistimp/src'
 +make[1]: Entering directory `/home/nbrimme1/src/datas/linkedlistimp/testing'
 +rm -f *.o stacktest core
 +make[1]: Leaving directory `/home/nbrimme1/src/datas/linkedlistimp/testing'
 +lab46:~/src/datas/linkedlistimp$ make debug
 +make[1]: Entering directory `/home/nbrimme1/src/datas/linkedlistimp/src'
 +make[2]: Entering directory `/home/nbrimme1/src/datas/linkedlistimp/src/list'
 +ar rcs ../../lib/liblist.a 
 +make[2]: Leaving directory `/home/nbrimme1/src/datas/linkedlistimp/src/list'
 +
 +make[2]: Entering directory `/home/nbrimme1/src/datas/linkedlistimp/src/node'
 +gcc -Wall -I ../../inc  -DDEBUG -g -c cp.c
 +gcc -Wall -I ../../inc  -DDEBUG -g -c mk.c
 +gcc -Wall -I ../../inc  -DDEBUG -g -c rm.c
 +ar rcs ../../lib/libnode.a cp.o mk.o rm.o
 +make[2]: Leaving directory `/home/nbrimme1/src/datas/linkedlistimp/src/node'
 +
 +make[2]: Entering directory `/home/nbrimme1/src/datas/linkedlistimp/src/stack'
 +gcc -Wall -I ../../inc  -DDEBUG -g -c peek.c
 +peek.c: In function 'peek':
 +peek.c:6: warning: control reaches end of non-void function
 +gcc -Wall -I ../../inc  -DDEBUG -g -c pop.c
 +gcc -Wall -I ../../inc  -DDEBUG -g -c push.c
 +gcc -Wall -I ../../inc  -DDEBUG -g -c stackops.c
 +ar rcs ../../lib/libstack.a peek.o pop.o push.o stackops.o
 +make[2]: Leaving directory `/home/nbrimme1/src/datas/linkedlistimp/src/stack'
 +
 +
 +make[1]: Leaving directory `/home/nbrimme1/src/datas/linkedlistimp/src'
 +
 +
 +lab46:~/src/datas/linkedlistimp$</cli>
 +\\
 +__**Matt's Code:**__ 
 +  * **/src/datas/linkedlistimp**
 +    * **/inc**
 +      * **list.h**<code c>
 +#ifndef _LIST_H
 +#define _LIST_H
 +
 +#include "node.h"
 +
 +struct list {
 + Node *start;
 + Node *end;
 + int qty;
 +};
 +typedef struct list List;
 +
 +List *mklist();
 +List *insert(List *, Node *, Node *);
 +List *append(List *, Node *, Node *);
 +List *removeNode(List *, Node **);
 +void displayf(List *);
 +void displayb(List *);
 +List *clearlist(List *);
 +List *sortlist(List *);
 +
 +#endif</code>
 +      * **node.h**<code c>
 +#ifndef _NODE_H
 +#define _NODE_H
 +
 +#include <stdlib.h>
 +
 +struct node {
 + struct node *next;
 + struct node *prev;
 + int value;
 +};
 +typedef struct node Node;
 +
 +Node *mknode(int);
 +void  rmnode(Node **);
 +Node *cpnode(Node *);
 +
 +#endif
 +</code>
 +      * **queue.h**<code c>
 +#ifndef _QUEUE_H
 +#define _QUEUE_H
 +
 +#include "list.h"
 +
 +struct queue {
 + List *data;
 + Node *front;
 + Node *back;
 + int bufsize;
 +};
 +typedef struct queue Queue;
 +
 +Queue *mkqueue(int);
 +Queue *enqueue(Queue *, Node *);
 +Node  *dequeue(Queue **);
 +
 +#endif
 +</code>
 +      * **stack.h**<code c>
 +#ifndef _STACK_H
 +#define _STACK_H
 +
 +#include "list.h"
 +
 +struct stack {
 + List *data;
 + Node *top;
 + int size;
 +};
 +typedef struct stack Stack;
 +
 +Stack *mkstack(int);
 +Stack *push   (Stack *, Node *);
 +Node  *pop    (Stack **);
 +Node  *peek   (Stack *);
 +
 +#endif
 +</code>
 +    * **/lib**
 +      * **liblist.a**: Library file for list.c.
 +      * **libnode.a**: Library file for node.c.
 +      * **libstack.a**: Library file for stack.c.
 +    * **/src**
 +      * **/src/list**
 +        * **Makefile**<code make>
 +CFLAGS = -Wall
 +INC = -I ../../inc
 +AR = ar
 +CC = gcc $(CFLAGS) $(INC) 
 +SRC = $(shell /bin/ls -1 *.c 2>/dev/null)
 +OBJ = $(SRC:.c=.o)
 +LIB = $(shell /bin/pwd | /bin/sed 's/^.*\///')
 +all: $(SRC) $(OBJ) lib
 +
 +debug: CC += -DDEBUG -g
 +debug: DEBUG = debug
 +debug: $(SRC) $(OBJ) lib
 +
 +.c.o:
 +ifneq ($(MAKECMDGOALS),debug)
 + @printf "[B]   %-20s ... " "$<"
 + @$(CC) -c $< && echo "OK" || echo "FAIL"
 +else
 + $(CC) -c $<
 +endif
 +
 +lib:
 +ifneq ($(MAKECMDGOALS),debug)
 + @printf "[L]   %-20s ... " "$<"
 + @$(AR) rcs ../../lib/lib$(LIB).a $(OBJ) && echo "SUCCESS" || echo "FAIL"
 +else
 + $(AR) rcs ../../lib/lib$(LIB).a $(OBJ)
 +endif
 +
 +
 +clean:
 + rm -f *.o $(OBJ) core</code>
 +      * **/src/node**
 +        * **Makefile**<code make>
 +CFLAGS = -Wall
 +INC = -I ../../inc
 +AR = ar
 +CC = gcc $(CFLAGS) $(INC) 
 +SRC = $(shell /bin/ls -1 *.c 2>/dev/null)
 +OBJ = $(SRC:.c=.o)
 +LIB = $(shell /bin/pwd | /bin/sed 's/^.*\///')
 +all: $(SRC) $(OBJ) lib
 +
 +debug: CC += -DDEBUG -g
 +debug: DEBUG = debug
 +debug: $(SRC) $(OBJ) lib
 +
 +.c.o:
 +ifneq ($(MAKECMDGOALS),debug)
 + @printf "[B]   %-20s ... " "$<"
 + @$(CC) -c $< && echo "OK" || echo "FAIL"
 +else
 + $(CC) -c $<
 +endif
 +
 +lib:
 +ifneq ($(MAKECMDGOALS),debug)
 + @printf "[L]   %-20s ... " "$<"
 + @$(AR) rcs ../../lib/lib$(LIB).a $(OBJ) && echo "SUCCESS" || echo "FAIL"
 +else
 + $(AR) rcs ../../lib/lib$(LIB).a $(OBJ)
 +endif
 +
 +
 +clean:
 + rm -f *.o $(OBJ) core
 +</code>
 +      * **/src/node**
 +        * **cp.c**<code c>
 +#include "node.h"
 +
 +Node *cpnode(Node *tmp)
 +{
 + Node *newNode = mknode(tmp -> value);
 +
 + newNode -> prev = newNode -> next = NULL;
 +
 + return(newNode);
 +}
 +</code>
 +        * **mk.c**<code c>
 +#include "node.h"
 +
 +Node *mknode(int value)
 +{
 + Node *tmp = (Node *) malloc (sizeof(Node));
 +
 + tmp -> prev  = tmp -> next = NULL;
 + tmp -> value =               value;
 +
 + return(tmp);
 +}
 +</code>
 +        * **rm.c**<code c>
 +#include "node.h"
 +
 +void rmnode(Node **tmp)
 +{
 + if ((*tmp) -> prev != NULL)
 + {
 + (*tmp) -> prev -> next = (*tmp) -> next;
 + }
 +
 + if ((*tmp) -> next != NULL)
 + {
 + (*tmp) -> next -> prev = (*tmp) -> prev;
 + }
 +
 + (*tmp) -> prev  = (*tmp) -> next = NULL;
 +}
 +</code>
 +        * **cp.o**: Object file for cp.c
 +        * **mk.o**: Object file for mk.o
 +        * **rm.o**: Object file for rm.o
 +      * **/src/stack**
 +        * **Makefile**<code make>
 +CFLAGS = -Wall
 +INC = -I ../../inc
 +AR = ar
 +CC = gcc $(CFLAGS) $(INC) 
 +SRC = $(shell /bin/ls -1 *.c 2>/dev/null)
 +OBJ = $(SRC:.c=.o)
 +LIB = $(shell /bin/pwd | /bin/sed 's/^.*\///')
 +all: $(SRC) $(OBJ) lib
 +
 +debug: CC += -DDEBUG -g
 +debug: DEBUG = debug
 +debug: $(SRC) $(OBJ) lib
 +
 +.c.o:
 +ifneq ($(MAKECMDGOALS),debug)
 + @printf "[B]   %-20s ... " "$<"
 + @$(CC) -c $< && echo "OK" || echo "FAIL"
 +else
 + $(CC) -c $<
 +endif
 +
 +lib:
 +ifneq ($(MAKECMDGOALS),debug)
 + @printf "[L]   %-20s ... " "$<"
 + @$(AR) rcs ../../lib/lib$(LIB).a $(OBJ) && echo "SUCCESS" || echo "FAIL"
 +else
 + $(AR) rcs ../../lib/lib$(LIB).a $(OBJ)
 +endif
 +
 +
 +clean:
 + rm -f *.o $(OBJ) core
 +</code>
 +        * **peek.c**<code c>
 +#include "stack.h"
 +
 +Node *peek(Stack *myStack)
 +{
 + // exercise left to the implementer
 +}
 +</code>
 +        * **pop.c**<code c>
 +#include "stack.h"
 +
 +Node *pop(Stack **myStack)
 +{
 + Node *tmp = NULL;
 +
 + if ((*myStack) != NULL)
 + {
 + tmp = (*myStack) -> data -> end;
 + (*myStack) -> data = removeNode((*myStack) -> data, &tmp);
 + (*myStack) -> top  = (*myStack) -> data -> end;
 + }
 +
 + return (tmp);
 +}
 +</code>
 +        * **push.c**<code c>
 +#include "stack.h"
 +
 +Stack *push(Stack *myStack, Node *newNode)
 +{
 + if ((myStack -> size <= 0) || ((myStack -> data -> qty) < (myStack -> size)))
 + {
 + myStack -> data = append(myStack -> data, myStack -> data -> end, newNode);
 + myStack -> top  = myStack -> data -> end;
 + }
 +
 + return(myStack);
 +}
 +</code>
 +        * **stackops.c**<code c>
 +#include "stack.h"
 +
 +Stack *mkstack(int size)
 +{
 + Stack *myStack;
 +
 + myStack = (Stack *) malloc (sizeof(Stack));
 +
 + myStack -> data = mklist();
 + myStack -> size = size;
 + myStack -> top  = myStack -> data -> end;
 +
 + return (myStack);
 +}
 +</code>
 +        * **peek.o**: Object file for peek.c.
 +        * **pop.o**: Object file for pop.c.
 +        * **push.o**: Object file for push.c.
 +        * **stackops.o**: Object file for stackops.c.
 +      * **Makefile**<code make>
 +SHELL = /bin/bash -e
 +CC = g++ $(CFLAGS) $(OPTS) $(INC)
 +INC = -I ../include/
 +CFLAGS = -Wall 
 +OBJ = $(shell find . -name *.o)
 +OPTS =
 +PARTS = $(shell /bin/ls -1Ad * | grep -v 'Makefile')
 +AR = ar
 +DEBUG = 
 +default: libs
 +
 +debug: CC += -DDEBUG -g
 +debug: DEBUG = debug
 +debug: libs
 +
 +libs:
 + @for i in $(PARTS); do make -C $$i $(DEBUG); echo; done
 + @echo
 +
 +clean:
 + @for i in $(PARTS); do make -C $$i clean; done
 + @rm -f ${LIB}</code>
 +    * **/testing**
 +      * **Makefile**<code make>
 +SHELL = /bin/bash -e
 +INC = -I inc/
 +CCFLAGS = -Wall 
 +OPTS =
 +CC = gcc $(CCFLAGS) $(OPTS) $(INC)
 +OBJ = 
 +AR = ar
 +DEBUG = 
 +LIBS = src
 +UNIT = testing
 +ALL = $(LIBS) testing
 +BIN = 
 +default: libs testing
 +debug: libs-debug testing-debug
 +#default: $(LIBS) $(BIN)
 +
 +libs-debug: CC += -DDEBUG -g
 +libs-debug: DEBUG = debug
 +libs-debug: libs
 +
 +libs:
 + @for i in $(LIBS); do make -C $$i $(DEBUG); echo; done
 + @echo
 +
 +testing-debug: CC += -DDEBUG -g
 +testing-debug: DEBUG = debug
 +testing-debug: testing
 +
 +testing:
 + @for i in $(UNIT); do make -C $$i $(DEBUG); echo; done
 + @echo
 +
 +clean:
 + @for i in $(ALL); do make -C $$i clean; done
 +# @rm -f bin/*
 +
 +copy:
 + mkdir -p ~/src/data/linkedlistimp
 + cp -av /var/public/data/fall2013/linkedlistimp/* ~/src/data/linkedlistimp/
 +
 +help:
 + @echo
 + @echo "****************[ Data Structures List Implementation ]*****************"
 + @echo "** make                     - build everything                        **"
 + @echo "** make debug               - build everything with debug symbols     **"
 + @echo "**                                                                    **"
 + @echo "** make testing             - build unit tests                        **"
 + @echo "** make testing-debug       - build unit tests with debugging symbols **"
 + @echo "** make libs                - build all supporting libraries          **"
 + @echo "** make libs-debug          - build all libraries with debug symbols  **"
 + @echo "**                                                                    **"
 + @echo "** make clean               - clean; remove all objects/compiled code **"
 + @echo "** make help                - this information                        **"
 + @echo "************************************************************************"
 +</code>
 +        * **stacktest.c**<code c>
 +#include <stdio.h>
 +#include "stack.h"
 +
 +int main()
 +{
 + Node *tmp;
 + Stack *myStack;
 + myStack = mkstack(0);
 + tmp = create();
 + tmp -> value = fgetc(stdin);
 + fgetc(stdin);
 +
 + while(tmp->value != '\n')
 + {
 + myStack = push(myStack, tmp);
 + tmp = create();
 + tmp->value = fgetc(stdin);
 + fgetc(stdin);
 + }
 +
 + fprintf(stdout, "linked list has %d nodes\n", myStack->data->qty);
 + fprintf(stdout, "String is: ");
 + do {
 + tmp = pop(&myStack);
 + if(tmp != NULL)
 + {
 + fprintf(stdout, "%c", tmp->value);
 + }
 + rmnode(*tmp);
 + }
 + while(tmp != NULL);
 +
 + fprintf(stdout, "\n");
 + return(0);
 +}
 +</code>
 +    * **Makefile**<code make>
 +SHELL = /bin/bash -e
 +INC = -I inc/
 +CCFLAGS = -Wall 
 +OPTS =
 +CC = gcc $(CCFLAGS) $(OPTS) $(INC)
 +OBJ = 
 +AR = ar
 +DEBUG = 
 +LIBS = src
 +UNIT = testing
 +ALL = $(LIBS) testing
 +BIN = 
 +default: libs testing
 +debug: libs-debug testing-debug
 +#default: $(LIBS) $(BIN)
 +
 +libs-debug: CC += -DDEBUG -g
 +libs-debug: DEBUG = debug
 +libs-debug: libs
 +
 +libs:
 + @for i in $(LIBS); do make -C $$i $(DEBUG); echo; done
 + @echo
 +
 +testing-debug: CC += -DDEBUG -g
 +testing-debug: DEBUG = debug
 +testing-debug: testing
 +
 +testing:
 + @for i in $(UNIT); do make -C $$i $(DEBUG); echo; done
 + @echo
 +
 +clean:
 + @for i in $(ALL); do make -C $$i clean; done
 +# @rm -f bin/*
 +
 +copy:
 + mkdir -p ~/src/data/linkedlistimp
 + cp -av /var/public/data/fall2013/linkedlistimp/* ~/src/data/linkedlistimp/
 +
 +help:
 + @echo
 + @echo "****************[ Data Structures List Implementation ]*****************"
 + @echo "** make                     - build everything                        **"
 + @echo "** make debug               - build everything with debug symbols     **"
 + @echo "**                                                                    **"
 + @echo "** make testing             - build unit tests                        **"
 + @echo "** make testing-debug       - build unit tests with debugging symbols **"
 + @echo "** make libs                - build all supporting libraries          **"
 + @echo "** make libs-debug          - build all libraries with debug symbols  **"
 + @echo "**                                                                    **"
 + @echo "** make clean               - clean; remove all objects/compiled code **"
 + @echo "** make help                - this information                        **"
 + @echo "************************************************************************"
 +</code>
 +
 +I'm finding that GDB is quickly becoming an indispensable and invaluable tool when finding and fixing bugs. The command alias **gcc='gcc -g'** won't negatively effect anything in the future, so I'll make it an alias. Also, Google has some nice GDB "cheat sheets" that will come in handy.
 +
 +I had the idea to use 2 lists instead of modifying just 1. My logic is complicated and that logic seems far more simpler; and hopefully simpler to implement.
 +
 +**Pseudocode/Logic steps:**
 +  - Create 2 lists: (Not sure this will work; Could copy tmp's list into tmp2's list, then call clearList() on tmp2?).
 +    - tmp  -> unsorted list.
 +    - tmp2 -> sorted list. 
 +  - Find the lowest value in tmp's list
 +  - Insert that value into tmp2's list
 +  - Remove the lowest value from tmp's list
 +  - Repeat steps 2-4 until the program is at the end of the tmp's list.
 +    - Not sure if this is needed; delete empty tmp list?
 +  - Display tmp2's sorted list.
 +
 +[[http://lab46.corning-cc.edu/opus/fall2013/nbrimme1/start|Back to my Opus]]