Hello in next program SLListADT::~SLListADT() { for(node *p;!isEmpty();) { p = h
ID: 3704436 • Letter: H
Question
Hello
in next program
SLListADT::~SLListADT()
{
for(node *p;!isEmpty();)
{
p = head->next;
delete head;
head = p;
}
}
bool SLListADT::isEmpty()
{
return head == NULL;
}
int SLListADT::countNodes()
{
return count;
}
void SLListADT::printList()
{
if(count > 0)
{
for(node *tmp = head; tmp!= NULL; tmp = tmp->next)
{
cout << tmp->info << " ";
}
cout << endl;
}
}
void SLListADT::initList()
{
for (node *p; !isEmpty(); )
{
p = head->next;
delete head;
head = p;
}
count = 0;
head = NULL;
tail = NULL;
}
void SLListADT::insertToHead(int a)
{
head = new node(a, head);
if (tail == NULL)
tail = head;
count++;
}
void SLListADT::insertToTail(int b)
{
if (tail != NULL)
{
tail->next = new node(b);
tail = tail->next;
}
else
head = tail = new node(b);
count++;
}
int SLListADT::removeFromHead()
{
if(!isEmpty())
{
int vla = head;
}
}
int SLListADT::removeFromTail()
{
}
bool SLListADT::find(int c) const
{
}
i want ask how to do the function for:
int removeFromHead(); // function to remove the head and return its info;
int removeFromTail(); // function to remove the tail and return its info;
bool find(int) const; // function to check if at-least one instance of given value exists in the list
void insertAt(int, int); // function to insert a value at the given position in the list
void removeAt(int); // function to remove a node from the list at given position
Explanation / Answer
int SLListADT::removeFromHead(){
if (head != NULL){
node *p = head;
head = head->next;
int a = p->info;
delete p;
count--;
return a;
}
return -1 // Error situation
}
int SLListADT::removeFromTail(){
if (tail != NULL){
if (tail == head){
int a = tail->info;
delete tail;
tail = NULL;
head = NULL;
count--;
return a;
}
if (tail != head){
int a = tail->info
node *q = head;
while (q->next != tail){
q = q->next;
}
delete tail;
tail = q;
count--;
return a;
}
}
return -1 // Error situation
}
int SLListADT::find(int a) const {
if (head != NULL){
node *q = head;
while (q != NULL){
if (q->info == a)
return true;
}
}
return false;
}
void SLListADT::insertAt(int val, int pos) const {
node *p = head;
if (pos < 0){
cout << "Invalid index ";
}
if (p == NULL && pos == 0){ // starting index as 0
head = new node(val, head);
tail = head;
}
if (p == NULL && pos > 0){ // starting index as 0
cout << "Invalid index ";
}
if (p != NULL && pos == 0){ // starting index as 0
head = new node(val, head);
}
if (p != NULL && pos > 0){ // starting index as 0
node *q = head;
count = 0;
while (q != NULL && count < pos-1){
q = q->next;
count++;
}
q->next = new node(val,q->next);
}
}
void SLListADT::removetAt(int val, int pos) const {
node *p = head;
if (pos < 0){
cout << "Invalid index ";
}
if (p == NULL && pos == 0){ // starting index as 0
head = new node(val, head);
tail = head;
count--;
}
if (p == NULL && pos > 0){ // starting index as 0
cout << "Invalid index ";
}
if (p != NULL && pos == 0){ // starting index as 0
head = head->next;
count--;
}
if (p != NULL && pos > 0){ // starting index as 0
node *q = head;
count1 = 0;
while (q != NULL && count1 < pos-1){
q = q->next;
count1++;
}
node *p = q->next;
q->next = p->next;
delete p;
count--;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.