Remove whitespace from a string in-place, in linear time

Should not there be a \o in the end to terminate t… Neha - Oct 2, 2013Should not there be a \o in the end to terminate the string?? Yes this is a ‘\0’ terminated string only. We are moving ahead only when we find a white space (when we say): while (str[ahead] == ' ‘) ahead++; Rest all the characters except the space are copied as is, which also includes ‘\0’ and hence string is ‘\0’ . [Read More]

Remove whitespace from a string in-place, in linear time

void removeWhitespace(char\* str) {  
    int current \= 0;  
    int ahead \= 0;  
   
    while (str\[ahead\] !\= '\\0') {  
        while (str\[ahead\] \=\= ' ') ahead++;  
   
        str\[current\] \= str\[ahead\];  
        current++;  
        ahead++;  
    }  
}  

Deleting a node from a singly linked link list

Question: You are given a pointer to a node (not the tail node) in a singly linked list. Delete that node from the linked list. Write code in C. Answer: To delete a node, you have to redirect the next pointer of the previous node to point to the next node instead of the current one. Since we don’t have a pointer to the previous node, we can’t redirect its next pointer. [Read More]

Deleting a node in singly linked list if it is less than any of the successor nodes

Hi, try with 1,7,0,10. output must be 10. but your…

Unknown - Jun 6, 2012

Hi,
try with 1,7,0,10.
output must be 10.
but your algorithm outputs wrong answer

it seems that algo is correct..
1,7,0,10
reverse

10 0 7 1

max_till_here= 10

0 < 10 delete 0

left : 10 7 1

7 < 10 : delete 7

1 < 10 : delete 1

output : 10

Deleting a node in singly linked list if it is less than any of the successor nodes

Question : Given a link list of integers delete all nodes which have value smaller than the value of any following node. **Example :**7 4 5 2 3 6 **Output : ** 7 6 Solution: The solution is to : 1. Reverse the list 6 3 2 5 4 7 2. Delete all the elements which are below the max-till-now element. eg. max-till-now=6….Delete all the elements below 6…till you find another max element = max-till-now = 7 [Read More]

Duplicate removal in Binary Search Tree

#include <stdio.h\> class BinarySearchTree { private: int data; BinarySearchTree \*left, \*right; public: BinarySearchTree(const int d):data(d), left(NULL), right(NULL){} ~BinarySearchTree() { if(left !\= NULL) { delete left; } if(right !\= NULL) { delete right; } } //remove duplicates void removeDup() { if(left) { left\-\>removeDup(); left\-\>remove(data, this); } if(right) right\-\>removeDup(); } void remove(int value, BinarySearchTree \*parent) { if(value < data && left) { left\-\>remove(value, this); } else if(value \> data && right) { right\-\>remove(value, this); } else remove(parent); } void remove(BinarySearchTree \*parent) //remove this node { if(left \=\= NULL && right \=\= NULL) //leaf { ((parent\-\>left \=\= this) ? [Read More]

Deleting a node from a Binary Search Tree

Suppose we want to delete a node k. First we need to search for the node k. Offcourse k should not be null. Then we have three cases: 1. The node to be deleted has no children - The memory occupied by this node must be freed and either the left link or the right link of the parent of this node must be set to NULL. 2. The node to be deleted has exactly one child - [Read More]