Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

push,pop, & print  a stack

Posted on 1998-07-30
4
Medium Priority
?
455 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.

609 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