1. Operations on Linked Lists [30 point] Part A: Operations on Head-Only Lists [20 points] Implement a data type List th
Posted: Thu May 05, 2022 1:34 pm
c++
#ifndef LIST_H
#define LIST_H
struct Node {
int val;
Node* next;
};
class List {
public:
List();
virtual ~List();
void AddAsTail(int i);
void AddAsTailV2(int i);
protected:
private:
Node* head;
Node* tail; // this is for Part B
};
#include "List.h"
List::List()
{
//ctor
}
List::~List()
{
//dtor
}
#include <iostream>
using namespace std;
int main()
{
cout << "Assignment - Q1" << endl;
List t;
t.AddAsHead(15);
t.AddAsTail(10);
t.AddAsTail(14);
t.AddAsHead(14);
t.RemoveFirst(14);
t.Print();
return 0;
}
1. Operations on Linked Lists [30 point] Part A: Operations on Head-Only Lists [20 points] Implement a data type List that realizes linked lists consisting of nodes with integer values. A class of type List has one field, a pointer to a Node head and with the following functions. The structure of type Node has two fields, an integer value and (a pointer to) a Node next. The type List must have the following methods: boolean IsEmpty() returns true when List is empty; int LengthIs() void Print() returns the number of nodes in the list, which is 0 for the empty list; print the content of all nodes; void AddAsHead(int i) creates a new node with the integer and adds it to the beginning of the list; void AsTail(int i) creates a new node with the integer and adds it to the end of the list; Node Find(int i) returns the first node with value i; void Reverse() reverses the list; int PopHead() returns the value of the head of the list and removes the node, if the list is nonempty, otherwise returns NULL; void RemoveFirst(int i) void RemoveAll(int i) removes the first node with value i; removes all nodes with value i; void AddAll(List 1) appends the list 1 to the last element of the current list, if the current list is nonempty, or let the head of the current list point to the first element of 1 if the current list is empty. Part B: Operations on Head-Tail Lists [10 points] Suppose we include another pointer in the class, and it points to the tail of the list. To accommodate and take advantage of the new pointer, we need to modify some methods. Write the new versions of the methods named as V2 where you need to manage the tail pointer. For example, if you need to modify funcOne() then add a new function funcOne V2(). CodeBlocks Details Project Name: AsnListDemo, Files: List.h, List.cpp, main.cpp
#ifndef LIST_H
#define LIST_H
struct Node {
int val;
Node* next;
};
class List {
public:
List();
virtual ~List();
void AddAsTail(int i);
void AddAsTailV2(int i);
protected:
private:
Node* head;
Node* tail; // this is for Part B
};
#include "List.h"
List::List()
{
//ctor
}
List::~List()
{
//dtor
}
#include <iostream>
using namespace std;
int main()
{
cout << "Assignment - Q1" << endl;
List t;
t.AddAsHead(15);
t.AddAsTail(10);
t.AddAsTail(14);
t.AddAsHead(14);
t.RemoveFirst(14);
t.Print();
return 0;
}
1. Operations on Linked Lists [30 point] Part A: Operations on Head-Only Lists [20 points] Implement a data type List that realizes linked lists consisting of nodes with integer values. A class of type List has one field, a pointer to a Node head and with the following functions. The structure of type Node has two fields, an integer value and (a pointer to) a Node next. The type List must have the following methods: boolean IsEmpty() returns true when List is empty; int LengthIs() void Print() returns the number of nodes in the list, which is 0 for the empty list; print the content of all nodes; void AddAsHead(int i) creates a new node with the integer and adds it to the beginning of the list; void AsTail(int i) creates a new node with the integer and adds it to the end of the list; Node Find(int i) returns the first node with value i; void Reverse() reverses the list; int PopHead() returns the value of the head of the list and removes the node, if the list is nonempty, otherwise returns NULL; void RemoveFirst(int i) void RemoveAll(int i) removes the first node with value i; removes all nodes with value i; void AddAll(List 1) appends the list 1 to the last element of the current list, if the current list is nonempty, or let the head of the current list point to the first element of 1 if the current list is empty. Part B: Operations on Head-Tail Lists [10 points] Suppose we include another pointer in the class, and it points to the tail of the list. To accommodate and take advantage of the new pointer, we need to modify some methods. Write the new versions of the methods named as V2 where you need to manage the tail pointer. For example, if you need to modify funcOne() then add a new function funcOne V2(). CodeBlocks Details Project Name: AsnListDemo, Files: List.h, List.cpp, main.cpp