Posted on 2004-09-21
Medium Priority
Last Modified: 2010-04-01
My apologies for the re-post, I need help on this.  I just got back online.  Del

# Makefile:
#        It uses the C++ Compiler with all warnings and
#        full debugging; will create a single executable called 'main'
# ---------------------------------------------------------------
# Note: If you want to use the g++ compiler on pegasus or helios
# uncomment the g++ lines and comment the Digital Unix lines

# the Digital Unix C++ compiler needs these lines:
CPP = cxx
CFLAGS = -L/usr/lib/cmplrs/cxx -DPOSIX_4D9 -w0 -gall

# the g++ compiler on pegasus and helios needs these lines:
# CPP = g++
# CFLAGS = -DPOSIX_4D9 -w -g

# link in the math library
LFLAGS = -lm

RM = rm -f
# ----------------------------------------------------------------
# Explanation of macros:
#     $< is any dependent file which is out of file1
#     $* is the target's basename (without extension)
#     $@ is the target's fullname
# add suffix .cpp since it is not a default with make util
.SUFFIXES:     .cpp .o
# implicit rule for compilation only:
     ${CPP} -c ${CFLAGS} $<

OFILES=          main.o life.o util.o

HFILES=          life.h  util.h

# dependencies
default:     main    
main:           $(OFILES)
          ${CPP} ${CFLAGS} ${LFLAGS} $(OFILES) -o $@

main.o:          main.cpp life.h util.h
life.o:          life.cpp life.h util.h
util.o:          util.cpp util.h

     ${RM} *.o
     ${RM} core
veryclean:     clean
     ${RM}  main  

// main.cpp
#include <stack>
#include <string>
#include <iostream>

using namespace std;

#define SIZE 100
typedef char item_t;

typedef struct {
     item_t Items[SIZE];
     int top;

typedef struct{
     int count,front, rear;
     item_t Item[SIZE];

void InitialiseStack (Stack_t *S)
{     S->top=0;     }

int emptystack (Stack_t S)
{     return (S.top==0);     }

int fullstack (Stack_t S)
{     return (S.top==SIZE);     }

void push (char x,Stack_t *S)
          printf("stack overflow\n");

void pop (Stack_t *S, char *x)
          printf("stack empty\n");

template <class T> void stackMenu(T data);
void stackMenu();

int main()
//   int option;
   Stack_t S;
     // Queue_t Q;
   item_t input[SIZE] = { 0 };
   item_t Q_output[SIZE]="",S_output[SIZE]="";
     int i,length;


     printf("Enter a words to reverse> ");
          // insert(input[i],&Q);

     length=strlen(input)-1;   // we don't need final '/n'

               // Remove(&Q,&Q_output[i]);
          // }
               printf("\nThe words are not palindrome\n");
               return 0;

          else /* if(Q_output[i]==S_output[i]) */
               cout << S_output[i];
     return 0;

void stackoptions()
   cout << "1: Push\n";
   cout << "2: Pop\n";
   cout << "3: Top\n";
   cout << "4: Quit\n\n";
   cout << "Enter Option: ";

template <class T> void stackMenu(T data)
   stack<T> mystack;  // create the right stack
   int option;
   cin >> option;

   while (option != 4)
      if (option == 1)
            cout << "enter data";
            cin >> data;
            cout << "Stack Added Value " << data << " to Top\n";
      if (option == 2)
            if (!mystack.empty())
               cout << "Stack Successfully Poped\n";
               cout << "Stack is Empty, Cannot Pop\n";
      if (option == 3)
            if (!mystack.empty())
               cout << "Value at Top is: " << mystack.top() << "\n";
               cout << "Stack is Empty, Cannot Top\n";
      if (option > 3 || option < 1)
            cout << "Invalid Option\n";
      cin >> option;

This is the program behvior needed:


                Enter a palindrome: madam

                The word reversed is: madam

                Enter a list of integers followed by -1: 1 2 3 4 5 -1

                The sum of the numbers on the stack is: 15


Question by:edelossantos
  • 3
  • 2
LVL 10

Accepted Solution

Sys_Prog earned 2000 total points
ID: 12118200
FRom your requirement, it seems that u require a generic stack...thus using templates......but u should be using templates for the stack class itself

I would start something like this

template <class T>
class Stack {
    private :
        T stk ;
        int top ;
    public :
            Stack () ;
            T pop () ;
            int push ( T stack ) ;
            bool is_stack_empty () ;
            bool is_stack_full () ;            
} ;  

LVL 10

Assisted Solution

Sys_Prog earned 2000 total points
ID: 12118209
While creating an insteance for Stack of type char, u say

Stack <char> char_stk ;

While creating an insteance for Stack of type int, u say

Stack <int> int_stk ;


Author Comment

ID: 12118216
Hello, Amit.  Good to hear from you.  Del
LVL 10

Expert Comment

ID: 12119247
Same here Del....Did u get what i try to say


Author Comment

ID: 12119519
yes I did.  Thank you.  Del

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

597 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