Write a function implementation that overloads the == operator of a dynamic sequ
ID: 3807728 • Letter: W
Question
Write a function implementation that overloads the == operator of a dynamic sequence container (linked list) class to lest if two object have the same data sets. What type of structure (static array, dynamic array, linked list, doubly linked list, stack or queue) would you use to manipulate a simple grocery list? Explain why the other structures would not work as well. Explain at least two errors that can occur when using a stack. What can be done (as pan of a stack class) to ensure the errors do not cause data errors? What can be done (as part of the program using the stack) to ensure the errors do not occur? Show the state of the private member variables of s before and after each line of the code executes. You can number the lines for reference. (the Slack class below is implemented using a linked list) Stack s; s.push(1); s.push(2); s.push(3); coutExplanation / Answer
1)
If say we have a class LinkedList, and it has member named "data"
then we can overload the == operator as below.
bool LinkedList::operator==(LinkedList a)
{
return this->data == a.data;
}
2)
For implementing Grocery List, singly Linked List would be sufficient.
Why not others ?
- static array and dynamic arrays.
We would require to mix items with different datatype like price, item name for single
transaction, which is not possible with arrays.
Also, static array provided fixed size array and cannot be increased.
- Doubly Linked list.
This can be considered but for basic implementation of Grocery list,
having two pointers per node will cause overhead.
- Stack - (First In Last Out)
Say, if we have 10 items in Grocery list and if we need the item starting from first
then each item we have to pop all the items, take out the first item
and then push all the items again. This is an overhead.
- Queue - (First In First Out)
Similar to Stack, say if we have 10 items in Queue with 1st item being pushed
first, then to get the 10th item, we have to dequeue all the 9 items, get the
10th item and again Enqueue 9 items.
This is an overhead.
3)
The two most common errors which can occur in Stack class is as below
- Overflow error
If the Stack is full and still we are pushing the contents to stack
then this error happens.
- Underflow error
If the Stack is Empty and we try to pop out the items, then this error happens.
From Stack class, we can always have a check for Stack Full and Stack Empty before
we push or pop the contents from the Stack.
From the program, using the Stack, we can have a check on the size of stack by using the
interfaces provided by Stack class like isFull() or isEmpty(). We have to ensure that
push and pop operations happen only after the check.
Also, we should take care of the datatype which the Stack can hold.
4)
Given program snippet is:
a) Stack <int> s;
b) s.push(1);
c) s.push(2);
d) s.push(3);
e) cout << s.pop();
*) s.push(1) will have s = 1
*) s.push(2) will have s = 2, 1
*) s.push(3) will have s = 3, 2, 1
*) cout << s.pop() will output the number 3 to the console
and the stack will be updated to s = 2, 1.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.