In: Computer Science
This C++ CODE has many functions that needs to be tested piece by piece
For example, you going to display each function with [ ] and a , separating each piece of data, like so: [1,2,3,4]?
results must be copied and pasted as commnets at the bottom of this file
this is the code:
#include <iostream>
#include <string>
#include <cstddef> //for NULL
using namespace std;
class List
{
private:
struct Node
{
int data;
Node* next;
Node(int data): data(data), next(NULL){}
};
typedef struct Node* Nodeptr;
Nodeptr first;
Nodeptr last;
int size;
public:
/**Constructors */
List()
{
first = NULL;
last = NULL;
size = 0;
}
/* Destructors*/
~List()
{
Nodeptr cursor = first;
Nodeptr temp;
while (cursor != NULL)
{
temp = cursor -> next;
delete cursor;
cursor = temp;
}
}
//ANOTHER WAY TO WRITE
//AND YES IT WORKS
/* last = NULL;
while(first != NULL)
{
Nodeptr temp = first;
first = first->next;
delete temp;
} /*
/**Accessors*/
int getFirst()
{
if(isEmpty())
return 0;
return first->data;
}
int getLast()
{
if(isEmpty())
return 0;
return last->data;
}
bool isEmpty()
{
if(first == NULL)
{
return true;
}
else return false;
}
int getSize()
{
return size; // return the size of the list
}
/**Manipulation Procedures*/
void removeLast()
{
if(isEmpty() == false) // if list is not empty
{
if(first -> next == NULL) // list has only 1 node
{
delete first;
first = NULL;
last = NULL;
}
else
{
Nodeptr temp = first;
while(temp -> next != last)
{
temp = temp -> next; //advance the pointer
}
delete last; //free the memory for the original last node
temp -> next = NULL; //so last->next is not pointing at freed memory
last = temp;
size -- ; // decreasing size
}
}
}
void removeFirst()
{
if (size == 0)
{
cout << "removeFirst: List is empty. no data to remove." << endl;
}
else if (size == 1)
{
delete first;
first = last = NULL;
size = 0;
}
else
{
Nodeptr temp = first; //store pointer to first so we dont lose access
first = first -> next; //advance the pointer
delete temp; //free the memory for the original first node
size--;
}
}
/*{
if(isEmpty() == false)
{
if(first -> next == NULL)// list has only one node
{
first = NULL;
last = NULL;
delete first;
}
else
{
Nodeptr temp = first;
first = first -> next;
delete temp;
}
size = size - 1;
}
} */
void insertLast(int data)
{
if(first == NULL)
{
first = new Node(data);
last = first;
}
else
{
last -> next = new Node(data);
last = last -> next;
}
size++;
}
void insertFirst(int data)
{
if (size == 0)
{
first = new Node(data);
last = first;
}
else
{
Nodeptr N = new Node(data);
N -> next = first;
first = N;
}
size ++;
/*if(first == NULL)
{
first = new Node(data);
last = first;
}
else
{
Nodeptr temp = new Node(data);
temp -> next = first;
first = temp;
}
size++; */
}
/**Additional List Operations*/
void printList()
{
Nodeptr temp = first;
while(temp != NULL)
{
cout << temp -> data <<" ";
temp = temp -> next;
}
cout << endl;
}
};
int main()
{
List list;
list.insertFirst(2);
list.insertFirst(4);
list.printList();
list.removeFirst();
list.printList();
cout<<"Size: "<<list.getSize()<<endl;
return 0;
}
#include <iostream>
#include <string>
#include <cstddef> //for NULL
using namespace std;
class List
{
private:
struct Node
{
int data;
Node* next;
Node(int data): data(data), next(NULL){}
};
typedef struct Node* Nodeptr;
Nodeptr first;
Nodeptr last;
int size;
public:
/**Constructors */
List()
{
first = NULL;
last = NULL;
size = 0;
}
/* Destructors*/
~List()
{
Nodeptr cursor = first;
Nodeptr temp;
while (cursor != NULL)
{
temp = cursor -> next;
delete cursor;
cursor = temp;
}
}
//ANOTHER WAY TO WRITE
//AND YES IT WORKS
/* last = NULL;
while(first != NULL)
{
Nodeptr temp = first;
first = first->next;
delete temp;
} /*
/**Accessors*/
int getFirst()
{
if(isEmpty())
return 0;
return first->data;
}
int getLast()
{
if(isEmpty())
return 0;
return last->data;
}
bool isEmpty()
{
if(first == NULL)
{
return true;
}
else return false;
}
int getSize()
{
return size; // return the size of the list
}
/**Manipulation Procedures*/
void removeLast()
{
if(isEmpty() == false) // if list is not empty
{
if(first -> next == NULL) // list has only 1 node
{
delete first;
first = NULL;
last = NULL;
}
else
{
Nodeptr temp = first;
while(temp -> next != last)
{
temp = temp -> next; //advance the pointer
}
delete last; //free the memory for the original last node
temp -> next = NULL; //so last->next is not pointing at freed
memory
last = temp;
size -- ; // decreasing size
}
}
}
void removeFirst()
{
if (size == 0)
{
cout << "removeFirst: List is empty. no data to remove."
<< endl;
}
else if (size == 1)
{
delete first;
first = last = NULL;
size = 0;
}
else
{
Nodeptr temp = first; //store pointer to first so we dont lose
access
first = first -> next; //advance the pointer
delete temp; //free the memory for the original first node
size--;
}
}
/*{
if(isEmpty() == false)
{
if(first -> next == NULL)// list has only one node
{
first = NULL;
last = NULL;
delete first;
}
else
{
Nodeptr temp = first;
first = first -> next;
delete temp;
}
size = size - 1;
}
} */
void insertLast(int data)
{
if(first == NULL)
{
first = new Node(data);
last = first;
}
else
{
last -> next = new Node(data);
last = last -> next;
}
size++;
}
void insertFirst(int data)
{
if (size == 0)
{
first = new Node(data);
last = first;
}
else
{
Nodeptr N = new Node(data);
N -> next = first;
first = N;
}
size ++;
}
/**Additional List Operations*/
void printList()
{
Nodeptr temp = first;
cout <<"[";
while(temp != NULL)
{
cout << temp -> data <<",";
temp = temp -> next;
}
cout <<"]"<< endl;
}
};
int main()
{
List list;
list.insertFirst(1);
list.printList(); // this should print [1]
list.insertFirst(2);
list.printList(); // this should print [2,1]
list.insertLast(3);
list.printList(); // this should print [2,1,3]
list.insertFirst(4);
list.printList(); // this should print [4,2,1,3]
list.removeFirst();
list.printList(); // this should print [2,1,3]
cout<<"Size: "<<list.getSize()<<endl;
return 0;
}