I need a sample on how i can use a function using the linked list(insertion): pl
ID: 3640313 • Letter: I
Question
I need a sample on how i can use a function using the linked list(insertion): please make a sample function using the following:List *nodePtr, *previousNodePtr;
cout<<"please enter the number to be inserted"<<endl;
cin>>number;
if (head == NULL || head->value ==10)
{
// A new node goes at the beginning of the list.
head = new List(number, head);
}
else
{
previousNodePtr = head;
nodePtr = head->next;
// Find the insertion point.
while (nodePtr != NULL && nodePtr->value==35)
{
previousNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
// Insert the new node just before nodePtr.
previousNodePtr->next = new List(number, nodePtr);
}
Explanation / Answer
#include using namespace std; class linklist { private: struct node { int data; node *link; }*p; public: linklist(); void append( int num ); void add_as_first( int num ); void addafter( int c, int num ); void del( int num ); void display(); int count(); ~linklist(); }; linklist::linklist() { p=NULL; } void linklist::append(int num) { node *q,*t; if( p == NULL ) { p = new node; p->data = num; p->link = NULL; } else { q = p; while( q->link != NULL ) q = q->link; t = new node; t->data = num; t->link = NULL; q->link = t; } } void linklist::add_as_first(int num) { node *q; q = new node; q->data = num; q->link = p; p = q; } void linklist::addafter( int c, int num) { node *q,*t; int i; for(i=0,q=p;ilink; if( q == NULL ) { coutlink; delete q; return; } r = q; while( q!=NULL ) { if( q->data == num ) { r->link = q->link; delete q; return; } r = q; q = q->link; } 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.