I am doing a project for my communication networks class in C++. Basically I am given a set of digits "1010011010" and I have to convert it into a bandwidth NRZ-L, NRZ-I, B-AMI, Manchester and D-Manchester.
I was able to do the first two bandwidths and I am currently doing the B-AMI. So what I created was a doubly linked list that holds the digits and a pointer "current" that points to each node. Then I created an array that will store a string depending on the data that the pointer is pointing to. This is what it looks like on paper.

There is a "c" for my current pointer, a "t" for my temp pointer, an "i" for my index in the array and a "j" so that it can move backwards.
As you can see, I commented out my else if statement under my BAMI function. If I were to uncomment it, and try to run it, the program will crash. My BAMI function is supposed to output "-V 0 +V 0 0 -V +V 0 -V 0".
If anyone can help me out I would gladly appreciate it.
Thanks!
#include <iostream>
#include <string>
using namespace std;
template <class OPERATOR>
class doublyLinkedList{
private:
class node{
public:
OPERATOR data;
node * next;
node * prev;
};
node * head; //ptr to first node in list
node * tail; //ptr to last node in list
public:
void insert_at_back(OPERATOR x){
node * ptr = new node;
ptr->data = x;
if (tail == NULL){ //empty list
head = ptr;
ptr->prev = NULL;
tail = ptr;
ptr->next = NULL;
}else{ //list not empty
tail->next = ptr;
ptr->prev = tail;
ptr->next = NULL;
tail = ptr;
}
}
void printList(){
node * zombie = head;
while (zombie != NULL){
cout << zombie->data;
zombie = zombie->next;
}
}
//NRZ-L Formula
void NRZL(){
node * current = head;
while (current->next != NULL){
if (current->data == 1){
cout << "|-V ";
current = current->next;
}else if (current->data == 0){
cout << "|+V ";
current = current->next;
}
}
if (tail->data == 0){
cout << "|+V |" << endl;
}else{
cout << "|-V |" << endl;
}
}
//NRZ-I Formula
void NRZI(){
node * current = head;
string value[10];
while (current != NULL){
for (int i = 0; i < 10; i++){
if ( (current->data == 1 || current->data == 0)
&& current->prev == NULL){
//If first bit in node is 1 or 0, it will be -V
value[i] = "|-V ";
}else if (current->data == 0 && current->prev->data == 1){
//Checking if previous is 1 and current is 0
value[i] = value[i - 1];
}else if (current->data == 0 && current->prev->data == 0){
//Checking if previous and current is 0
value[i] = value[i - 1];
}else if (current->data == 1 && current->prev->data == 1){
//Checking is previous and current is 1
if (value[i - 1] == "|-V "){
//Checking is previous index is -V
value[i] = "|+V ";
}else{ //Checking is previous index is +V
value[i] = "|-V ";
}
}else if (current->data == 1
&& current->prev->data == 0){
//Checking is previous is 0 and current is 1
if (value[i - 1] == "|-V "){
//Checking if previous index is -V
value[i] = "|+V ";
}else{ //Checking if previous index is +V
value[i] = "|-V ";
}
}
current = current->next;
}
}
for (int i = 0; i < 10; i++)//Printing the array
cout << value[i];
cout << "|" << endl;
}
//B-AMI formula
void BAMI(){////Fix this code. Keeps crashing
node * current = head;
string value[10];
while (current != NULL){
for (int i = 0; i < 10; i++){
if ((current->data == 1 || current->data == 0)
&& current->prev == NULL){
//If current is first bit it will be -V
value[i] = "|-V ";
}
//else if (current->data == 1 && current->prev->data != 1){
// //If current is 1 and prev is 0
// node * temp = current->prev;//
// for (int j = i-1; j < i; j--){
// if (temp->prev->data != 1){
// temp = temp->prev;
// }else{
// if (value[j-1] == "|-V "){
// value[i] = "|+V ";
// }else if (value[j-1] == "|+V "){
// value[i] = "|-V ";
// }
// }
// }
// delete temp;
//}
else{//If current is 0
value[i] = "|0 ";
}
current = current->next;
}
}
for (int i = 0; i < 10; i++)//Printing the array
cout << value[i];
cout << "|" << endl;
}
//Manchester formula
//D-Manchester formula
//constructor
doublyLinkedList(){
head = NULL;
tail = NULL;
}
};
int main(){
doublyLinkedList<int> numberList;
cout << "This is the original binary code:";
numberList.insert_at_back(1);
numberList.insert_at_back(0);
numberList.insert_at_back(1);
numberList.insert_at_back(0);
numberList.insert_at_back(0);
numberList.insert_at_back(1);
numberList.insert_at_back(1);
numberList.insert_at_back(0);
numberList.insert_at_back(1);
numberList.insert_at_back(0);
cout << endl;
numberList.printList();
cout << endl;
cout << "=========================================" << endl << endl;
cout << "This is NRZ-L:" << endl;
numberList.NRZL();
cout << "=========================================" << endl << endl;
cout << "This is NRZ-I:" << endl;
numberList.NRZI();
cout << "=========================================" << endl << endl;
cout << "This is B-AMI:" << endl;
numberList.BAMI();///fix function for this code to work
cout << "=========================================" << endl << endl;
system("pause");
return 0;
}