User Tools

Site Tools


user:nbrimme1:portfolio:dataproject4

Data Project 4: Stack

A project for COURSENAME by YOUR NAME OR GROUPMEMBER NAMES during the SEMESTER YEAR.

This project was begun on DATE and is anticipated to take X AMOUNT OF TIME. (Upon completion you can correct this with the actual length).

Objectives

State the purpose of this project. What is the point of this project? What do we hope to accomplish by undertaking it?

Prerequisites

In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • resource1
  • resource2
  • resource3
  • experience1
  • experience2
  • etc.

Background

State the idea or purpose of the project. What are you attempting to pursue?

Upon approval, you'll want to fill this section out with more detailed background information. DO NOT JUST PROVIDE A LINK.

Providing any links to original source material, such as from a project page, is a good idea.

You'll want to give a general overview of what is going to be accomplished (for example, if your project is about installing a web server, do a little write-up on web servers. What is it, why do we need one, how does it work, etc.)

Scope

Give a general overview of your anticipated implementation of the project. Address any areas where you are making upfront assumptions or curtailing potential detail. State the focus you will be taking in implementation.

I have had previous experience with both Stacks and Queues from a different program at another college (Not with C on UNIX; but with the Motorolla MC68HC12 (.pdf) Microcontroller. Also, Matt provided almost all of the code needed for the stack (except the peek() function); making the implementation simple.

Attributes

State and justify the attributes you'd like to receive upon successful approval and completion of this project.

  • attribute1: why you feel your pursuit of this project will gain you this attribute
  • attribute2: why you feel your pursuit of this project will gain you this attribute
  • etc…

Procedure

Matt was so nice, kind, and generous that he wrote ALMOST the entire stack program. The only function that needed to be written was peek. Using Matt's existing code as a “template”, the peek function was easy to implement. The same code also made the Queue easy to implement.

Code

Matt provided the code for the stack. I added some comments; Matt must hate comments. The required header files were also copied to the same directory.

node.h

/* If not defined. Prevents the header file from
being declared twice. */
#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


list.h

/* If not defined. Prevents the header file from
being declared twice. */
#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


stack.h

/* If not defined. Prevents the header file from
being declared twice. */
#ifndef _STACK_H
#define _STACK_H
 
// Doubley Linked list header file.
#include "list.h"
 
struct stack {
        // Pointer to the data in the List.
	List *data;
        // Pointer to the top of the Stack.
	Node *top;
        // Size of the stack.
	int size;
};
typedef struct stack Stack;
 
Stack *mkstack(int);
Stack *push   (Stack *, Node *);
/* Need to update the stack. Double pointer passing
stack by double reference rather than value. It is
passing the function a pointer to a pointer. */
Node  *pop    (Stack **);
Node  *peek   (Stack *);
 
#endif


stackops.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);
}


main.c: Nearly identical to the stack testing program: /var/public/data/fall2013/linkedlistimp/testing/stacktest.c

/*
Stack Testing Program.
 
Compile doubley linked list.
Compile stack.
Compiile main.c
	gcc -o main main.c -l dll -l stack
*/
 
#include <stdio.h>
#include "stack.h"
 
int main()
{
	Node *tmp;
	Stack *myStack;
	myStack = mkstack(0);
	tmp = create();
	// Second fgetc eliminates the naturally occuring \n char.
	tmp->value = fgetc(stdin);
	fgetc(stdin);
 
	while (tmp->value != "\n")
	{
		myStack = push(myStack, tmp);
		tmp = create();
		// Second fgetc eliminates the naturally occuring \n char.
		tmp->value = fgetc(stdin);
		fgetc(stdin);
 
	}
 
	printf("Linked List has %d nodes.\n", myStack->data->qty);
	printf("String is: ");
 
	do
	{
		tmp = pop(&myStack);
		if (tmp != NULL)
		{
			printf("%c", tmp->value);
		}
	} while (tmp != NULL);
	printf("\n")
 
return (0);
}


peek.c

#include "stack.h"
 
Node *peek(Stack *myStack)
{
  // The peek() function only returns the value at the top of the stack.
  return(myStack->top);
}


pop.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);
}


push.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);
}

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/cprog$ ./hello
Hello, World!
lab46:~/src/cprog$ 

Reflection

Comments/thoughts generated through performing the project, observations made, analysis rendered, conclusions wrought. What did you learn from doing this project?

References

In performing this project, the following resources were referenced:

  • URL1
  • URL2
  • URL3 (provides useful information on topic)
  • URL4

Back to my Portfolio
Back to my Opus

user/nbrimme1/portfolio/dataproject4.txt · Last modified: 2013/12/09 22:25 by nbrimme1