By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,087 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

Can anyone help me in this question?

P: 4
Given a linked list class defined as follows:

Expand|Select|Wrap|Line Numbers
  1. class NumberList 
  2. {     
  3. private:
  4.      struct Node       
  5. {          
  6.        double value;          
  7.        struct Node *next;       
  8. };       
  9.       ListNode *head;  //points to the first node        public:          
  10.        NumberList(); 
  11. }
Write a member function remove(int i) to remove the element at position i. If i is equal to or greater than the length of the list (or less than 0) then the list should remain unchanged.
Jan 7 '16 #1
Share this Question
Share on Google+
6 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
To remove an element, the next* of the element preceding the element to be deleted must be set to the node pointer of the element being deleted.

It will help when you traverse the list looking for the element to be deleted to remember what the previous node was because you will need that info should you remove an element.
Jan 7 '16 #2

P: 4
I didn't get you! Can you please explain me?
Jan 7 '16 #3

zmbd
Expert Mod 5K+
P: 5,397
S2510: Homework question?
Jan 7 '16 #4

P: 4
Not a homework question. Preparing for a programming exam. Got stuck in this question. Thanks
Jan 7 '16 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
OK. Take 3 nodes A B C.

So A.next = &B and B.next = &C and C.next = 0 (because it's the end).

To delete B you must set A.next = B.next.

When you locate B to remove it, you will need the address of A since you need to change the value in A.next.

However, there is no previous node in B. So you will need remember what the previous node to B was as you search for B.
Jan 7 '16 #6

P: 4
Thank you!!
Got an idea!!
Will try to write a program and verify!!
Jan 7 '16 #7

Post your reply

Sign in to post your reply or Sign up for a free account.