mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-25 05:04:36 +08:00
Merge pull request #16 from kvedala/merge_upstream
simplify subfolders in data_structures
This commit is contained in:
8
.github/workflows/awesome_forkflow.yml
vendored
8
.github/workflows/awesome_forkflow.yml
vendored
@@ -167,13 +167,13 @@ jobs:
|
||||
|
||||
print("cpplint:")
|
||||
for cpp_file in cpp_files:
|
||||
subprocess.run(["cpplint", "--filter=-legal/copyright", cpp_file], check=True, text=True)
|
||||
subprocess.run(["cpplint", "--filter=-legal/copyright,-build/include", cpp_file], check=True, text=True)
|
||||
|
||||
print("g++:")
|
||||
# print("g++:")
|
||||
# compile_exts = tuple(".c .c++ .cc .cpp .cu .cxx".split())
|
||||
# compile_files = [file for file in cpp_files if file.lower().endswith(compile_exts)]
|
||||
for cpp_file in cpp_files:
|
||||
subprocess.run(["g++", cpp_file], check=True, text=True)
|
||||
# for cpp_file in cpp_files:
|
||||
# subprocess.run(["g++", cpp_file], check=True, text=True)
|
||||
|
||||
upper_files = [file for file in cpp_files if file != file.lower()]
|
||||
if upper_files:
|
||||
|
||||
@@ -30,7 +30,6 @@ if(DOXYGEN_FOUND)
|
||||
set(DOXYGEN_INLINE_SOURCES YES)
|
||||
set(DOXYGEN_CREATE_SUBDIRS YES)
|
||||
set(DOXYGEN_EXTRACT_PRIVATE YES)
|
||||
set(DOCYGEN_GENERATE_TREEVIEW YES)
|
||||
set(DOXYGEN_GENERATE_TREEVIEW YES)
|
||||
set(DOXYGEN_STRIP_CODE_COMMENTS NO)
|
||||
set(DOXYGEN_EXT_LINKS_IN_WINDOW YES)
|
||||
|
||||
14
DIRECTORY.md
14
DIRECTORY.md
@@ -25,21 +25,17 @@
|
||||
* [Linkedlist Implentation Usingarray](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/linkedlist_implentation_usingarray.cpp)
|
||||
* [List Array](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/list_array.cpp)
|
||||
* [Morrisinorder](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/morrisinorder.cpp)
|
||||
* Queue
|
||||
* [Queue](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/queue/queue.cpp)
|
||||
* [Queue](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/queue/queue.h)
|
||||
* [Test Queue](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/queue/test_queue.cpp)
|
||||
* [Queue](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/queue.h)
|
||||
* [Queue Using Array](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/queue_using_array.cpp)
|
||||
* [Queue Using Array2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/queue_using_array2.cpp)
|
||||
* [Queue Using Linked List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/queue_using_linked_list.cpp)
|
||||
* [Queue Using Linkedlist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/queue_using_linkedlist.cpp)
|
||||
* [Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/stack.h)
|
||||
* [Stack Using Array](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/stack_using_array.cpp)
|
||||
* [Stack Using Linked List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/stack_using_linked_list.cpp)
|
||||
* Stk
|
||||
* [Main](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/stk/main.cpp)
|
||||
* [Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/stk/stack.cpp)
|
||||
* [Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/stk/stack.h)
|
||||
* [Test Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/stk/test_stack.cpp)
|
||||
* [Test Queue](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/test_queue.cpp)
|
||||
* [Test Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/test_stack.cpp)
|
||||
* [Test Stack Students](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/test_stack_students.cpp)
|
||||
* [Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/tree.cpp)
|
||||
* [Trie Modern](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/trie_modern.cpp)
|
||||
* [Trie Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structures/trie_tree.cpp)
|
||||
|
||||
@@ -16,3 +16,5 @@ foreach( testsourcefile ${APP_SOURCES} )
|
||||
install(TARGETS ${testname} DESTINATION "bin/data_structures")
|
||||
|
||||
endforeach( testsourcefile ${APP_SOURCES} )
|
||||
|
||||
add_subdirectory(cll)
|
||||
|
||||
5
data_structures/cll/CMakeLists.txt
Normal file
5
data_structures/cll/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
add_executable( cll
|
||||
cll.cpp
|
||||
main_cll.cpp
|
||||
)
|
||||
install(TARGETS cll DESTINATION "bin/data_structures")
|
||||
@@ -1,11 +0,0 @@
|
||||
CC= g++
|
||||
CFLAGS = -c -Wall
|
||||
|
||||
all: cll
|
||||
cll.o: cll.cpp main_cll.cpp
|
||||
$(CC) $(CFLAGS) cll.cpp main_cll.cpp
|
||||
cll: cll.o
|
||||
$(CC) main_cll.o cll.o -o cll
|
||||
|
||||
clean:
|
||||
rm *o cll
|
||||
88
data_structures/queue.h
Normal file
88
data_structures/queue.h
Normal file
@@ -0,0 +1,88 @@
|
||||
/* This class specifies the basic operation on a queue as a linked list */
|
||||
#ifndef DATA_STRUCTURES_QUEUE_H_
|
||||
#define DATA_STRUCTURES_QUEUE_H_
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
/** Definition of the node */
|
||||
template <class Kind>
|
||||
struct node {
|
||||
Kind data;
|
||||
node<Kind> *next;
|
||||
};
|
||||
|
||||
/** Definition of the queue class */
|
||||
template <class Kind>
|
||||
class queue {
|
||||
public:
|
||||
/** Show queue */
|
||||
void display() {
|
||||
node<Kind> *current = queueFront;
|
||||
std::cout << "Front --> ";
|
||||
while (current != NULL) {
|
||||
std::cout << current->data << " ";
|
||||
current = current->next;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
std::cout << "Size of queue: " << size << std::endl;
|
||||
}
|
||||
|
||||
/** Default constructor*/
|
||||
queue() {
|
||||
queueFront = NULL;
|
||||
queueRear = NULL;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
/** Destructor */
|
||||
~queue() {}
|
||||
|
||||
/** Determine whether the queue is empty */
|
||||
bool isEmptyQueue() { return (queueFront == NULL); }
|
||||
|
||||
/** Add new item to the queue */
|
||||
void enQueue(Kind item) {
|
||||
node<Kind> *newNode;
|
||||
newNode = new node<Kind>;
|
||||
newNode->data = item;
|
||||
newNode->next = NULL;
|
||||
if (queueFront == NULL) {
|
||||
queueFront = newNode;
|
||||
queueRear = newNode;
|
||||
} else {
|
||||
queueRear->next = newNode;
|
||||
queueRear = queueRear->next;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
/** Return the first element of the queue */
|
||||
Kind front() {
|
||||
assert(queueFront != NULL);
|
||||
return queueFront->data;
|
||||
}
|
||||
|
||||
/** Remove the top element of the queue */
|
||||
void deQueue() {
|
||||
node<Kind> *temp;
|
||||
if (!isEmptyQueue()) {
|
||||
temp = queueFront;
|
||||
queueFront = queueFront->next;
|
||||
delete temp;
|
||||
size--;
|
||||
} else {
|
||||
std::cout << "Queue is empty !" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
/** Clear queue */
|
||||
void clear() { queueFront = NULL; }
|
||||
|
||||
private:
|
||||
node<Kind> *queueFront; /**< Pointer to the front of the queue */
|
||||
node<Kind> *queueRear; /**< Pointer to the rear of the queue */
|
||||
int size;
|
||||
};
|
||||
|
||||
#endif // DATA_STRUCTURES_QUEUE_H_
|
||||
@@ -1,11 +0,0 @@
|
||||
CC= g++
|
||||
CFLAGS = -c -Wall
|
||||
|
||||
all: test_queue
|
||||
queue.o: queue.cpp
|
||||
$(CC) $(CFLAGS) queue.cpp
|
||||
test_queue: queue.o
|
||||
$(CC) test_queue.cpp queue.o -o queue
|
||||
|
||||
clean:
|
||||
rm *o queue
|
||||
@@ -1,80 +0,0 @@
|
||||
#include "queue.h"
|
||||
#include <assert.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* Default constructor*/
|
||||
template <class Kind>
|
||||
queue<Kind>::queue() {
|
||||
queueFront = NULL;
|
||||
queueRear = NULL;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
/* Destructor */
|
||||
template <class Kind>
|
||||
queue<Kind>::~queue() {}
|
||||
|
||||
/* Display for testing */
|
||||
template <class Kind>
|
||||
void queue<Kind>::display() {
|
||||
node<Kind> *current = queueFront;
|
||||
cout << "Front --> ";
|
||||
while (current != NULL) {
|
||||
cout << current->data << " ";
|
||||
current = current->next;
|
||||
}
|
||||
cout << endl;
|
||||
cout << "Size of queue: " << size << endl;
|
||||
}
|
||||
|
||||
/* Determine whether the queue is empty */
|
||||
template <class Kind>
|
||||
bool queue<Kind>::isEmptyQueue() {
|
||||
return (queueFront == NULL);
|
||||
}
|
||||
|
||||
/* Clear queue */
|
||||
template <class Kind>
|
||||
void queue<Kind>::clear() {
|
||||
queueFront = NULL;
|
||||
}
|
||||
|
||||
/* Add new item to the queue */
|
||||
template <class Kind>
|
||||
void queue<Kind>::enQueue(Kind item) {
|
||||
node<Kind> *newNode;
|
||||
newNode = new node<Kind>;
|
||||
newNode->data = item;
|
||||
newNode->next = NULL;
|
||||
if (queueFront == NULL) {
|
||||
queueFront = newNode;
|
||||
queueRear = newNode;
|
||||
} else {
|
||||
queueRear->next = newNode;
|
||||
queueRear = queueRear->next;
|
||||
}
|
||||
size++;
|
||||
}
|
||||
|
||||
/* Return the top element of the queue */
|
||||
template <class Kind>
|
||||
Kind queue<Kind>::front() {
|
||||
assert(queueFront != NULL);
|
||||
return queueFront->data;
|
||||
}
|
||||
|
||||
/* Remove the element of the queue */
|
||||
template <class Kind>
|
||||
void queue<Kind>::deQueue() {
|
||||
node<Kind> *temp;
|
||||
if (!isEmptyQueue()) {
|
||||
temp = queueFront;
|
||||
queueFront = queueFront->next;
|
||||
delete temp;
|
||||
size--;
|
||||
} else {
|
||||
cout << "Queue is empty !" << endl;
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/* This class specifies the basic operation on a queue as a linked list */
|
||||
#ifndef QUEUE_H
|
||||
#define QUEUE_H
|
||||
|
||||
/* Definition of the node */
|
||||
template <class Kind>
|
||||
struct node {
|
||||
Kind data;
|
||||
node<Kind> *next;
|
||||
};
|
||||
|
||||
/* Definition of the queue class */
|
||||
template <class Kind>
|
||||
class queue {
|
||||
public:
|
||||
void display(); /* Show queue */
|
||||
queue(); /* Default constructor*/
|
||||
~queue(); /* Destructor */
|
||||
bool isEmptyQueue(); /* Determine whether the queue is empty */
|
||||
void enQueue(Kind item); /* Add new item to the queue */
|
||||
Kind front(); /* Return the first element of the queue */
|
||||
void deQueue(); /* Remove the top element of the queue */
|
||||
void clear();
|
||||
|
||||
private:
|
||||
node<Kind> *queueFront; /* Pointer to the front of the queue */
|
||||
node<Kind> *queueRear; /* Pointer to the rear of the queue */
|
||||
int size;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,41 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "queue.cpp"
|
||||
#include "queue.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
queue<string> q;
|
||||
cout << "---------------------- Test construct ----------------------"
|
||||
<< endl;
|
||||
q.display();
|
||||
cout << "---------------------- Test isEmptyQueue ----------------------"
|
||||
<< endl;
|
||||
if (q.isEmptyQueue())
|
||||
cout << "PASS" << endl;
|
||||
else
|
||||
cout << "FAIL" << endl;
|
||||
cout << "---------------------- Test enQueue ----------------------"
|
||||
<< endl;
|
||||
cout << "After Hai, Jeff, Tom, Jkingston go into queue: " << endl;
|
||||
q.enQueue("Hai");
|
||||
q.enQueue("Jeff");
|
||||
q.enQueue("Tom");
|
||||
q.enQueue("Jkingston");
|
||||
q.display();
|
||||
cout << "---------------------- Test front ----------------------" << endl;
|
||||
string value = q.front();
|
||||
if (value == "Hai")
|
||||
cout << "PASS" << endl;
|
||||
else
|
||||
cout << "FAIL" << endl;
|
||||
cout << "---------------------- Test deQueue ----------------------"
|
||||
<< endl;
|
||||
q.display();
|
||||
q.deQueue();
|
||||
q.deQueue();
|
||||
cout << "After Hai, Jeff left the queue: " << endl;
|
||||
q.display();
|
||||
return 0;
|
||||
}
|
||||
111
data_structures/stack.h
Normal file
111
data_structures/stack.h
Normal file
@@ -0,0 +1,111 @@
|
||||
/* This class specifies the basic operation on a stack as a linked list */
|
||||
#ifndef DATA_STRUCTURES_STACK_H_
|
||||
#define DATA_STRUCTURES_STACK_H_
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
/* Definition of the node */
|
||||
template <class Type>
|
||||
struct node {
|
||||
Type data;
|
||||
node<Type> *next;
|
||||
};
|
||||
|
||||
/* Definition of the stack class */
|
||||
template <class Type>
|
||||
class stack {
|
||||
public:
|
||||
/** Show stack */
|
||||
void display() {
|
||||
node<Type> *current = stackTop;
|
||||
std::cout << "Top --> ";
|
||||
while (current != NULL) {
|
||||
std::cout << current->data << " ";
|
||||
current = current->next;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
std::cout << "Size of stack: " << size << std::endl;
|
||||
}
|
||||
|
||||
/** Default constructor*/
|
||||
stack() {
|
||||
stackTop = NULL;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
/** Destructor */
|
||||
~stack() {}
|
||||
|
||||
/** Determine whether the stack is empty */
|
||||
bool isEmptyStack() { return (stackTop == NULL); }
|
||||
|
||||
/** Add new item to the stack */
|
||||
void push(Type item) {
|
||||
node<Type> *newNode;
|
||||
newNode = new node<Type>;
|
||||
newNode->data = item;
|
||||
newNode->next = stackTop;
|
||||
stackTop = newNode;
|
||||
size++;
|
||||
}
|
||||
|
||||
/** Return the top element of the stack */
|
||||
Type top() {
|
||||
assert(stackTop != NULL);
|
||||
return stackTop->data;
|
||||
}
|
||||
|
||||
/** Remove the top element of the stack */
|
||||
void pop() {
|
||||
node<Type> *temp;
|
||||
if (!isEmptyStack()) {
|
||||
temp = stackTop;
|
||||
stackTop = stackTop->next;
|
||||
delete temp;
|
||||
size--;
|
||||
} else {
|
||||
std::cout << "Stack is empty !" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
/** Clear stack */
|
||||
void clear() { stackTop = NULL; }
|
||||
|
||||
/** Overload "=" the assignment operator */
|
||||
stack<Type> &operator=(const stack<Type> &otherStack) {
|
||||
node<Type> *newNode, *current, *last;
|
||||
|
||||
/* If stack is no empty, make it empty */
|
||||
if (stackTop != NULL) {
|
||||
stackTop = NULL;
|
||||
}
|
||||
if (otherStack.stackTop == NULL) {
|
||||
stackTop = NULL;
|
||||
} else {
|
||||
current = otherStack.stackTop;
|
||||
stackTop = new node<Type>;
|
||||
stackTop->data = current->data;
|
||||
stackTop->next = NULL;
|
||||
last = stackTop;
|
||||
current = current->next;
|
||||
/* Copy the remaining stack */
|
||||
while (current != NULL) {
|
||||
newNode = new node<Type>;
|
||||
newNode->data = current->data;
|
||||
newNode->next = NULL;
|
||||
last->next = newNode;
|
||||
last = newNode;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
size = otherStack.size;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
node<Type> *stackTop; /**< Pointer to the stack */
|
||||
int size;
|
||||
};
|
||||
|
||||
#endif // DATA_STRUCTURES_STACK_H_
|
||||
@@ -1,13 +0,0 @@
|
||||
CC= g++
|
||||
CFLAGS = -c -Wall
|
||||
|
||||
all: main test_stack
|
||||
stack.o: stack.cpp
|
||||
$(CC) $(CFLAGS) stack.cpp
|
||||
test_stack: stack.o
|
||||
$(CC) test_stack.cpp stack.o -o stk
|
||||
main: stack.o
|
||||
$(CC) main.cpp stack.o -o main
|
||||
|
||||
clean:
|
||||
rm *o stk main
|
||||
@@ -1,103 +0,0 @@
|
||||
#include "stack.h"
|
||||
#include <assert.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* Default constructor*/
|
||||
template <class Type>
|
||||
stack<Type>::stack() {
|
||||
stackTop = NULL;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
/* Destructor */
|
||||
template <class Type>
|
||||
stack<Type>::~stack() {}
|
||||
|
||||
/* Display for testing */
|
||||
template <class Type>
|
||||
void stack<Type>::display() {
|
||||
node<Type> *current = stackTop;
|
||||
cout << "Top --> ";
|
||||
while (current != NULL) {
|
||||
cout << current->data << " ";
|
||||
current = current->next;
|
||||
}
|
||||
cout << endl;
|
||||
cout << "Size of stack: " << size << endl;
|
||||
}
|
||||
|
||||
/* Determine whether the stack is empty */
|
||||
template <class Type>
|
||||
bool stack<Type>::isEmptyStack() {
|
||||
return (stackTop == NULL);
|
||||
}
|
||||
|
||||
/* Clear stack */
|
||||
template <class Type>
|
||||
void stack<Type>::clear() {
|
||||
stackTop = NULL;
|
||||
}
|
||||
|
||||
/* Add new item to the stack */
|
||||
template <class Type>
|
||||
void stack<Type>::push(Type item) {
|
||||
node<Type> *newNode;
|
||||
newNode = new node<Type>;
|
||||
newNode->data = item;
|
||||
newNode->next = stackTop;
|
||||
stackTop = newNode;
|
||||
size++;
|
||||
}
|
||||
|
||||
/* Return the top element of the stack */
|
||||
template <class Type>
|
||||
Type stack<Type>::top() {
|
||||
assert(stackTop != NULL);
|
||||
return stackTop->data;
|
||||
}
|
||||
|
||||
/* Remove the top element of the stack */
|
||||
template <class Type>
|
||||
void stack<Type>::pop() {
|
||||
node<Type> *temp;
|
||||
if (!isEmptyStack()) {
|
||||
temp = stackTop;
|
||||
stackTop = stackTop->next;
|
||||
delete temp;
|
||||
size--;
|
||||
} else {
|
||||
cout << "Stack is empty !" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
/* Operator "=" */
|
||||
template <class Type>
|
||||
stack<Type> stack<Type>::operator=(stack<Type> &otherStack) {
|
||||
node<Type> *newNode, *current, *last;
|
||||
|
||||
if (stackTop != NULL) /* If stack is no empty, make it empty */
|
||||
stackTop = NULL;
|
||||
if (otherStack.stackTop == NULL)
|
||||
stackTop = NULL;
|
||||
else {
|
||||
current = otherStack.stackTop;
|
||||
stackTop = new node<Type>;
|
||||
stackTop->data = current->data;
|
||||
stackTop->next = NULL;
|
||||
last = stackTop;
|
||||
current = current->next;
|
||||
/* Copy the remaining stack */
|
||||
while (current != NULL) {
|
||||
newNode = new node<Type>;
|
||||
newNode->data = current->data;
|
||||
newNode->next = NULL;
|
||||
last->next = newNode;
|
||||
last = newNode;
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
size = otherStack.size;
|
||||
return *this;
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
/* This class specifies the basic operation on a stack as a linked list */
|
||||
#ifndef STACK_H
|
||||
#define STACK_H
|
||||
|
||||
/* Definition of the node */
|
||||
template <class Type>
|
||||
struct node {
|
||||
Type data;
|
||||
node<Type> *next;
|
||||
};
|
||||
|
||||
/* Definition of the stack class */
|
||||
template <class Type>
|
||||
class stack {
|
||||
public:
|
||||
void display(); /* Show stack */
|
||||
stack(); /* Default constructor*/
|
||||
~stack(); /* Destructor */
|
||||
bool isEmptyStack(); /* Determine whether the stack is empty */
|
||||
void push(Type item); /* Add new item to the stack */
|
||||
Type top(); /* Return the top element of the stack */
|
||||
void pop(); /* Remove the top element of the stack */
|
||||
void clear();
|
||||
|
||||
stack<Type> operator=(stack<Type> &otherStack);
|
||||
// Overload "=" the assignment operator.
|
||||
private:
|
||||
node<Type> *stackTop; /* Pointer to the stack */
|
||||
int size;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,57 +0,0 @@
|
||||
#include <iostream>
|
||||
#include "stack.cpp"
|
||||
#include "stack.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
stack<int> stk;
|
||||
cout << "---------------------- Test construct ----------------------"
|
||||
<< endl;
|
||||
stk.display();
|
||||
cout << "---------------------- Test isEmptyStack ----------------------"
|
||||
<< endl;
|
||||
if (stk.isEmptyStack())
|
||||
cout << "PASS" << endl;
|
||||
else
|
||||
cout << "FAIL" << endl;
|
||||
cout << "---------------------- Test push ----------------------" << endl;
|
||||
cout << "After pushing 10 20 30 40 into stack: " << endl;
|
||||
stk.push(10);
|
||||
stk.push(20);
|
||||
stk.push(30);
|
||||
stk.push(40);
|
||||
stk.display();
|
||||
cout << "---------------------- Test top ----------------------" << endl;
|
||||
int value = stk.top();
|
||||
if (value == 40)
|
||||
cout << "PASS" << endl;
|
||||
else
|
||||
cout << "FAIL" << endl;
|
||||
cout << "---------------------- Test pop ----------------------" << endl;
|
||||
stk.display();
|
||||
stk.pop();
|
||||
stk.pop();
|
||||
cout << "After popping 2 times: " << endl;
|
||||
stk.display();
|
||||
cout << "---------------------- Test overload = operator "
|
||||
"----------------------"
|
||||
<< endl;
|
||||
stack<int> stk1;
|
||||
cout << "stk current: " << endl;
|
||||
stk.display();
|
||||
cout << endl << "Assign stk1 = stk " << endl;
|
||||
stk1 = stk;
|
||||
stk1.display();
|
||||
cout << endl << "After pushing 8 9 10 into stk1:" << endl;
|
||||
stk1.push(8);
|
||||
stk1.push(9);
|
||||
stk1.push(10);
|
||||
stk1.display();
|
||||
cout << endl << "stk current: " << endl;
|
||||
stk.display();
|
||||
cout << "Assign back stk = stk1:" << endl;
|
||||
stk = stk1;
|
||||
stk.display();
|
||||
return 0;
|
||||
}
|
||||
41
data_structures/test_queue.cpp
Normal file
41
data_structures/test_queue.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "./queue.h"
|
||||
|
||||
int main() {
|
||||
queue<std::string> q;
|
||||
std::cout << "---------------------- Test construct ----------------------"
|
||||
<< std::endl;
|
||||
q.display();
|
||||
std::cout
|
||||
<< "---------------------- Test isEmptyQueue ----------------------"
|
||||
<< std::endl;
|
||||
if (q.isEmptyQueue())
|
||||
std::cout << "PASS" << std::endl;
|
||||
else
|
||||
std::cout << "FAIL" << std::endl;
|
||||
std::cout << "---------------------- Test enQueue ----------------------"
|
||||
<< std::endl;
|
||||
std::cout << "After Hai, Jeff, Tom, Jkingston go into queue: " << std::endl;
|
||||
q.enQueue("Hai");
|
||||
q.enQueue("Jeff");
|
||||
q.enQueue("Tom");
|
||||
q.enQueue("Jkingston");
|
||||
q.display();
|
||||
std::cout << "---------------------- Test front ----------------------"
|
||||
<< std::endl;
|
||||
std::string value = q.front();
|
||||
if (value == "Hai")
|
||||
std::cout << "PASS" << std::endl;
|
||||
else
|
||||
std::cout << "FAIL" << std::endl;
|
||||
std::cout << "---------------------- Test deQueue ----------------------"
|
||||
<< std::endl;
|
||||
q.display();
|
||||
q.deQueue();
|
||||
q.deQueue();
|
||||
std::cout << "After Hai, Jeff left the queue: " << std::endl;
|
||||
q.display();
|
||||
return 0;
|
||||
}
|
||||
59
data_structures/test_stack.cpp
Normal file
59
data_structures/test_stack.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "./stack.h"
|
||||
|
||||
int main() {
|
||||
stack<int> stk;
|
||||
std::cout << "---------------------- Test construct ----------------------"
|
||||
<< std::endl;
|
||||
stk.display();
|
||||
std::cout
|
||||
<< "---------------------- Test isEmptyStack ----------------------"
|
||||
<< std::endl;
|
||||
if (stk.isEmptyStack())
|
||||
std::cout << "PASS" << std::endl;
|
||||
else
|
||||
std::cout << "FAIL" << std::endl;
|
||||
std::cout << "---------------------- Test push ----------------------"
|
||||
<< std::endl;
|
||||
std::cout << "After pushing 10 20 30 40 into stack: " << std::endl;
|
||||
stk.push(10);
|
||||
stk.push(20);
|
||||
stk.push(30);
|
||||
stk.push(40);
|
||||
stk.display();
|
||||
std::cout << "---------------------- Test top ----------------------"
|
||||
<< std::endl;
|
||||
int value = stk.top();
|
||||
if (value == 40)
|
||||
std::cout << "PASS" << std::endl;
|
||||
else
|
||||
std::cout << "FAIL" << std::endl;
|
||||
std::cout << "---------------------- Test pop ----------------------"
|
||||
<< std::endl;
|
||||
stk.display();
|
||||
stk.pop();
|
||||
stk.pop();
|
||||
std::cout << "After popping 2 times: " << std::endl;
|
||||
stk.display();
|
||||
std::cout << "---------------------- Test overload = operator "
|
||||
"----------------------"
|
||||
<< std::endl;
|
||||
stack<int> stk1;
|
||||
std::cout << "stk current: " << std::endl;
|
||||
stk.display();
|
||||
std::cout << std::endl << "Assign stk1 = stk " << std::endl;
|
||||
stk1 = stk;
|
||||
stk1.display();
|
||||
std::cout << std::endl << "After pushing 8 9 10 into stk1:" << std::endl;
|
||||
stk1.push(8);
|
||||
stk1.push(9);
|
||||
stk1.push(10);
|
||||
stk1.display();
|
||||
std::cout << std::endl << "stk current: " << std::endl;
|
||||
stk.display();
|
||||
std::cout << "Assign back stk = stk1:" << std::endl;
|
||||
stk = stk1;
|
||||
stk.display();
|
||||
return 0;
|
||||
}
|
||||
@@ -8,29 +8,26 @@
|
||||
* ./main student.txt
|
||||
************************************************************
|
||||
* */
|
||||
#include <assert.h>
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "stack.cpp"
|
||||
#include "stack.h"
|
||||
|
||||
using namespace std;
|
||||
#include "./stack.h"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
double GPA;
|
||||
double highestGPA;
|
||||
string name;
|
||||
std::string name;
|
||||
|
||||
assert(argc == 2);
|
||||
ifstream infile;
|
||||
stack<string> stk;
|
||||
std::ifstream infile;
|
||||
stack<std::string> stk;
|
||||
|
||||
infile.open(argv[1]);
|
||||
cout << fixed << showpoint;
|
||||
cout << setprecision(2);
|
||||
std::cout << std::fixed << std::showpoint;
|
||||
std::cout << std::setprecision(2);
|
||||
infile >> GPA >> name;
|
||||
highestGPA = GPA;
|
||||
|
||||
@@ -44,12 +41,12 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
infile >> GPA >> name;
|
||||
}
|
||||
cout << "Highest GPA: " << highestGPA << endl;
|
||||
cout << "Students the highest GPA are: " << endl;
|
||||
std::cout << "Highest GPA: " << highestGPA << std::endl;
|
||||
std::cout << "Students the highest GPA are: " << std::endl;
|
||||
while (!stk.isEmptyStack()) {
|
||||
cout << stk.top() << endl;
|
||||
std::cout << stk.top() << std::endl;
|
||||
stk.pop();
|
||||
}
|
||||
cout << endl;
|
||||
std::cout << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user