STACK DATA STRUCTURE LIBRARY
John T. Rine
09/11/2011
My stack library is available for download from: My library is available at:
http://lab46.corning-cc.edu/~jr018429/stackLibrary.zip
The main objectives of this project are to learn about stack data structures, and create a functioning stack library in C. What are stack data structures used for and how are they implemented? These are the questions that I will attempt to answer as this project develops.
In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:
A data structure holds data and provides operations to the user that can be performed on the data it contains. A stack is a one type of data structure; it is a (LIFO) last in, first out data structure. Spring-loaded plate dispensers used in some restaurants are used to illustrate how the stack data structure operates. When the dispenser is empty, plates are loaded one on top of another. When a patron gets a plate, he or she takes a plate off the top of the “stack”; the spring loaded device then pushes the stack of plates up so that the next patron may take the next plate. This is how a stack data structure works. In fact, the stack operations are: push and pop. In order for push to work, it is important that data isn't pushed into a location that doesn't exist so the stack must be checked to see if the position in which data is to be pushed is valid.
Stack Library implementation Linked Lists provide a foundation for dynamic structures. Further exploring Data Structures, we find that particular manipulations of these structures enable interesting possibilities for computing. Stacks are one such iteration of these Data Structures we are exploring this semester.
An excellent exploration with Stacks would be to extend your Linked List Library with Stack Functionality (or to create a new Stack library that depends on the Linked List library).
To create a Stack library, you may want to have the following functions:
A stack can be relatively infinite (so long as available resources are present for allocation) or fixed in size.
Your stack implementation should be able to handle/recognize the following situations:
Implementing this functionality into a library will enable you to utilize it in additional projects.
The Stack Library project scope is available at:
http://lab46.corning-cc.edu/haas/fall2011/data/projects/stacklib
The course requirement for Data Structures projects are listed at:
http://lab46.corning-cc.edu/haas/fall2011/data/projects
This project has the following project attributes described on that page:
Attribute | Qty Needed | Description |
---|---|---|
Pointers | 8 | Indirection/dereferencing are demonstarted |
malloc/new | 4 | Utilization of dynamic memory allocation |
Stack | 4 | Implementation/utilization of a stack |
Libraries | 4 | Implementation/utilization of non-core libc library |
This project, therefore, is eligible for 20 attributes total, however, only 6 of these are achieveable per project, so project far exceeds the minimum attribute requirements.
My library is composed of the following files and functions:
all.h
all.h is the header file for the stack library. It should be included in any .c library files whose function prototypes have been added to it. Also, it must be added to any programs utilizing the library's functions.
Example code:
//all.h //John T. Rine //October 13, 2011 #ifndef _ALL_H #define _ALL_H #include<stdlib.h> #include<stdio.h> struct Node { int data; struct Node * prev; struct Node * next; }; typedef struct Node node; int isEmpty(node *); void push(node **, node **, /*int position,*/ int); int pop(node **head, node **tail); int peek(node *); int listStackSizeHead(node *); int listStackSizeTail(node *); void destroyFixedStackHead(node **, node **); void destroyFixedStackTail(node **, node **); void createFixedStack(node **, node **, int); void copyStack(node *, node **, node **); #endif
copyStack.c
copyStack.c contains only one function, copyStack. copyStack takes as its parameters head, the head of the stack to be copied, copyHead and copyTail; copyHead and copyTail become the head and tail of the copied stack.
//copyStack.c //John T. Rine //October 13, 2011 #include<stdio.h> #include<stdlib.h> #include"all.h" void copyStack(node *head, node **copyHead, node **copyTail) { node *new, *temp; new = *copyHead = *copyTail = temp = NULL; temp = head; while(temp != NULL) { if (*copyHead == NULL) { *copyHead = (node *) malloc(sizeof(node)); *copyTail = *copyHead; (*copyHead)->prev = NULL; (*copyHead)->next = NULL; (*copyHead)->data = temp->data; } else { new = (node *) malloc(sizeof(node)); new->prev = *copyTail; new->next = NULL; (*copyTail)->next = new; *copyTail = new; (*copyTail)->data = temp->data; } temp = temp->next; } }
createFixedStack.c
createFixedStack.c contains one function, createFixedStack. The function takes three parameters; head, tail, and elements. head and tail become the head and the tail of the new stack, whereas elements is the size of the new stack.
//createFixedStack.c //John T. Rine //October 12, 2011 #include<stdlib.h> #include<stdio.h> #include"all.h" void createFixedStack(node **head, node **tail, int elements) { int i; node *new; new = NULL; *head = *tail = NULL; for(i = 1; i<=elements; i++) { if (*head == NULL) { *head = (node *) malloc(sizeof(node)); *tail = *head; (*head)->prev = NULL; (*head)->next = NULL; } else { new = (node *) malloc(sizeof(node)); new->prev = *tail; new->next = NULL; (*tail)->next = new; *tail = new; } } }
destroyFixedStack.c
destroyFixedStack.c contains two functions for destroying a stack. destroyFixedStackHead destroys a stack from its head; destroyFixedStackTail destroys a stack from its tail.
//destroyFixedStack.c //John T. Rine //October 12, 2011 #include<stdlib.h> #include<stdio.h> #include"all.h" void destroyFixedStackHead(node **head, node **tail) { node *temp; node *temp2; temp = NULL; temp2 = NULL; temp = *head; while(temp != NULL) { temp2 = temp->next; if (temp->prev != NULL) temp->prev = NULL; if (temp->next != NULL) temp->next = NULL; free(temp); temp = temp2; } *head = *tail = NULL; } void destroyFixedStackTail(node **head, node **tail) { node *temp; node *temp2; temp = NULL; temp2 = NULL; temp = *tail; while(temp != NULL) { temp2 = temp->prev; if (temp->prev != NULL) temp->prev = NULL; if (temp->next != NULL) temp->next = NULL; free(temp); temp = temp2; } *head = *tail = NULL; }
isEmpty.c
isEmpty.c contains one function, isEmpty, to test a stack for to see if it is empty. If the stack is empty, it returns true, otherwise it returns false.
//isEmpty.c //John T. Rine //October 13, 2011 #include<stdio.h> #include<stdlib.h> #include"all.h" int isEmpty(node *tail) { if (tail == NULL) return 1; else return 0; }
listStackSize.c
listStackSize.c contains two functions for returning the size of a stack. listStackSizeHead takes head as its parameter; listStackSizeTail takes tail as its parameter.
//listrStackSize.c //John T. Rine //October 12, 2011 #include<stdlib.h> #include<stdio.h> #include"all.h" int listStackSizeHead(node *head) { int size = 0; while (head != NULL) { head = head->next; size++; } return size; } int listStackSizeTail(node *tail) { int size = 0; while (tail != NULL) { tail = tail->prev; size++; } return size; }
peek.c
peek.c contains one function for peeking, returning the value, at the top of the stack. Since it is peeking, it will not destroy the node as does the pop function.
//peek.c //John T. Rine //October 13, 2011 #include<stdio.h> #include<stdlib.h> #include"all.h" int peek(node *tail) { if (tail == NULL) { printf("No value to peek at...\n"); exit(1); } return tail->data; }
pop.c
pop.c contains one function for popping data from the top of a stack.
//pop.c //John T. Rine //October 13, 2011 #include"all.h" #include<stdio.h> #include<stdlib.h> int pop(node **head, node **tail) { int data = 0; node *temp = NULL; temp = *tail; if(*head != NULL && *tail != NULL) { if (temp == *head) { temp->next = NULL; data = temp->data; free(temp); *head = *tail = NULL; } else { temp->prev->next = NULL; *tail = temp->prev; temp->prev = NULL; data = temp->data; free(temp); } } else { printf("No stack to pop...\n"); exit(1); } return data; }
push.c
push.c contains one function for pushing data onto a stack.
//push.c //John T. Rine //October 12, 2011 #include<stdio.h> #include<stdlib.h> #include"all.h" void push(node **head, node **tail, /*int position,*/ int data) { int i; node *temp, *temp2; temp = temp2 = NULL; temp = *head; //for(i = 0; i < position; i++) //{ // temp = temp -> next; //} while(temp != NULL) { temp = temp->next; } if (*head == NULL) { *head = (node *) malloc(sizeof(node)); *tail = *head; (*head)->prev = NULL; (*head)->next = NULL; (*head)->data = data; } else// if (*tail == temp) { temp2 = (node *) malloc (sizeof(node)); temp2->prev = *tail; temp2->next = NULL; (*tail)->next = temp2; *tail = temp2; (*tail)->data = data; } //else //{ //temp2 = (node *) malloc (sizeof(node)); //temp2->prev = temp; //temp2->next = temp->next; //temp->next->prev = temp2; //temp->next = temp2; //temp2->data = data; //} }
stackTest.c
stackTest.c is a program used to test the stack library.
//stackTest.c //Author: John T. Rine //Date: October 12, 2011 #include"all.h" int main(int argc, char **argv) { int i, data; node *head, *tail, *temp, *copyHead, *copyTail; head = tail = temp = copyHead = copyTail = NULL; printf("Head is = 0x%x; Tail is = 0x%x\n", head, tail); printf("\nCreating a fixed size stack of 10 nodes...\n"); createFixedStack(&head, &tail, 10); printf("\nHead is = 0x%x; Tail is = 0x%x\n", head, tail); printf("\nSize of the list is: %d\n", listStackSizeTail(tail)); i = 10; temp = tail; while(temp != NULL) { temp->data = i; i--; temp = temp->prev; //temp = stack pointer } printf("\nPeeking at data in tail...\n"); printf("\nThe value in top of stack is %d\n", peek(tail)); if(isEmpty(tail)) printf("\nStack is empty!\n"); else printf("\nStack is not empty!\n"); temp = tail; while(temp != NULL) { printf("\nThis node contains %d\n", temp->data); temp = temp->prev; } printf("\nMaking a copy of the stack...\n"); copyStack(head, ©Head, ©Tail); printf("\nDestroying the first Stack...\n"); destroyFixedStackTail(&head, &tail); if(isEmpty(tail)) printf("\nStack is empty!\n"); else printf("\nStack is not empty!\n"); printf("\nHead is = 0x%x; Tail is = 0x%x\n", head, tail); printf("\nPrinting values of copy of stack...\n"); temp = copyTail; while(temp != NULL) { printf("\nThis node contains %d\n", temp->data); temp = temp->prev; } if(isEmpty(copyTail)) printf("\nCopy of stack is empty!\n"); else printf("\nCopy of stack is not empty!\n"); printf("\nDestroying copy of the stack...\n"); destroyFixedStackTail(©Head, ©Tail); if(isEmpty(copyTail)) printf("\nCopy of stack is empty!\n"); else printf("\nCopy of stack is not empty!\n"); printf("\nStarting a new stack using push and a loop...\n"); printf("\nEnter the number of data to push onto the stack...\n"); scanf("%d", &i); head = tail = NULL; for(;i > 0; i--) { printf("\nEnter the value to be pushed onto the stack...\n"); scanf("%d", &data); push(&head, &tail, data); } printf("\nHead is = 0x%x; Tail is = 0x%x\n", head, tail); printf("\nThe size of the stack is: %d\n", listStackSizeTail(tail)); printf("\nEnter the number of data to be popped off of the stack...\n"); scanf("%d", &i); for (;i > 0; i--) { printf("The value being popped off of the stack is %d\n", pop(&head, &tail)); } printf("\nHead is = 0x%x; Tail is = 0x%x\n", head, tail); printf("\nThe size of the stack is: %d\n", listStackSizeTail(tail)); getchar(); }
makeStack.bat
The Windows batch file makeStack.bat automates, manufacture of all of the .o files, the creation of the static library file, compiling, linking, and execution of stackTest, the test file.
Example code:
REM cd c:\mingw\jrprogs\Stack del *.o del *.exe del *.a gcc -c createFixedStack.c -o createFixedStack.o rem pause gcc -c destroyFixedStack.c -o destroyFixedStack.o rem pause gcc -c push.c -o push.o rem pause gcc -c pop.c -o pop.o rem pause gcc -c listStackSize.c -o listStackSize.o rem pause gcc -c isEmpty.c -o isEmpty.o rem pause gcc -c peek.c -o peek.o rem pause gcc -c copyStack.c -o copyStack.o rem pause ar rcs libdll.a *.o rem pause gcc -I. stackTest.c -o stackTest libdll.a rem pause stackTest pause
makeStack.sh
The bash shell script file makeStack.sh automates the manufacture of all of the .o files, the creation of the static library file, compiling, linking and execution of stackTest, the test file.
Example code:
#!/bin/bash rm *.o rm stackTest gcc -c createFixedStack.c -o createFixedStack.o gcc -c destroyFixedStack.c -o destroyFixedStack.o gcc -c push.c -o push.o gcc -c pop.c -o pop.o gcc -c listStackSize.c -o listStackSize.o gcc -c isEmpty.c -o isEmpty.o gcc -c peek.c -o peek.o gcc -c copyStack.c -o copyStack.o ar rcs libdll.a *.o gcc -I. stackTest.c -o stackTest libdll.a ./stackTest
Execution of makeStack.bat (compilation, creation of a static library, linking, and execution of the stackTest test program on a Windows machine
C:\MinGW\Stack>del *.o C:\MinGW\Stack>del *.exe C:\MinGW\Stack>gcc -c createFixedStack.c -o createFixedStack.o C:\MinGW\Stack>gcc -c destroyFixedStack.c -o destroyFixedStack.o C:\MinGW\Stack>gcc -c push.c -o push.o C:\MinGW\Stack>gcc -c pop.c -o pop.o C:\MinGW\Stack>gcc -c listStackSize.c -o listStackSize.o C:\MinGW\Stack>gcc -c isEmpty.c -o isEmpty.o C:\MinGW\Stack>gcc -c peek.c -o peek.o C:\MinGW\Stack>gcc -c copyStack.c -o copyStack.o C:\MinGW\Stack>ar rcs libdll.a *.o C:\MinGW\Stack>gcc -I. stackTest.c -o stackTest libdll.a C:\MinGW\Stack>stackTest Head is = 0x0; Tail is = 0x0 Creating a fixed size stack of 10 nodes... Head is = 0x3e2470; Tail is = 0x3e2548 Size of the list is: 10 Peeking at data in tail... The value in top of stack is 10 Stack is not empty! This node contains 10 This node contains 9 This node contains 8 This node contains 7 This node contains 6 This node contains 5 This node contains 4 This node contains 3 This node contains 2 This node contains 1 Making a copy of the stack... Destroying the first Stack... Stack is empty! Head is = 0x0; Tail is = 0x0 Printing values of copy of stack... This node contains 10 This node contains 9 This node contains 8 This node contains 7 This node contains 6 This node contains 5 This node contains 4 This node contains 3 This node contains 2 This node contains 1 Copy of stack is not empty! Destroying copy of the stack... Copy of stack is empty! Starting a new stack using push and a loop... Enter the number of data to push onto the stack... 3 Enter the value to be pushed onto the stack... 1 Enter the value to be pushed onto the stack... 2 Enter the value to be pushed onto the stack... 3 Head is = 0x3e2500; Tail is = 0x3e2530 The size of the stack is: 3 Enter the number of data to be popped off of the stack... 3 The value being popped off of the stack is 3 The value being popped off of the stack is 2 The value being popped off of the stack is 1 Head is = 0x0; Tail is = 0x0 The size of the stack is: 0 C:\MinGW\Stack>pause Press any key to continue . . .
Execution of makeStack.sh (compilation, creation of a static library, linking, and execution of the stackTest test program on a Linux machine
lab46:~/src/data/Stack$ ./makeStack.sh Head is = 0x0; Tail is = 0x0 Creating a fixed size stack of 10 nodes... Head is = 0x22c9010; Tail is = 0x22c9130 Size of the list is: 10 Peeking at data in tail... The value in top of stack is 10 Stack is not empty! This node contains 10 This node contains 9 This node contains 8 This node contains 7 This node contains 6 This node contains 5 This node contains 4 This node contains 3 This node contains 2 This node contains 1 Making a copy of the stack... Destroying the first Stack... Stack is empty! Head is = 0x0; Tail is = 0x0 Printing values of copy of stack... This node contains 10 This node contains 9 This node contains 8 This node contains 7 This node contains 6 This node contains 5 This node contains 4 This node contains 3 This node contains 2 This node contains 1 Copy of stack is not empty! Destroying copy of the stack... Copy of stack is empty! Starting a new stack using push and a loop... Enter the number of data to push onto the stack... 3 Enter the value to be pushed onto the stack... 1 Enter the value to be pushed onto the stack... 2 Enter the value to be pushed onto the stack... 3 Head is = 0x22c9150; Tail is = 0x22c9190 The size of the stack is: 3 Enter the number of data to be popped off of the stack... 3 The value being popped off of the stack is 3 The value being popped off of the stack is 2 The value being popped off of the stack is 1 Head is = 0x0; Tail is = 0x0 The size of the stack is: 0 lab46:~/src/data/Stack$
Addition of the stack library and test file to the Subversion repository.
lab46:~/src/data$ svn add Stack A Stack A Stack/copyStack.c A Stack/createFixedStack.c A Stack/createFixedStack.h A Stack/destroyFixedStack.c A Stack/destroyFixedStack.h A Stack/isEmpty.c A Stack/copyStack.h A Stack/isEmpty.h A Stack/listStackSize.c A Stack/listStackSize.h A Stack/makeStack.bat A Stack/notes A Stack/peek.c A Stack/peek.h A Stack/pop.c A Stack/pop.h A Stack/push.c A Stack/push.h A Stack/stackTest.c A (bin) Stack/stackTest.exe A Stack/struct.h A Stack/makeStack.sh A (bin) Stack/stackTest lab46:~/src/data$ cd Stack lab46:~/src/data/Stack$ svn add *.a A (bin) libdll.a lab46:~/src/data/Stack$ svn add *.o A (bin) copyStack.o A (bin) createFixedStack.o A (bin) destroyFixedStack.o A (bin) isEmpty.o A (bin) listStackSize.o A (bin) peek.o A (bin) pop.o A (bin) push.o lab46:~/src/data/Stack$ svn add *.exe svn: warning: 'stackTest.exe' is already under version control lab46:~/src/data/Stack$
**Comital of stack library and test file to the Subversion repository.**\\ lab46:~/src/data$ svn commit -m "commital of stack library and test file to the Subversion repository -John Rine" Adding data/Stack Adding data/Stack/copyStack.c Adding data/Stack/copyStack.h Adding (bin) data/Stack/copyStack.o Adding data/Stack/createFixedStack.c Adding data/Stack/createFixedStack.h Adding (bin) data/Stack/createFixedStack.o Adding data/Stack/destroyFixedStack.c Adding data/Stack/destroyFixedStack.h Adding (bin) data/Stack/destroyFixedStack.o Adding data/Stack/isEmpty.c Adding data/Stack/isEmpty.h Adding (bin) data/Stack/isEmpty.o Adding (bin) data/Stack/libdll.a Adding data/Stack/listStackSize.c Adding data/Stack/listStackSize.h Adding (bin) data/Stack/listStackSize.o Adding data/Stack/makeStack.bat Adding data/Stack/makeStack.sh Adding data/Stack/notes Adding data/Stack/peek.c Adding data/Stack/peek.h Adding (bin) data/Stack/peek.o Adding data/Stack/pop.c Adding data/Stack/pop.h Adding (bin) data/Stack/pop.o Adding data/Stack/push.c Adding data/Stack/push.h Adding (bin) data/Stack/push.o Adding (bin) data/Stack/stackTest Adding data/Stack/stackTest.c Adding (bin) data/Stack/stackTest.exe Adding data/Stack/struct.h Transmitting file data ................................ Committed revision 14. lab46:~/src/data$
Added all.h to the Subversion repository
lab46:~/src$ svn add all.h
Added all.h to the Subversion repository
lab46:~/src$ svn add "makeStack.sh on Linux (all.h).txt"
Added other files to the repository
lab46:~/src$ svn add "makeStack.bath on Windows (all.h).txt"
lab46:~$ svn commit -m "Updated stack library to use a common .h file (all.h) rather than separate .h files -John T. Rine"
Cleaned up stack library files
lab46:~/src/data/Stack$ svn commit -m "Cleaned up stack library -John T. Rine" Sending Stack/all.h Sending Stack/copyStack.c Sending Stack/createFixedStack.c Sending Stack/destroyFixedStack.c Sending Stack/isEmpty.c Sending Stack/libdll.a Sending Stack/listStackSize.c Sending Stack/makeStack.bat Sending Stack/peek.c Sending Stack/pop.c Sending Stack/push.c Sending Stack/stackTest Sending Stack/stackTest.c Sending Stack/stackTest.exe Transmitting file data .............. Committed revision 22. lab46:~/src/data/Stack$
I learned to use a single .h file to prototye all of my functions and the node structure for all of my library files. Below is a record of the steps I went through to get there.
I successfully created and used a static linked list library, or so I thought, when I finalized my linked list project. However, when it came to implementing a static library of stack routines and associated data type, I had a horrible time. every library file wanted a node structure in it however when I linked the library files together, I got error messages! I had to figure out a way to have only one node struct reference that I could share with the library files. I appealed to the instructor for help!
Date: Wed, 12 Oct 2011 19:54:26 -0400 (EDT) From: John Rine <jr018429@lab46.corning-cc.edu> To: wedge@lab46.corning-cc.edu, haas@corning-cc.edu Subject: static library HELP!!!!! I am trying to finish my stack library and mange all the crap going on in my personal life and it ain't going very good. I think I got lucky compiling and linking the linked List library. I am having a time with the stack. I took a different approach with it. I am having many library files with names describing the functions inside. For example: createFixedStack.c createFixedStack.h listStackSize.c listStackSize.h destroyFixedStack.c destroyFixedStack.h I also have a test program named: stackTest.c please look closely at the #include "..." in the files, also look closely at the way I created the static library. What am I doing wrong????? I can't find any good examples on the Internet. My issues are compounded by having a struct. please look closely at how the struct is included in the includes. I took copious notes. From you class, I got: main #include "mything.h" how does "mything.h in the test program relate to multiple include files?? I also got the following: ar rcs libdll.a *.o How does libdll relate to "mything.h" in the test program? I am missing something!!!! in stackTest.c I am declaring the individual .h files: #include "createFixedStack.h" #include "destroyFixedStack.h" #include "listStackSize.h" I successfully created .o files for these libraries using the following: lab46:~/Stack$ gcc -c createFixedStack.c -o createFixedStack.o lab46:~/Stack$ gcc -c destroyFixedStack.c -o destroyFixedStack.o lab46:~/Stack$ gcc -c listStackSize.c -o listStackSize.o Then I can successfully create a static library using: lab46:~/Stack$ ar rcs libStackJRdll.a *.o Code and includes //createFixedStack.h //John T. Rine //October 12, 2011 #ifndef _CREATEFIXEDSTACK_JR_H #define _CREATEFIXEDSTACK_JR_H #include<stdio.h> #include<stdlib.h> struct Node { int data; struct Node * prev; struct Node * next; }; typedef struct Node node; void createFiixedStackList(node **, node **, int); #endif //createFixedStack.c //John T. Rine //October 12, 2011 #include<stdlib.h> #include<stdio.h> #include"createFixedStack.h" void createFixedStack(node **head, node **tail, int elements) { int i; node *new; new = NULL; *head = *tail = NULL; for(i = 1; i<=elements; i++) { if (*head == NULL) { *head = (node *) malloc(sizeof(node)); *tail = *head; (*head)->prev = NULL; (*head)->next = NULL; } else { new = (node *) malloc(sizeof(node)); new->prev = *tail; new->next = NULL; (*tail)->next = new; *tail = new; } } } //destroyFixedStack.h //John T. Rine //October 12, 2011 #ifndef _DESTROYFIXEDSTACK_JR_H #define _DESTROYFIXEDSTACK_JR_H #include<stdio.h> #include<stdlib.h> struct Node { int data; struct Node * prev; struct Node * next; }; typedef struct Node node; void destroyFixedStackHead(node *); void destroyFixedStackTail(node *); #endif //destroyFixedStack.c //John T. Rine //October 12, 2011 #include<stdlib.h> #include<stdio.h> #include "destroyFixedStack.h" void destroyFixedStackHead(node *head) { node *temp; node *temp2; temp = NULL; temp2 = NULL; temp = head; while(temp != NULL) { temp2 = temp->next; if (temp->prev != NULL) temp->prev = NULL; if (temp->next != NULL) temp->next = NULL; free(temp); temp = temp2; } } void destroyFixedStackTail(node *tail) { node *temp; node *temp2; temp = NULL; temp2 = NULL; temp = tail; while(temp != NULL) { temp2 = temp->prev; if (temp->prev != NULL) temp->prev = NULL; if (temp->next != NULL) temp->next = NULL; free(temp); temp = temp2; } } //listStackSize.h //John T. Rine //October 12, 2011 #ifndef _LISTSTACKSIZE_JR_H #define _LISTSTACKSIZE_JR_H #include<stdio.h> #include<stdlib.h> struct Node { int data; struct Node * prev; struct Node * next; }; typedef struct Node node; int listStackSizeHead(node *); int listStackSizeTail(node *); #endif //listrStackSize.c //John T. Rine //October 12, 2011 #include<stdlib.h> #include<stdio.h> #include"listStackSize.h" int listStackSizeHead(node *head) { int size = 0; while (head != NULL) { head = head->next; size++; } return size; } int listStackSizeTail(node *tail) { int size = 0; while (tail != NULL) { tail = tail->prev; size++; } return size; } //stackTest.c //Author: John T. Rine //Date: October 12, 2011 #include "createFixedStack.h" #include "destroyFixedStack.h" #include "listStackSize.h" int main(int argc, char **argv) { node *head, *tail; head = tail = NULL; printf("Creating a list of 10 nodes...\n"); createFixedStack(&head, &tail, 10); getchar(); //Holds the console window open on a Windows machine } However when I try to create the test executable, it bombs-HELP!!! John T. Rine Graduate Electrical Technology-Electronics Graduate General Studies --- Lab46: Penguin Powered
I finally figured that I could create a struct .h file to define the node structure and then include that .h file in my lother library files. Sure enough, it worked!
Date: Thu, 13 Oct 2011 10:40:10 -0400 From: Matthew Haas <wedge@lab46.corning-cc.edu> To: John Rine <jr018429@lab46.corning-cc.edu> Subject: Re: static library HELP!!!!! (fwd) On Oct 12, 2011, at 11:34 PM, John Rine wrote: I got it. The issue is that the structure used as a node is shared throughout all of the functions. When you break them up, you can't include the structure in every .h file, so what you do is to creat e a structure.h file and include that in the broken up files. Its seems you also need it in the test file as well. Man did that was driving me crazy.
</cli> My instructor pointed out, however, that I could have simply made one .h file for all of my library files! Why didn't I think of that?
Date: Thu, 13 Oct 2011 10:40:10 -0400 From: Matthew Haas <wedge@lab46.corning-cc.edu> To: John Rine <jr018429@lab46.corning-cc.edu> Subject: Re: static library HELP!!!!! (fwd) Parts/Attachments: 1 OK ~61 lines Text 2 Shown ~62 lines Text ---------------------------------------- On Oct 12, 2011, at 11:34 PM, John Rine wrote: I got it. The issue is that the structure used as a node is shared throughout all of the functions. When you break them up, you can't include the structure in every .h file, so what you do is to creat e a structure.h file and include that in the broken up files. Its seems you also need it in the test file as well. Man did that was driving me crazy. John, You may have appeared to resolve the issue, but I think the situation is still inordinately complex and can be simplified. For your stack library, you should be able to get away with just 1 header file, let's call it: stack.h stack.h can look as follows: ------------------------------------- #ifndef _STACK_H #define _STACK_H #include "linkedlist.h" struct stack { struct list *data; struct node *top; }; typedef struct stack Stack; Node *pop(Stack *); int push(Stack *, Node *); Node *peek(Stack *); int isEmpty(Stack *); #endif ------------------------------------- All your .c files can just #include "stack.h", which will take care of all the function prototypes, struct definition, and even pull in the linked list stuff for you (the linkedlist.h file will similarly have #ifndef _LINKED_LIST_H guards). With so many header files, the way you seem to be approaching it might be creating a lot more managerial work for yourself... let the preprocessor take care of it for you. Is this code on Lab46? I took a quick peek and couldn't find it. -Matthew -- Matthew Haas Lab46 System Administrator Instructor of Computer & Information Science http://lab46.corning-cc.edu/haas/home/ [lab46.corning-cc.edu]
In performing this project, the following resources were referenced:
Copyright © 1993-2000 by Robert I. Pitts</a></html>