| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 386 人关注过本帖
标题:继续面试题,明天去面试了,附答案
只看楼主 加入收藏
suntea
Rank: 2
等 级:论坛游民
帖 子:59
专家分:88
注 册:2010-6-24
结帖率:100%
收藏
 问题点数:0 回复次数:1 
继续面试题,明天去面试了,附答案
class String
{
public:
    String(const char *str = NULL); // 普通构造函数
    String(const String &other); // 拷贝构造函数
    ~ String(void); // 析构函数
    String & operate =(const String &other); // 赋值函数
private:
    char *m_data; // 用于保存字符串
};
实现类中的四个函数:
String::String(const char* str)
{
    if(str == NULL)
    {
        m_data = new char[1];
        m_data[0] = ' ';
    }
    else
    {
        m_data = new char[sizeof(str)+1];
        strcpy(m_data,str);
    }
}
String::String(const String &other)
{
    m_data = new char[sizeof(other.m_data)+1];
    strcpy(m_data,other.m_data);
}
String::String & operate =(const String &other)
{
    if(this == &other)
    {
        return *this;
    }
    delete[] m_data;
    m_data = new char[sizeof(other.m_data)+1];
    strcpy(m_data,other.m_data);
    return *this;
}
String::~String(void)
{
    delete[] m_data;
}

一个链表的结点结构
struct Node
{
int data ;
Node *next ;
};
typedef struct Node Node ;
已知两个链表head1 和head2 各自有序,请把它们合并成一个链表依然有序。
Node * Togeter(Node *head1 , Node *head2)
{
    if(head1 == NULL) return head2;//如果head1==NULL return head2
    if(head2 == NULL) return head1;
    Node* head = NULL;//定义3个辅助Node*
    Node* p1 = NULL;
    Node* p2 = NULL;
    if(head1->data < head2->data)
    {
        head = head1;
        p1 = head1->next;
        p2 = head2;
    }
    else
    {
        head = head2;
        p1 = head2->next;
        p2 = head1;
    }
    Node* pCur = head;
    while(p1 != NULL&& p2 != NULL)
    {
        if(p1->data <= p2->data)
        {
            pCur->next = p1;
            pCur = p1;
            p1 = p1->next;
        }
        else
        {
            pCur->next = p2;
            pCur = p2;
            p2 = p2->next;
        }
    }
    if(p1 != NULL) pCur->next = p1;
    if(p2 != NULL) pCur->next = p2;
    return head;
}
搜索更多相关主题的帖子: 面试 
2010-07-13 23:58
南国利剑
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:29
帖 子:1165
专家分:3536
注 册:2010-4-12
收藏
得分:0 
来学习。。。

南国利剑
2010-07-14 00:01
快速回复:继续面试题,明天去面试了,附答案
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039241 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved