Q1 Solution: . we can use doubly linked list with hash to implement all the
operation in O(1).

By keeping track of head and tail pointer we can do enqueue and dequeue in
O(1) time.

In hash we will keep track of each element present in linked list(queue).
With node value as a hash key and address of node as a value. So for
searching we can do it in O(1).

For deleting an element we will directly take address of that value  from
hash and delete it. O(1) time.

Q2.  /*my code will return NULL if not palindrome and head if it is
palindrome */

count = totalnode/2;
//odd = 0 if totalnode is even else 1

struct node *checkPalindrome(struct node *head, int count)
{
  struct node *temp;

  if(count == 0) {
    if(odd && head->next) return head->next;
    else return head;
  }

  if(count > 0)
    temp = checkPalindrome(head->next, count - 1);


  if(temp && (temp->data == head->data ) && !temp->next)
      return head;

  else if (temp && (temp->data == head->data ))
      return temp->next;

  else return NULL;

}

On Sun, Aug 26, 2012 at 12:41 AM, Ashish Goel <ashg...@gmail.com> wrote:

> Q1. Design a data structure for the following operations:
>
> I.                Enqueue
>
> II.               Dequeue
>
> III.              Delete a given number(if it is present in the queue,
> else do nothing)
>
> IV.               isNumberPresent
>
> All these operations should take O(1) time.
>
>
> Q2. Check if a linked list (each char is a node) is palindrome,
> recursively.
> Best Regards
> Ashish Goel
> "Think positive and find fuel in failure"
> +919985813081
> +919966006652
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to