Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 456
  • Last Modified:

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!!!
0
sdickens
Asked:
sdickens
  • 3
1 Solution
 
alexoCommented:
You were *this* close to getting it to work.
Please compare the following piece of code with your version:

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


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

    void pop(void)
    {
        ptr_t p = head;
        if (p!=NULL)
        {
            head=head->next;
            free(p);
        }
        printstk();
    }
0
 
alexoCommented:
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!

0
 
alexoCommented:
Damn butter fingers!

    int pop(ptr_t* pHead);

Ahhh...  Much better.
0
 
sdickensAuthor Commented:
Thanks alot for the help and advice.  
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now