Link to home
Start Free TrialLog in
Avatar of sdickens
sdickens

asked on

push,pop, & print a stack

I have been working on this program for 3 days and can't get anywhere.  I have to interactively ask if the user wants to add, or remove from the stack and print the contents.  I can push function seems to be working, but when I print, I only get the top number on the stack when I need to print the entire contents.  Here is my code so far:
#include<stdio.h>
#include<stdlib.h>
typedef struct node node_t;
typedef node_t *ptr_t;
struct node
   {
    int n;
    ptr_t next;
   };
ptr_t head=NULL;
void pop(void);
void push(void);
ptr_t newnode(void);
void choice(void);
void error(void);
void printstk(void);

void main(void)
{
   choice();
}

void choice(void)
{

   int choice;
   do
   {
   printf("This program will allow you to add or remove the first item\n");
   printf("of the stack.\n1-Add item\n2-Remove item\n3-Quit\nYour choice: ");
   scanf("%d",&choice);

    switch (choice)
       { case 1:
            push();
            printstk();
            break;
       case 2:
            pop();
            break;
       case 3:
            printf("Thank You");
       default:
           printf("Please enter your choice again");
       }
    }
   while (choice!=3);
}

ptr_t newnode(void)
{
    return (ptr_t)calloc(1,sizeof(node_t));
}
void push(void)
{
      int ans;
      ptr_t p;
      do
       {
      printf("\nEnter a number: ");
      scanf("%d",&p->n);
      p->next=NULL;
      head=p;
      printf("Enter more?\n1--Yes\n2--No\nChoice: ");
      scanf("%d",&ans);
       }
     while (ans==1);
}


void printstk(void)
{
      ptr_t p;
      p=head;
      while (p!=NULL)
         {
           printf("%d\t",p->n);
           p=p->next;
           free(p);
         }
}

void pop(void)
{
      ptr_t p;
      head=head->next;
      free(p);
     printstk();
}

Thanks!!!
ASKER CERTIFIED SOLUTION
Avatar of alexo
alexo
Flag of Antarctica image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
However, please allow me to say that your program exhibits unorthodox software engineering principles.

I understand that this is just a limited scope homework but a good advice never hurt anybody at any stage.

It is usually accepted that the functionality of the program (the "engine" or "back end") should be separated from the UI (the "front end").  That means, in your case, that the push() and pop() functions should function the same when called from an interactive or a batch program.

Also, you are limiting yourself to just one stack in the program because push() and pop() refer to a global variable (a major faux pas!)

The prototypes should look something like:
    void push(ptr_t* pHead, int number); /* Push a number on a given stack */
    int pop(ptr_t& pHead); /* Pop a number from a given stack and return it */

Have fun!

Damn butter fingers!

    int pop(ptr_t* pHead);

Ahhh...  Much better.
Avatar of sdickens
sdickens

ASKER

Thanks alot for the help and advice.