This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:bkenne11:portfolio:eocepalindrome [2011/12/15 00:56] – [Code] bkenne11 | user:bkenne11:portfolio:eocepalindrome [2011/12/15 03:04] (current) – [Attributes] bkenne11 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | A project for Data Structures by Brandon Kennedy during the Fall 2011 Semester. | ||
+ | |||
+ | This project was begun on 12-7-2011 and is anticipated to take 1 day. | ||
+ | =====Objectives===== | ||
+ | The purpose of this project is to broaden my understanding of using the push, pop and peek functions of a stack and to use those functions from a pre-existing library I made. | ||
+ | =====Prerequisites===== | ||
+ | In order to successfully accomplish/ | ||
+ | |||
+ | * an understanding of the functionality of a stack structure | ||
+ | * an udnerstanding of a linked list structure | ||
+ | * matt haas | ||
+ | * the c programming guide | ||
+ | * an understanding of file I/0 processing | ||
+ | =====Background===== | ||
+ | Through this project I hope to better understand how libraries of functions can be made more general. That way i can use the same library in as many different programs as I want. This code is taken from my Data Structures EoCE where it was used for the same purpose, as a palindrome project. | ||
+ | =====Scope===== | ||
+ | This will focus specifically on character based strings that will be tested for palindromality. It will use a stack to flip the string around and compare character by character. | ||
+ | =====Attributes===== | ||
+ | State and justify the attributes you'd like to receive upon successful approval and completion of this project. | ||
+ | |||
+ | * attribute1: pointers -> this program will use pointers to nodes and arrays of characters. | ||
+ | * attribute2: stacks -> this program will implement a stack. | ||
+ | * attribute3: File I/0 -> this program will record to a file. | ||
+ | * attribute4: terminal I/0 -> this program will output to the terminal -> used for systems programming | ||
+ | =====Code===== | ||
+ | ===main=== | ||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | # | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | FILE *fptr; | ||
+ | int i = 0; | ||
+ | int j = 0; | ||
+ | char *palin; | ||
+ | palin = malloc(sizeof(char)*(20)); | ||
+ | Stack *mystack; | ||
+ | mystack = (Stack *) malloc (sizeof(Stack)); | ||
+ | mystack -> top = mystack -> bottom = mystack -> tmp = NULL; | ||
+ | fptr = fopen(" | ||
+ | printf(" | ||
+ | scanf(" | ||
+ | fprintf(fptr, | ||
+ | printf(" | ||
+ | i = 0; | ||
+ | while(*(palin+i) != ' | ||
+ | { | ||
+ | mystack = push(mystack, | ||
+ | i++; | ||
+ | } | ||
+ | printf(" | ||
+ | fprintf(fptr, | ||
+ | mystack -> tmp = mystack -> top; | ||
+ | while(mystack -> tmp != NULL) | ||
+ | { | ||
+ | fprintf(fptr, | ||
+ | printf(" | ||
+ | mystack -> tmp = mystack -> tmp -> next; | ||
+ | } | ||
+ | mystack -> tmp = mystack -> top; | ||
+ | i = 0; | ||
+ | while(mystack -> tmp != NULL) | ||
+ | { | ||
+ | if(mystack -> tmp -> value != *(palin+i)) | ||
+ | { | ||
+ | fprintf(fptr, | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | mystack -> tmp = mystack -> tmp -> next; | ||
+ | i++; | ||
+ | } | ||
+ | } | ||
+ | fprintf(fptr, | ||
+ | printf(" | ||
+ | fclose(fptr); | ||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | ===push.c=== | ||
+ | <code c> | ||
+ | # | ||
+ | |||
+ | Stack *push(Stack *notes, int val) | ||
+ | { | ||
+ | Node *stick; | ||
+ | stick=(Node *)malloc(sizeof(Node)); | ||
+ | stick -> value = val; | ||
+ | |||
+ | if(notes -> top == NULL) | ||
+ | { | ||
+ | notes -> top = notes -> bottom = stick; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | notes -> top -> prev = stick; | ||
+ | stick -> next = notes -> top; | ||
+ | notes -> top = notes -> top -> prev; | ||
+ | } | ||
+ | return notes; | ||
+ | } | ||
+ | </ | ||
+ | ===stack.h=== | ||
+ | <code c> | ||
+ | #ifndef _STACK_H_ | ||
+ | #define _STACK_H_ | ||
+ | # | ||
+ | |||
+ | struct stack{ | ||
+ | struct node *top; | ||
+ | struct node *bottom; | ||
+ | struct node *tmp; | ||
+ | }; | ||
+ | typedef struct stack Stack; | ||
+ | |||
+ | int peek(Stack *); | ||
+ | Node *pop(Stack *); | ||
+ | Stack *push(Stack *, int); | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | ===linkedlist.h=== | ||
+ | <code c> | ||
+ | #ifndef _LINKED_LIST_H | ||
+ | #define _LINKED_LIST_H | ||
+ | # | ||
+ | # | ||
+ | |||
+ | struct node { | ||
+ | char value; | ||
+ | struct node *next; | ||
+ | struct node *prev; | ||
+ | }; | ||
+ | typedef struct node Node; | ||
+ | |||
+ | struct list{ | ||
+ | struct node *start; | ||
+ | struct node *end; | ||
+ | struct node *tmp; | ||
+ | }; | ||
+ | typedef struct list List; | ||
+ | |||
+ | List *copy(List *); | ||
+ | List *append(List *, Node *, int); | ||
+ | List *insert(List *, Node *, int); | ||
+ | Node *remov(List *, Node *); | ||
+ | void deletelist(List *); | ||
+ | Node *find(List *, int); | ||
+ | |||
+ | #endif | ||
+ | </ | ||
+ | =====Execution===== | ||
+ | Again, if there is associated code with the project, and you haven' | ||
+ | |||
+ | <cli> | ||
+ | lab46: | ||
+ | Enter string to check: hereh | ||
+ | Before: hereh | ||
+ | After: hereh | ||
+ | hereh is a palindrome! | ||
+ | lab46: | ||
+ | Before testing: hereh | ||
+ | After testing: hereh | ||
+ | Therefore hereh is a palindrome! | ||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | =====Reflection===== | ||
+ | From this project I really just brushed up on being able to stick simple file I/0 into a program so that is records its output. This can be handy when needing to process a large amount of data dn have it print some things to the screen while recording other stuff to files. I also worked with arrays in combination with stacks to make my data structure usage versatile. | ||
+ | =====References===== | ||
+ | In performing this project, the following resources were referenced: | ||
+ | |||
+ | * wiki documents on file I/O | ||
+ | * Matt Haas | ||
+ | * the C programming guide |