CS 3723
 Programming Languages 
   Stacks: C, C++, and Java  
One File  


A stack program: This example is showing the earlier stack program implemented in each case in a single file. The example illustrates that the C++ and Java implementations are very similar to what they were before, and they have many of the same advantages over C. On the other hand, the C implementation is very much the worse for being all in one file. The data fields are no longer hidden and inaccessible (in a separate file in the C case). If there were private member functions, they also would now be accessible to the whole program. In contrast, in the single-file implementation using C++ and Java, private data members and private member functions are just as inaccessible as they were when separate files were employed. This illustrates how essential separate files are to the enabling of object-oriented programming in C.

Stacks in One File
C Stack: stack_one.c C++ Stack: stack_one.cpp Java Stack: StackOne.java
/* stack_one.c: one file */
#include <stdio.h>







#define MAXSTACK 10
#define EMPTYSTACK -1
int top = EMPTYSTACK;
char items[MAXSTACK];




void push(char c) {
   items[++top] = c;
}

char pop() {
   return items[top--];
}

int full()  {
   return top+1 == MAXSTACK;
}

int empty()  {
   return top == EMPTYSTACK;
}



int main() {


   char ch;
   while ((ch = getchar())
         != '\n')
      if (!full()) push(ch);
   while (!empty())
      printf("%c", pop());
   printf("\n");
}
// stack_one.cpp: in one file
#include <iostream.h>
class Stack {
   int MaxStack;
   int EmptyStack;
   int top;
   char* items;
public:
   Stack(int size) {
      MaxStack = size;
      EmptyStack = -1;
      top = EmptyStack;
      items = new char[MaxStack];
   }

   ~Stack() { delete[] items; }

   void push(char c) {
      items[++top] = c;
   }

   char pop() {
      return items[top--];
   }

   int full()  {
      return top + 1 == MaxStack;
   }

   int empty()  {
      return top == EmptyStack;
   }
};


int main() {

   Stack s(10); // 10 chars
   char ch;
   while ((ch = cin.get())
         != '\n')
      if (!s.full()) s.push(ch);
   while (!s.empty())
      cout << s.pop();
   cout << endl;
}
// StackOne.java: one file
import java.io.*;
class Stack {
   private int maxStack;
   private int emptyStack;
   private int top;
   private char[] items;

   public Stack(int size) {
      maxStack= size;
      emptyStack = -1;
      top = emptyStack;
      items = new char[maxStack];
   }

   

   public void push(char c) {
      items[++top] = c;
   }

   public char pop() {
      return items[top--];
   }

   public boolean full()  {
      return top + 1 == maxStack;
   }

   public boolean empty()  {
      return top == emptyStack;
   }
}

public class StackOne {
  public static void main(String[] args)
       throws IOException {
    Stack s = new Stack(10); // 10 chars
    char ch;
    while ((ch = (char)System.in.read())
          != '\n')
       if (!s.full()) s.push(ch);
    while (!s.empty())
       System.out.print(s.pop());
    System.out.println();
  }
}
Execution of each program
% cc -o stack_one stack_one.c
% stack_one
mississippi
ppississim
% CC -o stack_one_cpp stack_one.cpp
% stack_one_cpp
mississippi
ppississim
% javac StackOne.java
% java StackOne
mississippi
ppississim


Revision date: 2013-02-16. (Please use ISO 8601, the International Standard Date and Time Notation.)