Table of Contents

Project: Palindrome

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/perform this project, the listed resources/experiences need to be consulted/achieved:

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.

Code

main

#include <stdlib.h>
#include <stdio.h>
#include"stack.h"
 
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("palindromerecords", "w");
        printf("Enter string to check: ");
        scanf("%s", palin);
        fprintf(fptr, "Before testing: %s\n", palin);
        printf("Before: %s\n", palin);
        i = 0;
        while(*(palin+i) != '\0')
        {
                mystack = push(mystack, *(palin+i));
                i++;
        }
        printf("After: ");
        fprintf(fptr, "After testing: ");
        mystack -> tmp = mystack -> top;
        while(mystack -> tmp != NULL)
        {
                fprintf(fptr, "%c", mystack -> tmp -> value);
                printf("%c", mystack -> tmp -> value);
                mystack -> tmp = mystack -> tmp -> next;
        }
        mystack -> tmp = mystack -> top;
        i = 0;
        while(mystack -> tmp != NULL)
        {
                if(mystack -> tmp -> value != *(palin+i))
                {
                        fprintf(fptr, "\nTherefore %s is NOT a palindrome\n", palin);
                        printf("\n%s is NOT a palindrome\n", palin);
                        exit(1);
                }
                else
                {
                        mystack -> tmp = mystack -> tmp -> next;
                        i++;
                }
        }
        fprintf(fptr, "Therefore %s is a palindrome!", palin);
        printf("\n%s is a palindrome!\n", palin);
        fclose(fptr);
        return 0;
}

push.c

#include"stack.h"
 
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

#ifndef _STACK_H_
#define _STACK_H_
#include"linkedlist.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

#ifndef _LINKED_LIST_H
#define _LINKED_LIST_H
#include<stdlib.h>
#include<stdio.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't already indicated how to run it, provide a sample run of your code:

lab46:~/src/DataS/EoCE$ ./palindrome
Enter string to check: hereh
Before: hereh
After: hereh
hereh is a palindrome!
lab46:~/src/DataS/EoCE$ cat palindromerecords
Before testing: hereh
After testing: hereh
Therefore hereh is a palindrome!
lab46:~/src/DataS/EoCE$

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: