add a few examples for stack

This commit is contained in:
mooophy
2014-12-04 15:11:53 +13:00
parent e9385ff6e3
commit fab58a66d8
6 changed files with 702 additions and 0 deletions

84
assignment2/Stack_LL.cpp Normal file
View File

@@ -0,0 +1,84 @@
#include <stdio.h>
struct Node {
float data;
Node *next;
};
class Stack {
private: //the data differs from the array
Node *listpointer;
public: //the functions (methods) are identical
Stack();
~Stack();
void Push(float newthing);
void Pop();
float Top();
bool isEmpty();
};
Stack::Stack() {
// constructor
listpointer = NULL;
}
Stack::~Stack() {
// destructor
}
void Stack::Push(float newthing) {
// place the new thing on top of the stack
Node *temp;
temp = new Node; //same as add node to front of linked-list
temp->data = newthing;
temp->next = listpointer; //NOTE: no overflow problem
listpointer = temp;
}
void Stack::Pop() {
// remove top item from the stack
Node *p;
p = listpointer;
if (listpointer != NULL) { // check to avoid underflow
listpointer = listpointer->next;
delete p; //always delete a TEMPORARY variable
}
}
float Stack::Top() {
// return the value of the top item
return listpointer->data; //WARNING: what if listpointer is NULL?
}
bool Stack::isEmpty() {
// returns true if the stack is empty
if (listpointer == NULL) {
return true;
}
return false;
}
Stack A, B;
int main() {
// main is the same as it was with the stack based on an array
Stack A, B; //This is how to declare a stack
A.Push(62.3);
A.Push(2.4);
B.Push(47.1);
A.Pop();
if (A.isEmpty()) {
printf("Stack A is empty\n");
} else {
//float x = A.Top();
//printf("Top item in A is %1.1f\n", x);
printf("Top item in A is %1.1f\n", A.Top());
}
printf("Top item in A is %1.1f\n", A.Top());
printf("Top item in B is %1.1f\n", B.Top());
A.Pop();
if (A.isEmpty()) {
printf("Stack A is empty\n");
}
else printf("Top item in A is %1.1f\n", A.Top());
}

View File

@@ -0,0 +1,136 @@
#include <stdio.h>
struct Node {
float data;
Node *next;
};
class Stack {
private: //the data differs from the array
Node *listpointer;
public: //the functions (methods) are identical
Stack();
~Stack();
void Push(float newthing);
void Pop();
float Top();
bool isEmpty();
bool TopTwo(float *array);
void PopTwo();
bool TwoorMore();
};
Stack::Stack() {
// constructor
listpointer = NULL;
}
Stack::~Stack() {
// destructor
}
void Stack::Push(float newthing) {
// place the new thing on top of the stack
Node *temp;
temp = new Node; //same as add node to front of linked-list
temp->data = newthing;
temp->next = listpointer; //NOTE: no overflow problem
listpointer = temp;
}
void Stack::Pop() {
// remove top item from the stack
Node *p;
p = listpointer;
if (listpointer != NULL) { // check to avoid underflow
listpointer = listpointer->next;
delete p; //always delete a TEMPORARY variable
}
}
void Stack::PopTwo() {
// remove the 2 top items from the stack
Node *p;
p = listpointer;
if (listpointer != NULL) { // check to avoid underflow
listpointer = listpointer->next;
delete p; //always delete a TEMPORARY variable
}
p = listpointer;
if (listpointer != NULL) {
listpointer = listpointer->next;
delete p;
}
}
float Stack::Top() {
// return the value of the top item
return listpointer->data; //WARNING: what if listpointer is NULL?
}
bool Stack::TopTwo(float *array) {
// return a pointer to an array[2]
array[0]=0;
array[1]=0;
if(listpointer==NULL) return false;
else {
if (listpointer->next==NULL) return false;
else{
array[0]=listpointer->data;
array[1]=(listpointer->next)->data;
return true; //success
}
}
}
bool Stack::isEmpty() {
// returns true if the stack is empty
if (listpointer == NULL) {
return true;
}
return false;
}
bool Stack::TwoorMore() {
// return true if the stack has at least two items
if(listpointer==NULL) return false;
else {
if (listpointer->next==NULL) return false;
else return true;
}
}
Stack A; //This is how to declare a stack
float temparray[2];
int main() {
A.Push(45.3);
A.Push(62.3);
A.Push(2.0);
A.Push(3.0);
A.Push(4.0);
if (A.TwoorMore()) {
printf("Stack A has more than two items\n");
A.TopTwo(&temparray[0]);
printf("two top items are %f and %f \n",temparray[0],temparray[1]);
} else {
printf("Stack A has less than two items\n");
}
A.PopTwo();
if (A.TwoorMore()) {
printf("Stack A has more than two items\n");
A.TopTwo(&temparray[0]);
printf("two top items are %f and %f \n",temparray[0],temparray[1]);
} else {
printf("Stack A has less than two items\n");
}
A.PopTwo();
if (A.TwoorMore()) {
printf("Stack A has more than two items\n");
A.TopTwo(&temparray[0]);
printf("two top items are %f and %f \n",temparray[0],temparray[1]);
} else {
printf("Stack A has less than two items\n");
}
}

View File

@@ -0,0 +1,193 @@
#include <stdio.h>
#include <iostream>
using namespace std;
struct Node {
float data;
Node *next;
};
class Stack {
private: //the data differs from the array
Node *listpointer;
public: //the functions (methods) are identical
Stack();
~Stack();
void Push(float newthing);
void Pop();
float Top();
bool isEmpty();
bool TopTwo(float *array);
void PopTwo();
bool TwoorMore();
void PopTwoandReturn(float *a, float *b );
void PopTwoandReturn2(float &a, float &b );
};
Stack::Stack() {
// constructor
listpointer = NULL;
}
Stack::~Stack() {
// destructor
}
void Stack::Push(float newthing) {
// place the new thing on top of the stack
Node *temp;
temp = new Node; //same as add node to front of linked-list
temp->data = newthing;
temp->next = listpointer; //NOTE: no overflow problem
listpointer = temp;
}
void Stack::Pop() {
// remove top item from the stack
Node *p;
p = listpointer;
if (listpointer != NULL) { // check to avoid underflow
listpointer = listpointer->next;
delete p; //always delete a TEMPORARY variable
}
}
void Stack::PopTwo() {
// remove the 2 top items from the stack
Node *p;
p = listpointer;
if (listpointer != NULL) { // check to avoid underflow
listpointer = listpointer->next;
delete p; //always delete a TEMPORARY variable
}
p = listpointer;
if (listpointer != NULL) {
listpointer = listpointer->next;
delete p;
}
}
void Stack::PopTwoandReturn(float *a, float *b ) {
// remove the 2 top items from the stack
Node *p;
p = listpointer;
if (listpointer != NULL) { // check to avoid underflow
*a = listpointer->data;
listpointer = listpointer->next;
delete p; //always delete a TEMPORARY variable
}
p = listpointer;
if (listpointer != NULL) {
*b = listpointer->data;
listpointer = listpointer->next;
delete p;
}
}
void Stack::PopTwoandReturn2(float &a, float &b ) {
// remove the 2 top items from the stack
Node *p;
p = listpointer;
if (listpointer != NULL) { // check to avoid underflow
a = listpointer->data;
listpointer = listpointer->next;
delete p; //always delete a TEMPORARY variable
}
p = listpointer;
if (listpointer != NULL) {
b = listpointer->data;
listpointer = listpointer->next;
delete p;
}
}
float Stack::Top() {
// return the value of the top item
return listpointer->data; //WARNING: what if listpointer is NULL?
}
bool Stack::TopTwo(float *array) {
// return a pointer to an array[2]
if(listpointer==NULL || listpointer->next==NULL) {
return false;
array[0]=0.0;
array[1]=0.0;
}//not two items
else{
array[0]=listpointer->data;
array[1]=(listpointer->next)->data;
return true; //success
}
}
bool Stack::isEmpty() {
// returns true if the stack is empty
if (listpointer == NULL) {
return true;
}
return false;
}
bool Stack::TwoorMore() {
// return true if the stack has at least two items
if(listpointer==NULL)return false;
else{
if(listpointer->next==NULL) return false;
else return true;
}
}
Stack A; //This is how to declare a stack
float temparray[2];
int main() {
A.Push(1.0);
A.Push(2.0);
A.Push(3.0);
A.Push(4.8);
A.Push(5.3);
A.Push(6.3);
A.Push(7.0);
A.Push(8.0);
A.Push(9.0);
A.Push(10.0);
if (A.TwoorMore()) {
printf("Stack A has more than two items\n");
//A.TopTwo(&temparray[0]);
A.TopTwo(temparray);
printf("two top items are %f and %f \n",temparray[0],temparray[1]);
} else {
printf("Stack A has less than two items\n");
}
float a,b;
A.PopTwoandReturn(&a,&b);
printf("Return PopTwoandReturn %f %f \n",a,b);
//or
float *c,*d;
c=new float;
d=new float;
A.PopTwoandReturn(c,d);
printf("Return PopTwoandReturn %f %f \n",*c,*d);
cout << "COUT Return PopTwoandReturn " << *c << " " << *d << endl;
//or
A.PopTwoandReturn2(a,b);
printf("Return PopTwoandReturn2 %f %f \n",a,b);
if (A.TwoorMore()) {
printf("Stack A has more than two items\n");
A.TopTwo(&temparray[0]);
printf("two top items are %f and %f \n",temparray[0],temparray[1]);
} else {
printf("Stack A has less than two items\n");
}
A.PopTwo();
if (A.TwoorMore()) {
printf("Stack A has more than two items\n");
A.TopTwo(&temparray[0]);
printf("two top items are %f and %f \n",temparray[0],temparray[1]);
} else {
printf("Stack A has less than two items\n");
}
}

View File

@@ -0,0 +1,67 @@
#include <stdio.h>
class Stack {
private:
float data[100];
int index;
public:
Stack();
~Stack();
void Push(float newthing);
void Pop();
float Top();
bool isEmpty();
};
Stack::Stack() { //Note: no data type in front
// constructor
index = -1;
}
Stack::~Stack() {
// destructor
}
void Stack::Push(float newthing) {
// place the new thing on top of the stack
index++;
data[index] = newthing; //Warning: watch for overflow
}
void Stack::Pop() {
// remove the top item from the stack
if (index > -1) { index--; } //Takes care of underflow
}
float Stack::Top() {
// return the value of the top item in the stack
return data[index]; //Warning: what if stack is empty?
}
bool Stack::isEmpty() {
// return true if the stack is empty
if (index < 0) { return true; }
return false;
}
Stack A, B; //This is how to declare a stack
int main() {
A.Push(62.3);
A.Push(2.0);
A.Push(3.0);
A.Push(4.0);
A.Pop();
A.Pop();
A.Pop();
A.Pop();
B.Push(5.0);
if (A.isEmpty()) {
printf("Stack A is empty\n");
} else {
float x = A.Top();
printf("Top item in A is %1.1f\n", x);
}
}

View File

@@ -0,0 +1,105 @@
#include <stdio.h>
class Stack {
private:
float data[100];
int index;
public:
Stack();
~Stack();
void Push(float newthing);
void Pop();
float Top();
bool isEmpty();
bool TopTwo(float *array);
void PopTwo();
bool TwoorMore();
};
Stack::Stack() { //Note: no data type in front
// constructor
index = -1;
}
Stack::~Stack() {
// destructor
}
void Stack::Push(float newthing) {
// place the new thing on top of the stack
index++;
data[index] = newthing; //Warning: watch for overflow
}
void Stack::Pop() {
// remove the top item from the stack
if (index > -1) { index--; } //Takes care of underflow
}
void Stack::PopTwo() {
// remove the 2 top items from the stack
if (index > -1) { index--; }
if (index > -1) { index--; }
}
float Stack::Top() {
// return the value of the top item in the stack
return data[index]; //Warning: what if stack is empty?
}
bool Stack::TopTwo(float *array) {
// return a pointer to an array[2]
if(index>=1){
array[0]=data[index];
array[1]=data[index-1];
return true; //success
}
else{ return false;}//less than two items
}
bool Stack::isEmpty() {
// return true if the stack is empty
if (index < 0) { return true; }
return false;
}
bool Stack::TwoorMore() {
// return true if the stack has at least two items
if (index >= 1 ) { return true; }
return false;
}
Stack A; //This is how to declare a stack
float temparray[2];
int main() {
A.Push(45.3);
A.Push(62.3);
A.Push(2.0);
A.Push(3.0);
A.Push(4.0);
if (A.TwoorMore()) {
printf("Stack A has more than two items\n");
A.TopTwo(&temparray[0]);
printf("two top items are %f and %f \n",temparray[0],temparray[1]);
} else {
printf("Stack A has less than two items\n");
}
A.PopTwo();
if (A.TwoorMore()) {
printf("Stack A has more than two items\n");
A.TopTwo(&temparray[0]);
printf("two top items are %f and %f \n",temparray[0],temparray[1]);
} else {
printf("Stack A has less than two items\n");
}
A.PopTwo();
if (A.TwoorMore()) {
printf("Stack A has more than two items\n");
A.TopTwo(&temparray[0]);
printf("two top items are %f and %f \n",temparray[0],temparray[1]);
} else {
printf("Stack A has less than two items\n");
}
}

View File

@@ -0,0 +1,117 @@
#include <stdio.h>
class Stack {
private:
float data[100];
int index;
public:
Stack();
~Stack();
void Push(float newthing);
void Pop();
float Top();
bool isEmpty();
bool TopTwo(float *array);
void PopTwo();
bool TwoorMore();
void PopTwoandReturn(float &a, float &b);
};
Stack::Stack() { //Note: no data type in front
// constructor
index = -1;
}
Stack::~Stack() {
// destructor
}
void Stack::Push(float newthing) {
// place the new thing on top of the stack
index++;
data[index] = newthing; //Warning: watch for overflow
}
void Stack::Pop() {
// remove the top item from the stack
if (index > -1) { index--; } //Takes care of underflow
}
void Stack::PopTwo() {
// remove the 2 top items from the stack
if (index > -1) { index--; }
if (index > -1) { index--; }
}
void Stack::PopTwoandReturn(float &a, float &b) {
// remove the 2 top items from the stack
if (index > -1) { a=data[index]; index--; }
if (index > -1) { b=data[index]; index--; }
}
float Stack::Top() {
// return the value of the top item in the stack
return data[index]; //Warning: what if stack is empty?
}
bool Stack::TopTwo(float *array) {
// return a pointer to an array[2]
if(index>=1){
array[0]=data[index];
array[1]=data[index-1];
return true; //success
}
else{ return false;}//less than two items
}
bool Stack::isEmpty() {
// return true if the stack is empty
if (index < 0) { return true; }
return false;
}
bool Stack::TwoorMore() {
// return true if the stack has at least two items
if (index >= 1 ) { return true; }
return false;
}
Stack A; //This is how to declare a stack
float temparray[2];
int main() {
A.Push(45.3);
A.Push(62.3);
A.Push(2.0);
A.Push(3.0);
A.Push(4.0);
if (A.TwoorMore()) {
printf("Stack A has more than two items\n");
A.TopTwo(&temparray[0]);
printf("two top items are %f and %f \n",temparray[0],temparray[1]);
} else {
printf("Stack A has less than two items\n");
}
//A.PopTwo();
float a,b;
A.PopTwoandReturn(a,b);
printf("PopTwo returning %f %f \n",a,b);
if (A.TwoorMore()) {
printf("Stack A has more than two items\n");
A.TopTwo(&temparray[0]);
printf("two top items are %f and %f \n",temparray[0],temparray[1]);
} else {
printf("Stack A has less than two items\n");
}
//A.PopTwo();
A.PopTwoandReturn(a,b);
printf("PopTwo returning %f %f \n",a,b);
if (A.TwoorMore()) {
printf("Stack A has more than two items\n");
A.TopTwo(&temparray[0]);
printf("two top items are %f and %f \n",temparray[0],temparray[1]);
} else {
printf("Stack A has less than two items\n");
}
}