Hundred battles c++ (7)

40. Linked list question: The node structure of a linked list

struct Node

{

int data ;

Node *next ;

};

typedef struct Node Node ;

(1) Knowing the head node head of the linked list, write a function to reverse the linked list (Intel)

Node * ReverseList(Node *head) // linked list reverse order

{

if ( head == NULL || head->next == NULL )

return head;

Node *p1 = head ;

Node *p2 = p1->next ;

Node *p3 = p2->next ;

p1->next = NULL ;

while ( p3 != NULL )

{

p2->next = p1 ;

p1 = p2 ;

p2 = p3 ;

p3 = p3->next ;

}

p2->next = p1 ;

head = p2 ;

return head ;

}

(2) It is known that the two linked lists head1 and head2 are in order, please merge them into a linked list and still in order. (keep all nodes, even if they are the same size)

Node * Merge(Node *head1 , Node *head2)

{

if ( head1 == NULL)

return head2 ;

if ( head2 == NULL)

return head1 ;

Node *head = NULL ;

Node *p1 = NULL;

Node *p2 = NULL;

if ( head1->data < head2->data )

{

head = head1 ;

p1 = head1->next;

p2 = head2 ;

}

else

{

head = head2 ;

p2 = head2->next ;

p1 = head1 ;

}

Node *pcurrent = head ;

while ( p1 != NULL && p2 != NULL)

{

if ( p1->data <= p2->data )

{

pcurrent->next = p1 ;

pcurrent = p1 ;

p1 = p1->next ;

}

else

{

pcurrent->next = p2 ;

pcurrent = p2 ;

p2 = p2->next ;

}

}

if ( p1 != NULL )

pcurrent->next = p1 ;

if ( p2 != NULL )

pcurrent->next = p2 ;

return head ;

}

(3) It is known that the two linked lists head1 and head2 are in order, please merge them into a linked list which is still in order, this time it is required to use the recursive method. (Autodesk)

Answer:

Node * MergeRecursive(Node *head1 , Node *head2)

{

if ( head1 == NULL )

return head2 ;

if ( head2 == NULL)

return head1 ;

Node *head = NULL ;

if ( head1->data < head2->data )

{

head = head1 ;

head->next = MergeRecursive(head1->next,head2);

}

else

{

head = head2 ;

head->next = MergeRecursive(head1,head2->next);

}

return head ;

}

41. Analyze the output of this program (Autodesk)

class B

{

public:

B()

{

cout<<"default constructor"<<endl;

}

~B()

{

cout<<"destructed"<<endl;

}

B(int i):data(i) //B(int) works as a converter ( int -> instance of B)

{

cout<<"constructed by parameter " << data <<endl;

}

private:

int data;

};

B Play( B b)

{

return b ;

}

(1) results:

int main(int argc, char* argv[]) constructed by parameter 5

{ destructed B(5) parameter destructor

B t1 = Play(5); B t2 = Play(t1); destructed t1 formal parameter destruction

return 0; destructed t2 Pay attention to the order!

} destructed t1

(2) results:

int main(int argc, char* argv[]) constructed by parameter 5

{ destructed B(5) parameter destructor

B t1 = Play(5); B t2 = Play(10);   constructed by parameter 10

return 0; destructed B(10) parameter destruct

} destructed t2 Pay attention to the order!

destructed t1

42. Write a function to find the second largest number in an integer array (microsoft)

Answer:

const int MINNUMBER = -32767 ;

int find_sec_max( int data[] , int count)

{

int maxnumber = data[0] ;

int sec_max = MINNUMBER ;

for ( int i = 1 ; i < count ; i++)

{

if ( data[i] > maxnumber )

{

sec_max = maxnumber ;

maxnumber = data[i] ;

}

else

{

if ( data[i] > sec_max )

sec_max = data[i] ;

}

}

return sec_max ;

}

Guess you like

Origin blog.csdn.net/hebtu666/article/details/127205279