Programmers must know: C/C ++ interview questions (10)

Source: Internet
Author: User

From: http://wxdlut.blog.163.com/blog/static/12877015820099237557679/

 

The reversal of a one-way linked list is a frequently asked interview question, which is also a very basic question. For example, a linked list is like this: 1-> 2-> 3-> 4-> 5, and then 5-> 4-> 3-> 2-> 1.

The easiest way to traverse the linked list is to use an auxiliary pointer to store the next element pointed to by the current pointer during the traversal process. After the pointer of the element on the current node is reversed, use the stored pointer to traverse the backend. The source code is as follows:

Struct linka {
Int daTa;
Linka * next;
};
Void reverse (linka * & head ){
If (Head = NULL)
Return;
Linka * pre, * cur, * ne;
Pre = head;
Cur = head-> next;
While (cur)
{
Ne = cur-> next;
Cur-> next = pre;
Pre = cur;
Cur = ne;
}
Head-> next = NULL;
Head = pre;
}

There is also a method that uses recursion. The basic idea of this method is to call recursive functions to reverse the subsequent nodes before reversing the current node. The source code is as follows. However, this method has one disadvantage: The last node after the inversion will form a ring, so the next field of the node returned by the function must be set to NULL. I used a reference to change the head pointer. The source code of the algorithm is as follows:

Linka * reverse (linka * p, linka * & head)
{
If (P = NULL | p-> next = NULL)
{
Head = P;
Return P;
}
Else
{
Linka * TMP = reverse (p-> next, head );
TMP-> next = P;
Return P;
}
}

② Known String classes are defined as follows:

Class string
{
Public:
String (const char * STR = NULL); // common Constructor
String (const string & Another); // copy the constructor
~ String (); // destructor
String & operater = (const string & RHs); // value assignment function
PRIVATE:
Char * m_daTa; // used to save strings
};

Try to write out the member function implementation of the class.

Answer:

String: String (const char * str)
{
If (str = NULL) // if the strlen parameter is NULL, an exception is thrown.
{
M_daTa = new char [1];
M_daTa [0] = '\ 0 ';
}
Else
{
M_daTa = new char [strlen (str) + 1];
Strcpy (m_daTa, str );
}

}

String: String (const String & another)
{
M_daTa = new char [strlen (another. m_daTa) + 1];
Strcpy (m_daTa, other. m_daTa );
}

String & String: operator = (const String & rhs)
{
If (this = & rhs)
Return * this;
Delete [] m_daTa; // Delete the original data and open a new memory
M_daTa = new char [strlen (rhs. m_daTa) + 1];
Strcpy (m_daTa, rhs. m_daTa );
Return * this;
}

String ::~ String ()
{
Delete [] m_daTa;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.