Question: THIS CODE NEEDS TO BE TESTED FUNCTION BY FUNCTION WITH THE DISPLAYED ANWERS IN COMMENT FORM NEXT TO EACH FUNCTION THANK YOU CODE: class List

THIS CODE NEEDS TO BE TESTED FUNCTION BY FUNCTION

WITH THE DISPLAYED ANWERS IN COMMENT FORM NEXT TO EACH FUNCTION

THANK YOU

CODE:

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;

}

}

/**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--;

}

}

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: "<

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!