Skip to content

Commit

Permalink
Update OOP
Browse files Browse the repository at this point in the history
  • Loading branch information
KidsadakornNuallaoong committed Apr 23, 2024
1 parent 853413b commit 9ea7803
Show file tree
Hide file tree
Showing 12 changed files with 560 additions and 0 deletions.
8 changes: 8 additions & 0 deletions OOP/LabW6/ChainNode/ChainNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <iostream>
#include "ChainNode.h"

ChainNode::ChainNode(int e, ChainNode *p)
{
element = e;
next = p;
}
15 changes: 15 additions & 0 deletions OOP/LabW6/ChainNode/ChainNode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#if !defined(_CHAINNODE_H)
#define _CHAINNODE_H

#include <iostream>

class ChainNode
{
public:
int element;
ChainNode *next;
public:
ChainNode(int e = 0, ChainNode *p = NULL);
};

#endif // _CHAINNODE_H
18 changes: 18 additions & 0 deletions OOP/LabW6/ChainNode/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/***************************************************************************************
* ID: 65037743
* NAME: Mr.Kidsadakorn Nuallaoong
* DESCRIPTION: ArrayStack Testing
***************************************************************************************/
#include <iostream>
#include "ChainNode.h"

using namespace std;

int main()
{
ChainNode a; // * ==> e = 0, p = NULL
ChainNode b(10, &a); // * ==> e = 10, p = &a
ChainNode c(20, &b); // * ==> e = 20, p = &b

return 0;
}
18 changes: 18 additions & 0 deletions OOP/LabW6/ChainNode/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
run:
@g++ -c main.cpp -o main.o -g
@g++ -c ChainNode.cpp -o ChainNode.o -g
@g++ main.o ChainNode.o -o LabW3 -g

@./LabW3

@rm -f *.o LabW3

all: main.o ChainNode.o
g++ main.o ChainNode.o -o week4 -g

main.o: main.cpp ChainNode.cpp
g++ -c main.cpp -o main.o -g
g++ -c ChainNode.cpp -o ChainNode.o -g

clean:
rm -f *.o LabW3
95 changes: 95 additions & 0 deletions OOP/LabW6/FastArrayQueue/FastArrayQueue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "FastArrayQueue.h"
#include <iostream>

using namespace std;

FastArrayQueue::FastArrayQueue(int maxSize)
{
this->maxSize = maxSize;
curSize = 0;
in = 0;
out = 0;
L = new int[this->maxSize];
}

FastArrayQueue::~FastArrayQueue()
{
delete []L;
}

void FastArrayQueue::enQueue(int e)
{
// * validate (queue Full)
if(curSize >= maxSize){
cerr << "Error : Queue is full..." << endl;
return;
}

// * enqueue alg.(example)
L[in] = e;
in = (in + 1) % maxSize;

curSize++;
}

int FastArrayQueue::deQueue()
{
// * validate (queue empty)
if(curSize <= 0){
cerr << "Error : Queue is Empty..." << endl;
return -1;
}

// * dequeue alg.(example)
int t = L[out];
L[out] = 0;
out = (out+1) % maxSize;
curSize--;

return t;
}

int FastArrayQueue::front()
{
// * validate (queue empty)
if(curSize <= 0){
cerr << "Error : Queue is Empty..." << endl;
return -1;
}

// * get front (example)

return L[out];
}

int FastArrayQueue::rear()
{
// * validate (queue empty)
if(curSize <= 0){
cerr << "Error : Queue is Empty..." << endl;
return -1;
}

// * get front (example)

return L[((in-1) + maxSize) % maxSize];
}

void FastArrayQueue::display()
{
// * Queue. {e0, e1, en...,} curSize/maxSize
cout << "Q : {";
if(curSize == 0){
cout << "}";
} else {
for(int i = 0; i < curSize; i++){
cout << L[(out + i) % maxSize];
if(i < curSize-1){
cout << ", ";
} else {
cout << "}";
}
}
}
cout << ", " << "Size : " << curSize << "/" << maxSize << ", in : " << in << ", out : " << out << endl;
}
34 changes: 34 additions & 0 deletions OOP/LabW6/FastArrayQueue/FastArrayQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#if !defined(_FASTARRAYQUEUE_H)
#define _FASTARRAYQUEUE_H

#define DEFAULT_MAX_SIZE 5

class FastArrayQueue
{
private:
int *L;
int curSize;
int maxSize;
int in;
int out;


public:
FastArrayQueue(int maxSize = DEFAULT_MAX_SIZE); // * constructor
~FastArrayQueue();

// * method
void enQueue(int e);
int deQueue();
int front();
int rear();

void display();

void append(int e){ enQueue(e); };
int remove(){ return deQueue(); };
int head(){ return front(); };
int back(){ return rear(); };
};

#endif // _FASTARRAYQUEUE_H
79 changes: 79 additions & 0 deletions OOP/LabW6/FastArrayQueue/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/***************************************************************************************
* ID: 65037743
* NAME: Mr.Kidsadakorn Nuallaoong
* DESCRIPTION: FastArrayQueue
***************************************************************************************/
#include <iostream>
#include "FastArrayQueue.h"

using namespace std;

int main()
{
FastArrayQueue Queue;

int choice, index, element, round;

do
{
cout << "----------------- MENU -----------------" << endl
<< endl;

cout << "================ DISPLAY ===============" << endl
<< endl;

Queue.display();

cout << endl
<< "========================================" << endl
<< endl;

cout << "1: Add Queue, enQueue()" << endl;
cout << "2: Get first , deQueue" << endl;
cout << "3: Show front Queue, front()" << endl;
cout << "4: shoe last Queue, rear()" << endl;

cout << "0: Exit!!!!" << endl;

cout << endl;

cout << "Enter your choice : ";
cin >> choice;

switch (choice)
{
case 1: // * enQueue
cout << "[Massage] Enter element : ";
cin >> element;
Queue.enQueue(element);
break;

case 2: // * pop
element = Queue.deQueue();
cout << "[Massage] get element : " << element << endl;
break;

case 3: // * top
element = Queue.front();
cout << "[Massage] front element : " << element << endl;
break;

case 4: // * top
element = Queue.rear();
cout << "[Massage] Last element : " << element << endl;
break;

case 0:
cout << "Bye...." << endl
<< endl;
cout << "-------------- Call method --------------" << endl;
break;

default: // * Exit
cout << "[Massage] Wrong choice,try again..." << endl;
}

} while (choice != 0);

return 0;
}
20 changes: 20 additions & 0 deletions OOP/LabW6/FastArrayQueue/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
run:
@g++ -c main.cpp -o main.o
@g++ -c FastArrayQueue.cpp -o FastArrayQueue.o
@g++ main.o FastArrayQueue.o -o FastArrayQueue

@./FastArrayQueue

@rm -f *.o FastArrayQueue

all: main.o FastArrayQueue.o
g++ main.o FastArrayQueue.o -o FastArrayQueue -g

main.o: main.cpp
g++ -c main.cpp -o main.o

FastArrayQueue.o: FastArrayQueue.cpp
g++ -c FastArrayQueue.cpp -o FastArrayQueue.o

clean:
rm -f *.o FastArrayQueue
Loading

0 comments on commit 9ea7803

Please sign in to comment.