Design your own linked list class to hold a series of integers . The class shoul
ID: 3610765 • Letter: D
Question
Design your own linked list class to hold a series ofintegers. The class should have member functionsfor appending and insertingnodes. The insert member function should inserta new item at a specified position. A position of 0 means thatthe value will become the first item on the list, a position of 1means that the value will become the second item on the list, andso on. A position equal to or greater than the length of the listmeans that the value is placed at the end of the list.
Explanation / Answer
#include using namespace std;class integers{private:int num;class integers *next;public:integers(int n){num=n;next =NULL;}void insert(int n,int pos){class integers *ptr,*tptr;ptr = tptr=this;int count=1;while(ptr !=NULL){tptr= ptr;ptr = ptr->next;count++;if(count==pos)break;}tptr->next = new class integers(n);tptr->next->next = ptr;}void append(int n){class integers *ptr,*tptr;ptr = tptr=this;while(ptr !=NULL){tptr= ptr;ptr = ptr->next;}tptr->next = new class integers(n);}void print(){class integers *ptr,*tptr;ptr = tptr=this;while(ptr !=NULL){coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.