User Tools

Site Tools


user:bkenne11:portfolio:libstack

Project: Stack Library Implementation

A project for Data Structures by Brandon Kennedy during the Fall 2011 semester.

This project was begun on 10-16 and took 2 days to complete.

Objectives

The purpose of this project is to implement a stack library. It will receive into in the form of characters, one at a time, and will prompt the user to edit the stack with appropriate stack functions

Prerequisites

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

  • basic knowledge on stacks
  • C++ pocket quide
  • the C programming guide

Background

The main purpose for creating this project is to explore stacks and their functionality. I have created 3 stack manipulation functions and a main function to run them all. they are:

  • pop()
  • push()
  • peek()
  • and the main()

Scope

I plan to implement this stack using character data. I will create a small, but expandable stack that can me manipulated with pop, peek, or push, and will all be wrapped in a library. I will also call another library and use one of it's struct to build nodes.

This library will also contain a struct, it will hold the top and bottom addresses of the stack.

Attributes

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

  • Stacks: This project will implement a stack
  • linked lists: this project will implement a linked list
  • doubly linked list: this project will not only implement a linked list, but a doubly linked list
  • pointers: this project will implement pointers.
  • Malloc/new: This project re-implements mallocs used in previous projects.
  • Libraries: This project will implement and utilize non-core libraries

Procedure

  1. Build pop function
  2. Build push function
  3. Build peek function
  4. build main function around other functions
  5. work out a few bugs, and be done!

Code

Main

#include"stack.h"
 
int main()
{
        char input;
        char junk;
        int choice;
        int i=0;
        int flag=0;
 
        Stack *mystack;
        mystack = (Stack *) malloc (sizeof(Stack));
        mystack -> top = mystack -> bottom = mystack -> tmp = NULL;
 
        printf("Enter a character, (# to exit): ");
        scanf("%c", &input);
 
        while(input != '#')
        {
                if(mystack -> top == NULL)
                {
                        mystack -> top = (Node *) malloc (sizeof(Node));
                        mystack -> bottom = mystack -> top;
                        mystack -> bottom -> next = NULL;
                        mystack -> top -> prev = NULL;
                        mystack -> tmp = mystack -> top;
                        mystack -> top -> value = input;
                }
                else
                {
                        mystack = push(mystack, input);
                }
                scanf("%c", &junk);
                printf("Enter a character, (# to exit): ");
                scanf("%c", &input);
        }
        printf("To pop a node, enter 1 \n");
        printf("To push a node, enter 2 \n");
        printf("To peek at the value of the top node, enter 3 \n: ");
        scanf("%d", &choice);
        if(choice == 1)
        {
 
                printf("You popped node memory address [0x%x]\n", pop(mystack));
        }
        else if(choice == 2)
        {
 
                scanf("%c", &junk);
                printf("Enter a character, (# to exit): ");
                scanf("%c", &input);
 
                mystack = push(mystack, input);
        }
        else if(choice == 3)
        {
                printf("The top node has a value of: %c \n", peek(mystack));
                flag = 1;
        }
        else
        {
                printf("Wrong choice, %d was not an option\n", input);
                exit(1);
        }
        if(flag == 0)
        {
                mystack -> tmp = mystack -> top;
                i=0;
                while(mystack -> tmp != NULL)
                {
                        printf("[%d] value: %c \n", i, mystack -> tmp -> value);
                        i++;
                        mystack -> tmp = mystack -> tmp -> next;
                }
        }
        return 0;
}

pop

#include"stack.h"
 
Node *pop(Stack *books)
{
        books -> tmp = books -> top;
        if(books -> top != books -> bottom)
        {
                books -> top = books -> top -> next;
                books -> top -> prev = NULL;
                books -> tmp -> next = NULL;
        }
        return(books -> tmp);
}

push

#include"stack.h"
 
Stack *push(Stack *notes, char val)
{
        Node *stick;
        stick = (Node *) malloc (sizeof(Node));
        stick -> value = val;
 
        notes -> top -> prev = stick;
        stick -> next = notes -> top;
        notes -> top = notes -> top -> prev;
        return notes;
}

peek.c

#include"stack.h"
 
char peek(Stack *stax)
{
        return(stax -> top -> value);
}

header file

#ifndef _STACK_H_
#define _STACK_H_
#include"linkedlist.h"
 
struct stack{
        struct node *top;
        struct node *bottom;
        struct node *tmp;
};
typedef struct stack Stack;
 
char peek(Stack *);
Node *pop(Stack *);
Stack *push(Stack *, char);
 
#endif

Execution

lab46:~/src/DataS/Project3$ ./mainstack
Enter a character, (# to exit): f
Enter a character, (# to exit): y
Enter a character, (# to exit): g
Enter a character, (# to exit): 3
Enter a character, (# to exit): #
To pop a node, enter 1
To push a node, enter 2
To peek at the value of the top node, enter3
1
You popped node memory address [0x1350090]
[0] value: g
[1] value: y
[2] value: f
lab46:~/src/DataS/Project3$ ./mainstack
Enter a character, (# to exit): 4
Enter a character, (# to exit): 6
Enter a character, (# to exit): h
Enter a character, (# to exit): d
Enter a character, (# to exit): #
To pop a node, enter 1
To push a node, enter 2
To peek at the value of the top node, enter3
2
Enter a character, (# to exit): s
[0] value: s
[1] value: d
[2] value: h
[3] value: 6
[4] value: 4
lab46:~/src/DataS/Project3$ ./mainstack
Enter a character, (# to exit): 4
Enter a character, (# to exit): 7
Enter a character, (# to exit): h
Enter a character, (# to exit): #
To pop a node, enter 1
To push a node, enter 2
To peek at the value of the top node, enter3
3
The top node has a value of: h
lab46:~/src/DataS/Project3$

Reflection

A stack is like a pile stickynotes, you can add on to the top, or remove the top one, but thats it! noone wants to mess with the brown thing on the bottom, so that is out of the question. Therefore, the first thing into a stack is the last thing out, verses a queue where the first thing in is the first thing out.

References

In performing this project, the following resources were referenced:

  • the C programming guide
  • C++ pocket reference
  • Cprogramming.com
user/bkenne11/portfolio/libstack.txt · Last modified: 2011/11/18 19:57 by bkenne11