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 here. To use Matt's Make file:
On a lab46 terminal:
$ cd /var/public/data/fall2013/linkedlistimp
Then copy the contents of the directory:
$ make copy
$ cp -a ~/src/dir
In Matt's Make file:
To use Matt's Make file:
$ make
$ ls lib
Shows the newly made .lib files.
$ make clean
Removes all the object files.
$ make debug
Builds the source code with debugging symbols. Not required but good practice; can also use “make”.
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$
Matt's Code:
#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
#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
#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
#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
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
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
#include "node.h" Node *cpnode(Node *tmp) { Node *newNode = mknode(tmp -> value); newNode -> prev = newNode -> next = NULL; return(newNode); }
#include "node.h" Node *mknode(int value) { Node *tmp = (Node *) malloc (sizeof(Node)); tmp -> prev = tmp -> next = NULL; tmp -> value = value; return(tmp); }
#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; }
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
#include "stack.h" Node *peek(Stack *myStack) { // exercise left to the implementer }
#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); }
#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); }
#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); }
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}
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 "************************************************************************"
#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); }
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 "************************************************************************"
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: