======Project: Stack library======
A project for C/C++, Data Structures, and System Programming by Karl Krauss for fall 2011.
Approximately 3 hours including taking the time to full grasp all concepts that were used, this was much simpler than doubly linked list as the "hard" parts had been figured out already.
=====Objectives=====
The purpose of this project was to create a library for stacks.
=====Prerequisites=====
In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:
* understand functions
* understand pointers
* understand structs
* understand malloc()
=====Background=====
This was the next evolution of the doubly linked list library. This is the stack library, taking what I learned from implementing the dl library this was much simpler.
=====Attributes=====
====Cprog attributes====
* variables
* pointers
* selection
* i/o
* repetition
* functions
* structures
* libraries
====Data Structures====
* Pointers
* Malloc/new
* linked list
* Doubly linked list
* Libraries
====Systems programming====
* Terminal I/O
=====Code=====
This is the header file:
#ifndef STACK_H
#define STACK_H
struct node { //predefined structure with one variable (
int value;
struct node *next;
struct node *prev;
};
typedef struct node Node;
struct stack {// predefined structure for use of returns in functions allow for easy access to stack. *end is top of stack
Node *start;
Node *end;
};
typedef struct stack Stack;
Stack *push(Stack *myStack, int input);
Node *pop(Stack *myStack);
Node *peek(Stack *myStack);
//minimum variables needed in main: int input, Node *tmp, Node *tmp2, List *myList.
#endif
This is the code:
typedef struct list List;
in List: * start will be the stack and *end the top of the stack.
*/
List *push(List *myList, Node *tmp, int input);
List *pop(List *myList, Node *tmp, int i);
void peek(List *myList);
int main()
{
List *myList;
//Node *stack, *top
Node *tmp;
int i=0, input;
//tmp = stack = top = NULL;
myList=(List *)malloc(sizeof(List));
myList->start =myList->end = tmp = NULL;
printf("Please enter a value (-1 to stop): ");//puts kept printing a new line
scanf("%d", &input);
while (input != -1)
{
push(myList, tmp, input);
i++;
printf("Please enter a value (-1 to stop): ");
scanf("%d", &input);
}
peek(myList);
getchar();
getchar();
while (i != 0)//tmp = (Node *)pop(myList, tmp, i)) != NULL)
{
myList=pop(myList, tmp, i);
i--;
}
return 0;
}
List *push(List *myList, Node *tmp, int input)// pushing a new value onto the top of the stack
{
myList = newNode(myList, tmp, input);
return myList;
}
List *pop(List *myList, Node *tmp, int i)// popping value off the top of the stack
{
int x;
tmp = myList->start;
for(x=1; xnext;
}
printf("value: %d\n", tmp -> value);
deleteNode(myList, tmp, i);
return myList;
}
void peek(List *myList)// checking to see whats on the top of the stack
{
if(myList->start == NULL)
{
printf("The stack is empty\n");
}
else
{
printf("THe value at the top of the stack is: %d\n", myList->end->value);
}
}
=====Reflection=====
*As with doubly linked lists, I really learned a lot. This library was written quite some time ago and the best part is when I came back to it, I had learned enough to realize I could vastly improve upon it. Something I will possibly do in the future.