Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is the SLinkedList.h in the github / Code from: // Data Structures and Algo

ID: 3588190 • Letter: T

Question

This is the SLinkedList.h in the github

/ Code from:
// Data Structures and Algorithms in C++, Goodrich, Tamassia, and Mount, 2nd Ed., 2011.
//

#pragma once

#include <stdexcept>

using namespace std;

template <typename E> class SLinkedList; // forward declaration to be used when declaring SNode

template <typename E>
class SNode {     // singly linked list node
private:
E elem;     // linked list element value
SNode<E> *next;    // next item in the list
friend class SLinkedList<E>;  // provide SLinkedList access
};

template <typename E>
class SLinkedList {    // a singly linked list
public:
SLinkedList();    // empty list constructor
~SLinkedList();    // destructor
bool empty() const;   // is list empty?
E& front();     // return front element
void addFront(const E& e);  // add to front of list
void removeFront();   // remove front item list
int size() const;     // list size
private:
SNode<E>* head;    // head of the list
int     n;       // number of items
};

template <typename E>
SLinkedList<E>::SLinkedList()   // constructor
: head(NULL), n(0) { }

template <typename E>
bool SLinkedList<E>::empty() const  // is list empty?
{
return head == NULL; // can also use return (n == 0);
}

template <typename E>
E& SLinkedList<E>::front()   // return front element
{
if (empty()) throw length_error("empty list");
return head->elem;
}

template <typename E>
SLinkedList<E>::~SLinkedList()   // destructor
{
while (!empty()) removeFront();
}

template <typename E>
void SLinkedList<E>::addFront(const E& e) { // add to front of list
SNode<E>* v = new SNode<E>;  // create new node
v->elem = e;    // store data
v->next = head;    // head now follows v
head = v;    // v is now the head
n++;
}

template <typename E>
void SLinkedList<E>::removeFront() {  // remove front item
if (empty()) throw length_error("empty list");
SNode<E>* old = head;   // save current head
head = old->next;   // skip over old head
delete old;    // delete the old head
n--;
}

template <typename E>
int SLinkedList<E>::size() const {    // list size
return n;
}

#3 [4 points] Given a singly linked list as in Problem 2, write a recursive function to re-orient the the next pointers of all the nodes in a list to each point to the previous element. Use the code available from SLinkedListh on our Github https://github.com/apanangadan/CSUF-CPSC 131 with modification to illustrate your approach. For the example given in Problem 2, the linked list will look like: I wan l 61.4 1 ! Rocio | 93.2 | Dan 79.1Ann | 67.0 |

Explanation / Answer

#include<iostream>

#include "SLinkedList.h"

using namespace std;

struct data

{

string name;

float no;

};

void lastPush(SLinkedList<data> *l,data d)

{

if(l->empty())

l->addFront(d);

else

{

data a=l->front();

l->removeFront();

lastPush(l,d);

l->addFront(a);

}

}

void reverse(SLinkedList<data> *l)

{

if(!l->empty())

{

data d=l->front();

l->removeFront();

reverse(l);

lastPush(l,d);

}

}

void display(SLinkedList<data> *l)

{

if(!l->empty())

{

data d=l->front();

cout<<d.name<<" "<<d.no<<" ";

l->removeFront();

display(l);

l->addFront(d);

}

}

int main()

{

SLinkedList<data> l;

data d;

//---------------------------------inputs------------------------------

d.name="Ann";

d.no=67.0;

l.addFront(d);

d.name="Dan";

d.no=79.1;

l.addFront(d);

d.name="Rick";

d.no=90.1;

l.addFront(d);

d.name="Rocio";

d.no=93.2;

l.addFront(d);

d.name="Wan";

d.no=61.4;

l.addFront(d);

//--------------------------------------------------------------------

display(&l);

reverse(&l);

cout<<endl;

display(&l);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote