/* 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();
}
}
|
% 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
|