Microsoft Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
error: 'Node' does not name a type
compilation terminated due to -Wfatal-errors.
when i execute the code this particular error
comes can u solve this
thanx
public LinkedListNode reverseAlternate(LinkedListNode fHead)
{
if (fHead.Next == null)
{
return fHead;
}
else
{
LinkedListNode temp1 = fHead;
LinkedListNode temp2 = fHead.Next;
temp1.Next = reverseAlternate(temp2.Next);
temp2.Next = temp1;
return temp2;
}
}
while(p && p->next )
{
temp = p->data;
p->data = p->next->data;
p->next->data = temp;
p = p->next->next;
}
Reverse(Node n)
{
Node before = null;
Node after = null;
Node curr = null;
if(n->next == null || n == null)
return n;
curr = n;
Node n = n->next;
while(curr->next!= null)
{
after = curr->next->next;
curr->next->next = curr;
if(before!= null)
before->next = curr->next;
curr->next = after;
before=curr;
curr = curr->next;
}
return n;
}
#include <iostream>
#include <cassert>
template <typename T>
struct ListNode
{
ListNode(T val):data_(val),next_(NULL),prev_(NULL)
{
}
std::shared_ptr <ListNode> next_;
std::shared_ptr <ListNode> prev_;
T data_;
};
template <typename T>
class LinkedList
{
public:
void Print();
void SwapNeighbours();
void Insert(T val);
private:
std::shared_ptr< ListNode<T> > head_;
std::shared_ptr< ListNode<T> > tail_;
};
template <typename T>
void LinkedList<T>::Insert(T val)
{
if(head_ == NULL)
{
head_ = std::make_shared< ListNode<T> >(val);
tail_ = head_;
}
else
{
assert(tail_!=NULL);
tail_->next_ = std::make_shared< ListNode<T> >(val);
tail_ = tail_->next_;
}
}
template <typename T>
void swap(std::shared_ptr< ListNode<T> > l1, std::shared_ptr< ListNode<T> > l2)
{
if(!l1 || !l2)
{
return;
}
auto val = l1->data_;
l1->data_ = l2->data_;
l2->data_ = val;
}
template <typename T>
void LinkedList<T>::SwapNeighbours()
{
auto temp = head_;
if(!temp)
{
return;
}
while(temp && temp->next_)
{
swap(temp,temp->next_);
temp = temp->next_->next_;
}
}
template <typename T>
void LinkedList<T>::Print()
{
if(!head_)
{
return;
}
auto temp = head_;
while(temp)
{
std::cout<<"["<<temp->data_<<"]->";
temp = temp->next_;
}
std::cout<<"[NULL]"<<std::endl;
}
#include "LList.hpp"
int main ()
{
LinkedList<int> llist;
for(int i=0; i<10; i++)
{
llist.Insert(i);
}
llist.SwapNeighbours();
llist.Print();
}
public static Node swapAlternateNodes(Node head)
{
if(head == null || head.next == null)
return head;
Node temp = head;
head = head.next;
Node current = temp;
Node prev = null;
while(current != null && current.next != null)
{
Node nextCurrent = current.next.next;
temp = current.next;
current.next = nextCurrent;
temp.next = current;
if(prev != null)
prev.next = temp;
prev = current;
current = nextCurrent;
}
return head;
}
#include <stdio.h>
struct link
{
int data;
struct link *next;
};
//struct link *global_ptr = NULL;
void insert_node(struct link **ptr,int value)
{
//global_ptr = *ptr;
struct link *temp = (struct link*)malloc(sizeof(struct link));
struct link *temp_ptr = NULL;
temp->data = value;
temp->next = NULL;
if(*ptr == NULL)
{
*ptr = temp;
}
else
{
temp_ptr = (*ptr);
while(temp_ptr->next != NULL)
{
temp_ptr = temp_ptr->next;
}
temp_ptr->next = temp;
}
}
void display_list(struct link *disp_list)
{
while(disp_list != NULL)
{
printf("The value in the list %d\n",disp_list->data);
disp_list = disp_list->next;
}
}
void display_swap_nodes(struct link **root_node)
{
struct link *test_ptr=NULL;
test_ptr = *root_node;
struct link *temp_node1=NULL;
struct link *temp_node2=NULL;
while(test_ptr->next != NULL)
{
temp_node1 = test_ptr->next;
if(temp_node1->next != NULL)
{
temp_node2 = temp_node1->next;
}
else
{
temp_node2 = temp_node1->next;
temp_node2->next = NULL;
break;
}
test_ptr = temp_node1->next;
if(temp_node2->next != NULL)
{
test_ptr->next = temp_node2->next;
}
else
{
test_ptr->next = temp_node2;
}
test_ptr = temp_node2;
}
}
int main()
{
struct link *link_ptr = NULL;
int count;
int value;
for(count =0 ; count<8; count++)
{
scanf("%d",&value);
insert_node(&link_ptr,value);
}
display_swap_nodes(&link_ptr);
display_list(link_ptr);
return 0;
}
Quickest and easiest way to swap the alternate nodes will be to swap the data of alternate nodes, you can also swap nodes by using pointers, The only overhead swapping nodes using pointer will be to actually have one additional pointer. Below is the function which will swap data for alternate nodes.
static void swap_nodes( struct node **head )
{
struct node *even, *odd;
odd = *head;
if( odd->next != NULL )
even = odd->next;
while( odd->next != NULL )
{
odd->data = odd->data ^ even->data;
even->data = odd->data ^ even->data;
odd->data = odd->data ^ even->data;
odd = even->next;
if( odd == NULL )
break;
even = odd->next;
}
}
deleting from specific location 0(1) I guess but every thing should not take more than 0(n)
void swap (struct list **list1)
{
struct list *cur, *tmp, *next;
cur = *list1;
if (cur && cur->next)
*list1 = cur->next;
//To make sure that we have at least two more elements to be swapped.
while (cur && cur->next)
{
next = cur->next;
tmp = next->next;
next->next = cur;
//We have to make 1->next as 4 in above example (figure).
if(tmp)
if (tmp->next)
cur->next = tmp->next;
else
cur->next = tmp; // take care of an add number of nodes
else
cur->next = NULL;
}
return;
}
Source : StackOverFlow
Ansewr in C# - This answer does not use recursion which makes it better for large lists.
The first prev instance is created so we will not have to check for null every iteration.
private static Node Reverse2(Node head) {
Node prev = new Node();
Node curr = head;
Node reverseHead = head.Next;
while (curr != null && curr.Next != null) {
Node nn = curr.Next.Next;
Node newHead = curr.Next;
newHead.Next = curr;
curr.Next = nn;
prev.Next = newHead;
prev = curr;
curr = nn;
}
return reverseHead;
}
- learner January 06, 2013