Created
January 24, 2015 16:01
-
-
Save ldclakmal/f98ed623a6122aed14f1 to your computer and use it in GitHub Desktop.
Stack using Linked List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LinkedList_OOP_Stack.cpp : Defines the entry point for the console application. | |
// L.D.C.Lakmal | |
#include "stdafx.h" | |
#include <iostream> | |
using namespace std; | |
typedef int E; | |
class Node { | |
private: | |
E value; | |
Node* next; | |
public: | |
Node(){}; | |
E Node::getValue(); | |
void Node::setValue(E val); | |
Node* Node::getNext(); | |
void Node::setNext(Node* newNode); | |
}; | |
E Node::getValue(){ | |
return this->value; | |
} | |
void Node::setValue(E val){ | |
this->value = val; | |
} | |
Node* Node::getNext(){ | |
return this->next; | |
} | |
void Node::setNext(Node* newNode){ | |
this->next = newNode; | |
} | |
class LinkedList { | |
private: | |
Node* head; | |
int length; | |
public: | |
void LinkedList::init_list(); | |
bool LinkedList::is_list_empty(); | |
Node* LinkedList::search(E value); | |
E LinkedList::deleteNode(E value); | |
E LinkedList::deleteAt(int i); | |
bool LinkedList::insert(E value); | |
bool LinkedList::insertAt(E value, int i); | |
int LinkedList::list_length(); | |
void LinkedList::displayList(); | |
}; | |
void LinkedList::init_list(){ | |
head = NULL; | |
length = 0; | |
} | |
bool LinkedList::is_list_empty(){ | |
if(list_length() == 0){ | |
return true; | |
} | |
return false; | |
} | |
Node* LinkedList::search(E value){ | |
Node* temp = head; | |
while(temp != NULL){ | |
if(temp->getValue() == value){ | |
cout << "Node found !" << endl; | |
return temp; | |
} | |
temp = temp->getNext(); | |
} | |
cout << "Node not found !" << endl; | |
return NULL; | |
} | |
E LinkedList::deleteNode(E value){ | |
Node* temp = head; | |
if(head->getValue() == value){ | |
head = head->getNext(); | |
length--; | |
return value; | |
} | |
while(temp->getNext() != NULL){ | |
if(temp->getNext()->getValue() == value){ | |
temp->setNext(temp->getNext()->getNext()); | |
length--; | |
return value; | |
} | |
temp = temp->getNext(); | |
} | |
return NULL; | |
} | |
E LinkedList::deleteAt(int i){ | |
Node* temp = head; | |
if(i == 1){ | |
int tmpVal = head->getValue(); | |
head = head->getNext(); | |
length--; | |
return tmpVal; | |
}else{ | |
int count = 1; | |
while(temp->getNext() != NULL){ | |
if(count == i-1){ | |
int tmpVal = temp->getNext()->getValue(); | |
temp->setNext(temp->getNext()->getNext()); | |
length--; | |
return tmpVal; | |
} | |
count++; | |
temp = temp->getNext(); | |
} | |
} | |
return NULL; | |
} | |
bool LinkedList::insert(E value){ | |
Node* newNode = new Node(); | |
newNode->setValue(value); | |
newNode->setNext(NULL); | |
if(list_length() != 0){ | |
newNode->setNext(head); | |
} | |
head = newNode; | |
length++; | |
return true; | |
} | |
bool LinkedList::insertAt(E value, int i){ | |
Node* temp = head; | |
Node* newNode = new Node(); | |
newNode->setValue(value); | |
if(i == 1){ | |
newNode->setNext(head); | |
head = newNode; | |
length++; | |
return true; | |
}else{ | |
int count = 1; | |
while(temp != NULL){ | |
if(count == i-1){ | |
newNode->setNext(temp->getNext()); | |
temp->setNext(newNode); | |
length++; | |
return true; | |
} | |
count++; | |
temp = temp->getNext(); | |
} | |
} | |
return NULL; | |
} | |
int LinkedList::list_length(){ | |
return this->length; | |
} | |
void LinkedList::displayList(){ | |
int i = 0; | |
Node* temp = head; | |
while(i != list_length()){ | |
cout << temp->getValue() << " "; | |
temp = temp->getNext(); | |
i++; | |
} | |
cout << endl; | |
} | |
//------------------------------------------STACK-------------------------------------------------------------- | |
//------------------------------------------------------------------------------------------------------------- | |
typedef int E; | |
class MyStack { | |
private: | |
LinkedList values; | |
public: | |
void MyStack::init_stack(); | |
bool MyStack::is_stack_empty(); | |
bool MyStack::push(E value); | |
E MyStack::pop(); | |
E MyStack::peek(); | |
void MyStack::displayStack(); | |
}; | |
void MyStack::init_stack(){ | |
values.init_list(); | |
} | |
bool MyStack::is_stack_empty(){ | |
if(values.is_list_empty()){ | |
return true; | |
} | |
return false; | |
} | |
bool MyStack::push(E value){ | |
return values.insert(value); | |
} | |
E MyStack::pop(){ | |
return values.deleteAt(1); | |
} | |
E MyStack::peek(){ | |
int val = values.deleteAt(1); | |
values.insertAt(val, 1); | |
return val; | |
} | |
void MyStack::displayStack(){ | |
values.displayList(); | |
} | |
void main(){ | |
MyStack stack; | |
stack.init_stack(); | |
cout << "is_stack_empty : " << stack.is_stack_empty() << endl; | |
stack.push(10); | |
stack.push(20); | |
stack.push(30); | |
stack.push(40); | |
stack.push(50); | |
stack.displayStack(); | |
cout << "pop : " << stack.pop() << endl; | |
stack.displayStack(); | |
cout << "peek : " << stack.peek() << endl; | |
stack.displayStack(); | |
system("PAUSE"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment