Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm not sure what this question is asking. Here is my code #include using namespace std; class node { public: int data; node *next; };

I'm not sure what this question is asking.

image text in transcribed

Here is my code

#include

using namespace std;

class node

{

public:

int data;

node *next;

};

class PointerList

{

public:

PointerList()

{

top = NULL;

}

bool empty()

{

if(top == NULL)

return true;

else

return false;

}

void insert(int position, int element)

{

node *newptr = new node;

newptr->data = element;

if(top == NULL) //insert the very first element

if(position==0)

{

newptr->next = NULL;

top = newptr;

}

else

cout

else

if(position==0) //insert on the first position when some elements existed

{

newptr->next = top;

top = newptr;

}

else //most cases belongs to this situation (as showed in the class slide)

{

node *preptr;

preptr = top;

for(int i=0;i

preptr=preptr->next;

newptr->next = preptr->next;

preptr->next = newptr;

}

}

void remove(int position)

{

if(position==0) //delete the first element

{

node * ptr = top;

top = ptr->next;

delete(ptr);

}

else

{

node *preptr;

preptr = top;

for(int i=0;i

preptr=preptr->next;

node * ptr = preptr->next;

preptr->next = ptr->next;

delete(ptr);

}

}

void print()

{

node *temp;

temp = top;

while(temp!=NULL)

{

coutdata

temp=temp->next;

}

}

private:

node *top;

int stackData;

};

int main() {

PointerList *sl = new PointerList();

sl->insert(0,10);

sl->insert(1,20);

sl->insert(2,30);

sl->insert(3,40);

sl->insert(0,50);

sl->insert(3,60);

sl->insert(5,70);

sl->remove(2);

sl->print();

return 0;

}

2. Re-write the insert and remove function by changing the input parameter from (a given index) to (the data value in front of the processed item). Therefore insert (preptr_value, element): means insert the element AFTER the data_value showed in the linked list. For example, for the linked list 10->20->30->40->50 After insert (30, 60) The linked list will become 10->20->30->60->40->50 remove (preptr value): means delete the element AFTER the data_value (NOTE!! Not delete the value itself!!) For example, for the linked list 10->20->30->40->50 After remove(30) The linked list will become 10->20-30->50 The code should also include the error checking on the given value, if the value is not in the list, the program will show an error message After the modification is done, perform the following main function and take a snapshot: int main) Pointer1st *31 = new PointerList(); sl->insert (0,10) //current linked list: 10 (default status, when empty) sl->insert (10,20) //current linked list: 10->20 sl->insert (20,30) //current linked list: 10->20->30 s1-insert (20,40): //current linked list: 10->20->40->30 sl->insert (30,50) /current linked list: 10->20->40->30->50 sl->print ) sl->insert (50, 60) //current linked list: 10->20->40->30->50->60 s1-insert (5,70) /error, no 5 existed in the linked list sl->print ); sl->remove (30) sl->print ) sl->remove (10) sl->print ); sl->remove (50) sl->print ) //current linked 1ist: 10->20->40-30->60 //current linked 1ist: 10-40-30->60 //error, no 50 existed in the linked list return O

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Formal SQL Tuning For Oracle Databases Practical Efficiency Efficient Practice

Authors: Leonid Nossov ,Hanno Ernst ,Victor Chupis

1st Edition

3662570564, 978-3662570562

More Books

Students also viewed these Databases questions

Question

=+2 Why did OBI create Centers of Excellence?

Answered: 1 week ago

Question

=+professionalism and competency in handling global HR issues?

Answered: 1 week ago