Solved

push,pop, & print  a stack

Posted on 1998-07-30
4
444 Views
Last Modified: 2010-04-15
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
Comment
Question by:sdickens
  • 3
4 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
ID: 1251882
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
 
LVL 11

Expert Comment

by:alexo
ID: 1251883
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
 
LVL 11

Expert Comment

by:alexo
ID: 1251884
Damn butter fingers!

    int pop(ptr_t* pHead);

Ahhh...  Much better.
0
 

Author Comment

by:sdickens
ID: 1251885
Thanks alot for the help and advice.  
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now