Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python

C++


JAVA
PHP
SQL
Assignments

Hanoi-Stack


stack.cpp

// A stack class
// Charles A. Cusack, Sept. 2000
//
//-----------------------------------------------------
#include "stack.h"
//-----------------------------------------------------
stack::stack() {
    head=NULL; 
    }
//-----------------------------------------------------
stack::~stack() {
    while(!empty()) {
       node *X=head;
       head=head->next;
       delete X;
       }
    }
//-----------------------------------------------------
stack::stack(const stack &S) {
   if(S.head==NULL) {
      head=NULL;
      }
    else {
       head=new node;
       node *x=S.head;
       head->key=x->key;
       x=x->next;
       node* y=head;
       while(x!=NULL) {
          y->next=new node;
          y=y->next;
          y->key=x->key;
          x=x->next;
          }
       y->next=NULL;
    }
}
//-----------------------------------------------------
stack& stack::operator =(const stack &S) {
if(&S!=this) {
   while(!empty()) {
      node *X=head;
      head=head->next;
      delete X;
      }
   if(S.head==NULL) {
      head=NULL;
      }
    else {
       head=new node;
       node *x=S.head;
       head->key=x->key;
       x=x->next;
       node* y=head;
       while(x!=NULL) {
          y->next=new node;
          y=y->next;
          y->key=x->key;
          x=x->next;
          }
       y->next=NULL;
    }
       }
    return *this;
}
//-----------------------------------------------------
int stack::push(int x) {
    node *X=new node;
    X->key=x;
    X->next=head; 
    head=X;
    return 1;
    }
//-----------------------------------------------------
int stack::pop() {
    if(!empty()) {
       node *X=head;
       head=head->next;
       int val=X->key;
       delete X;
       return val;
       }
    else {
       return -1;
       }
    }
//-----------------------------------------------------
int stack::peek() {
    if(!empty()) {
      return head->key;
      }
    else {
     return -1;
     }
  }
//-----------------------------------------------------
bool stack::empty() {
    return(head==NULL);
    }
//-----------------------------------------------------
bool stack::full() {
    return 0;
    }
//-----------------------------------------------------
stack& operator+(const stack X,const stack Y) {
    stack *Ans=new stack;
    *Ans=X;
    stack t1=Y;
    stack t2;
    while(!t1.empty())
       t2.push(t1.pop());
    while(!t2.empty())
       Ans->push(t2.pop());
    return *Ans;
}
//-----------------------------------------------------
int* stack2array(const stack S,int n) {
    stack Temp=S;
    stack R;
    while(!Temp.empty())
         R.push(Temp.pop());

    int *Array=new int[n];
    int i=0;
    while(!R.empty()) {
        Array[i]=R.pop();
        i++;
        }
    for(int j=i;j<n;j++) {
      Array[j]=0;
      }
   return Array;
   }
//-----------------------------------------------------