Here is an excerpt from the definition of a singly-linked list class. A LinkedLi
ID: 3826219 • Letter: H
Question
Here is an excerpt from the definition of a singly-linked list class. A LinkedList object represents a singly-linked list of integers. The implementation uses no dummy node, and the list end is indicated by the last node having NULL as its m_next data member. The empty list is represented by m_head and m_tail being NULL. class LinkedList { public: LinkedList(); creates an empty list void push back (int v) void unique (); bool dominates (LinkedList& other) const { return dom (m_head, other. m_head);} private: struct Node { Node (int v, Node *n) : m value (v), m_next (n) {} int m_value; Node *m_ next; }; Node *m_head; //points to first Node in the list Node* m_tail; // points to last Node in the list bool dom (const Node* p1, const Node* p2) const; }; For this problem, we will you to write some function implementations. Be sure our code is syntactically correct as well as functionally correct. Notice that the Node type has no default constructor. a. The push_back function appends to the end of the list a node whose value is v. Write an implementation of this function in the space below.Explanation / Answer
public void insertAtPos(int val , int pos)
{
Node nptr = new Node(val, null);
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink() ;
ptr.setLink(nptr);
nptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size++ ;
}
Related 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.