Created
January 24, 2015 16:02
-
-
Save ldclakmal/d2ef6bc6fa217931c996 to your computer and use it in GitHub Desktop.
Queue 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_Queue.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 MyQueue { | |
private: | |
LinkedList values; | |
public: | |
void MyQueue::init_queue(); | |
bool MyQueue::is_queue_empty(); | |
bool MyQueue::enqueue(E value); | |
E MyQueue::dequeue(); | |
void MyQueue::displayQueue(); | |
}; | |
void MyQueue::init_queue(){ | |
values.init_list(); | |
} | |
bool MyQueue::is_queue_empty(){ | |
if(values.is_list_empty()){ | |
return true; | |
} | |
return false; | |
} | |
bool MyQueue::enqueue(E value){ | |
return values.insert(value); | |
} | |
E MyQueue::dequeue(){ | |
return values.deleteAt(values.list_length()); | |
} | |
void MyQueue::displayQueue(){ | |
values.displayList(); | |
} | |
void main(){ | |
MyQueue queue; | |
queue.init_queue(); | |
cout << "is_queue_empty : " << queue.is_queue_empty() << endl; | |
queue.enqueue(10); | |
queue.enqueue(20); | |
queue.enqueue(30); | |
queue.enqueue(40); | |
queue.enqueue(50); | |
queue.displayQueue(); | |
cout << "dequeue : " << queue.dequeue() << endl; | |
queue.displayQueue(); | |
system("PAUSE"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment