Please answer all of the following by writing it on paper as specified. (a) An a
ID: 671570 • Letter: P
Question
Please answer all of the following by writing it on paper as specified.
(a) An addition to the Bag class: Write the implementation of a memeber function twice() that returns true if the invoking Bag contains at least one item at least twice, false otherwise. Use these declarations
....
private:
Item data[CAPACITY];
size_t used;
}
(b). A doubly-linked list has pointers to both it's first and it's last Nodes. Write C++ statements that insert a new Node, containing a value entry, at the far end of such a list.
Explanation / Answer
1.
class Bag:
def __init__(self):
self.data = []
def add(self, x):
self.data.append(x)
def addtwice(self, x):
self.add(x)
self.add(x)
2.
void DeleteLast(node *head)
{
node *p,*q;
for(p=head;p->next->next!=head;p=p->next); /*p Points Node3*/
q=p->next; /*q Points Node4 (i.e Last node)*/
p->next=q->next; /*Update the p's next field to head i.e q's current next field*/
head->previous=q->previous; /* Update the previous field of head to p*/
free(q); /*Free q (Last Node-node4) */
}
public Node Insert(Node head, int value)
{
if (head == null || value < head.value)
return new Node(value, head);
else
{
head.next = Insert(head.next, value);
return head;
}
}
public Node Remove(Node head, int value)
{
if (head == null)
return null;
else
if (head.value == value)
return head.next;
else
{
head.next = Remove(head.next, value);
return head;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.