Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
build my own stack (Lifo) with my own linked list class.
  • Loading branch information
eyalbi committed May 30, 2020
1 parent 703e554 commit 16fcac3
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 0 deletions.
141 changes: 141 additions & 0 deletions build_stack/LinkList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#include "LinkList.h"

Stack::Node::Node(const Node & Other):value(Other.value)
{
if (Other.next != NULL) {
next = new Node(*Other.next);
}
else {
next = NULL;
}
}

void Stack::Node::PrintNode()
{
cout << this->value;
cout << endl;
}

Stack::~Stack()
{
if (!IsEmpty())
{
Node* temp;
while (head != NULL) {
temp = head;
head = head->next;
delete temp;
}
size = ZERO;
}

}

Stack & Stack::operator+=(int value)
{
if (IsEmpty()) {
this->head = new Node(value);
size++;
}
else {
Node*current = head;
head = new Node(value);
head->next = current;
size++;
}
return *this;
}

Stack & Stack::operator-=(int removalAmount)
{
if (removalAmount > size) {
cout << "invalid removal amount" << endl;
return *this;
}
else if (removalAmount == size) {
Node* temp;
while (head != NULL) {
temp = head;
head = head->next;
delete temp;
size--;
}
return *this;
}
else if (removalAmount < size && removalAmount > ZERO) {
Node*temp;
while (removalAmount > ZERO)
{
temp = head;
head = head->next;
removalAmount--;
}

return *this;
}
}

bool Stack::operator==(const Stack &Rstack) const
{
if (size != Rstack.size) {
return false;
}
else {
Node* current = head;
Node* Rcurrent = Rstack.head;
while (current->next != NULL)
{
if (current->value != Rcurrent->value) {
return false;
}
else {
current = current->next;
Rcurrent = Rcurrent->next;
}
}
if (current->value != Rcurrent->value) {
return false;
}
else {
return true;
}
}
}

bool Stack::operator!=(const Stack &Rstack) const
{
if (*this == Rstack) {
return false;
}
return true;
}

Stack & Stack::operator!()
{
Node*current = head;
Node* prev = NULL;
Node *next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
return *this;
}

void Stack::PrintStack()
{
Node*temp = head;
while (temp->next != NULL) {
temp->PrintNode();
temp = temp->next;
}
temp->PrintNode();
}
ostream & operator<<(ostream & out, Stack & RStack)
{
RStack.PrintStack();
return out;
}
43 changes: 43 additions & 0 deletions build_stack/LinkList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <iostream>
#define ZERO 0
using namespace std;

class Stack {
private:
class Node {
private:
int value;
Node* next;
public:
Node(int num) :value(num), next(NULL) {}
Node(const Node &);
void PrintNode();
friend class Stack;
};
Node *head;
int size;
public:
Stack() :head(NULL), size(ZERO) {};
~Stack();
Stack & operator += (int value);
Stack & operator -= (int removalAmount);
bool operator ==(const Stack&)const;
bool operator !=(const Stack&)const;
Stack & operator !();
void PrintStack();
friend ostream& operator <<(ostream & out,Stack&);
bool IsEmpty()const { return head == NULL; };


friend class Node;


};








30 changes: 30 additions & 0 deletions build_stack/mainlinklist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "LinkList.h"


int main() {
Stack first, second;
for (int i = 10; i < 15; i++) {
first += i;
second += i;
}
cout << "First Stack :" << endl << first << "Second Stack :" << endl << second <<endl;
if (first == second) {
cout << "the stacks are equals !" << endl;
}
else{
cout << "the stacks are different !" << endl;
}
first -= 1;
second -= 3;
cout << endl << "After deleting :" << endl << " First Stack :" << endl << first << "Second Stack : " << endl << second << endl; if (first != second) { cout << "the stacks are different !" << endl; }
else
{
cout << "the stacks are equals !" << endl;
} cout << "Reverse form of the first stack :" << endl << !first << endl;
cout << "Delete top from first stack :" << endl << (first -= 1) << endl; Stack Third(first);
first = second;
cout << endl << "After assignment :" << endl << "First Stack :" << endl << first << "Second Stack : " << endl << second << endl;
cout << "Third stack :" << endl << Third;

return 0;
}
Expand Down

0 comments on commit 16fcac3

Please sign in to comment.