Revert "Update Assignment"

This reverts commit 58d406a1add551be02688fc4cbc3c5462b20e957.
This commit is contained in:
miti99 committed 2018-10-22 11:03:21 +07:00
1 parent 8ce89d7334
commit 7d31b6234c
6 files changed
-371

No files matched your search

-87
View File
@@ -1,87 +0,0 @@
/* Copyright (C) 2018
* Course: CO2003
* Author: Rang Nguyen
* Ho Chi Minh City University of Technology
*/
#include"Node.h"
Node::Node(int capacity) {
maxElements = capacity;
elements = new int[maxElements];
numElements = 0;
prev = next = NULL;
}
Node::~Node() {
if (elements != NULL)
delete[] elements;
}
int Node::getHalfNodeSize() {
return (int)ceil(maxElements/2.0);
}
bool Node::isUnderHalfFull() {
return numElements < getHalfNodeSize();
}
bool Node::isFull() {
return numElements >= maxElements;
}
bool Node::isOverflow() {
return numElements > maxElements;
}
bool Node::isEmpty() {
return numElements == 0;
}
void Node::add(int val) {
if (isFull())
throw "NodeOverflowExeception";
else
{
elements[numElements] = val;
numElements++;
}
}
void Node::insertAt(int pos, int val) {
if (isFull()) throw "NodeOverflowExeception";
else if (pos < 0 || pos > numElements) throw "IndexOutOfBoundsException";
else {
memmove(elements + pos + 1, elements + pos, (numElements - pos)*sizeof(int));
elements[pos] = val;
numElements++;
}
}
void Node::removeAt(int pos) {
if (pos < 0 || pos >= numElements) throw "IndexOutOfBoundsException";
else {
memmove(elements + pos, elements + pos + 1, (numElements - pos - 1)*sizeof(int));
numElements--;
}
}
void Node::reverse() {
for(int i = 0; i<numElements/2;i++)
std::swap(elements[i], elements[numElements - 1 - i]);
}
void Node::print() {
for (int i = 0; i < numElements; i++)
printf("%d ", elements[i]);
}
void Node::printDetail() {
printf("| prev(%d) |", prev);
for (int i = 0; i < numElements; i++)
printf(" %d |", elements[i]);
for (int i = numElements; i < maxElements; i++)
printf(" X |");
printf(" next(%d) |\n", next);
}
-36
View File
@@ -1,36 +0,0 @@
/* Copyright (C) 2018
* Course: CO2003
* Author: Rang Nguyen
* Ho Chi Minh City University of Technology
*/
#pragma once
#include<iostream>
// Unrolled Linked List Node
class Node {
private:
int maxElements;
public:
int numElements; // number of elements in this node, up to maxElements
int* elements; // an array of numElements elements,
Node *next; // reference to the next node in the list
Node *prev; // reference to the previous node in the list
Node(int capacity = 5);
~Node();
int getHalfNodeSize();
bool isUnderHalfFull();
bool isFull();
bool isOverflow();
bool isEmpty();
void add(int val); // append val element to the end of the node
void insertAt(int pos, int val); // insert val to position pos of the node
void removeAt(int pos); // remove the position pos from the node
void reverse(); // reverse the content of the node
void print(); // print the content of the node
void printDetail(); // print the detail content of the node
};
-61
View File
@@ -1,61 +0,0 @@
#include"UnrolledLinkedList.h"
void UnrolledLinkedList::add(int val) {
// TODO
}
int UnrolledLinkedList::getAt(int pos) {
if (pos < 0 || pos >= size)
throw "IndexOutOfBoundsException"; // check whether pos is valid or not
// TODO
}
void UnrolledLinkedList::setAt(int pos, int val) {
if (pos < 0 || pos >= size)
throw "IndexOutOfBoundsException"; // check whether pos is valid or not
// TODO
}
void UnrolledLinkedList::insertAt(int pos, int val) {
if (pos < 0 || pos > size)
throw "IndexOutOfBoundsException"; // check whether pos is valid or not
// TODO
}
void UnrolledLinkedList::deleteAt(int pos)
{
if (pos < 0 || pos >= size)
throw "IndexOutOfBoundsException"; // check whether pos is valid or not
// TODO
}
int UnrolledLinkedList::firstIndexOf(int val) {
// TODO
}
int UnrolledLinkedList::lastIndexOf(int val) {
// TODO
}
bool UnrolledLinkedList::contains(int val) {
// TODO
}
void UnrolledLinkedList::reverse() {
// TODO
}
int* UnrolledLinkedList::toArray() {
// TODO
}
-87
View File
@@ -1,87 +0,0 @@
/* Copyright (C) 2018
* Course: CO2003
* Author: Rang Nguyen
* Ho Chi Minh City University of Technology
*/
#pragma once
#include"Node.h"
class UnrolledLinkedList {
private:
Node* head;
Node* tail;
int size;
int numOfNodes;
int nodeSize;
public:
// Your tasks: complete the implementation of the below methods in UnrolledLinkedList.h
void add(int val);
bool contains(int val);
int getAt(int pos);
void setAt(int pos, int val);
int firstIndexOf(int val);
int lastIndexOf(int val);
void insertAt(int pos, int val);
void deleteAt(int pos);
void reverse();
int* toArray();
UnrolledLinkedList(int capacity) {
nodeSize = capacity;
head = tail = NULL;
size = numOfNodes = 0;
}
~UnrolledLinkedList() {
Node* p;
while (p = head) {
head = head->next;
delete p;
}
size = numOfNodes = 0;
head = tail = NULL;
}
int getSize() { return size; }
int getHalfNodeSize() {return (int)ceil(nodeSize / 2.0);}
void print() {
printf("=================METADATA================\n");
printf("Size = %d, nodeSize = %d, halfNodeSize = %d\n", size, nodeSize, getHalfNodeSize());
printf("-----------------CONTENTS----------------\n");
Node* p = head;
if (p == NULL) {
printf("NULL\n");
return;
}
while (p != NULL) {
// check whether your implementation satisfied the requirements
if ((p->isUnderHalfFull() && size >= getHalfNodeSize()) ||
p->isOverflow()) {
throw "Something wrong with your implementation";
}
p->print();
p = p->next;
}
printf("\n-------------------END-------------------\n");
}
void printDetail() {
printf("=================METADATA================\n");
printf("Size = %d, numOfNodes = %d, nodeSize = %d\n", size, numOfNodes, nodeSize);
printf("-----------------CONTENTS----------------\n");
Node* p = head;
if (p == NULL) printf("NULL\n");
while (p != NULL) {
p->printDetail();
p = p->next;
}
printf("-------------------END-------------------\n");
}
};
-5
View File
@@ -1,5 +0,0 @@
u 4
a 5
a 7
i 0 1
p
-95
View File
@@ -1,95 +0,0 @@
/* Copyright (C) 2018
* Course: CO2003
* Author: Rang Nguyen
* Ho Chi Minh City University of Technology
*/
#include"UnrolledLinkedList.h"
#include<fstream>
using namespace std;
int main()
{
UnrolledLinkedList* list = NULL;
ifstream ifs;
ifs.open("input.txt");
char command;
int pos, val;
try
{
while (ifs >> command) {
switch (command) {
case 'u': // create an object of UnrolledLinkedList
ifs >> val;
if (list != NULL) delete list;
list = new UnrolledLinkedList(val);
break;
case 'a': // append an element the the end of the list
ifs >> val;
list->add(val);
break;
case 'c': // check if an element is in the list or not
ifs >> val;
printf("The list contains %d: %d\n", val, list->contains(val));
break;
case 'd': // delete an element at pos position in the list
ifs >> pos;
list->deleteAt(pos);
break;
case 'f': // return the index of the first occurence of a specific element in the list
ifs >> val;
printf("First occurrence of %d is at %d\n", val, list->firstIndexOf(val));
break;
case 'g': // returns the element at the specified position in this list.
ifs >> pos;
printf("The element at position %d is %d\n", pos, list->getAt(pos));
break;
case 'i': // insert a new element to pos position in the list
ifs >> pos >> val;
list->insertAt(pos, val);
break;
case 'l': // return the index of the last occurence of a specific element in the list
ifs >> val;
printf("Last occurrence of %d is at %d\n", val, list->lastIndexOf(val));
break;
case 'p': // print the content of the list
list->print();
break;
case 'r': // reverse the list
list->reverse();
break;
case 's': // replaces the element at the specified position in this list with the specified element.
ifs >> pos >> val;
list->setAt(pos, val);
break;
case 't': //returns a pointer to an array containing all of the elements in this list in proper sequence(from first to last element).
{
int* arr = list->toArray();
int n = list->getSize();
printf("The list after converted to array:\n");
if (n > 0) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
delete[] arr;
}
else {
printf("NULL\n");
}
break;
}
case 'w': // print the detail content of the list
list->printDetail();
break;
default:
throw "Wrong input format!";
}
}
}
catch (char const* s) {
printf("An exception occurred. Exception type: %s\n", s);
}
if (list != NULL) delete list;
ifs.close();
return 0;
}