码迷,mamicode.com
首页 > 其他好文 > 详细

20141025

时间:2014-10-26 18:17:14      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   sp   div   

1、单链表反转(递归非递归)

ListNode *ReverseList(ListNode *pHead)
{
    if(pHead==NULL||pHead->Next==NULL)
        return pHead;
    ListNode *previousNode=NULL;
    ListNode *nextNode=NULL;
    ListNode *currentNode=pHead;
    while(currentNode!=NULL)
    {
        nextNode=currentNode->Next;
        currentNode->Next=previousNode;
        previousNode=currentNode;
        currentNode=nextNode;
    }
    return previousNode;  //返回的不是currentNode
}

ListNode *ReverseList_recursion(ListNode *currentNode)
{
    if(currentNode==NULL||currentNode->Next==NULL)
        return currentNode;
    ListNode *nextNode=currentNode->Next;
    ListNode *PreviousNode=NULL;
    currentNode->Next=PreviousNode;
    ListNode *newHead=ReverseList_recursion(nextNode);
    nextNode->Next=currentNode;
    return newHead;
}

 

2、单链表是否有环

#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct node
{
    int data;
    struct node * Next;
}ListNode;
ListNode *CreateList()
{
    int x=0;
    int flag=1;
    ListNode *pHead=(ListNode *)malloc(sizeof(ListNode));
    pHead->data=-1;
    pHead->Next=NULL;
    ListNode *rear=pHead;
    while(flag)
    {
        cout<<"input data:";  
        cin>>x;
        if(x!=0){
            ListNode *p=new ListNode[sizeof(ListNode)];
            p->data=x;    
            rear->Next=p;
            rear=p;
        }
        else{
            flag=0;
        }
    
    }
    //rear->Next=pHead;有环
    rear->Next=NULL;//无环链表
    return pHead;
}

bool CircleInList(ListNode *head)
{
    if(head==NULL)
        return false;
    ListNode *pfast,*plow;
    pfast=head;
    plow=head;
    while(pfast!=NULL&&pfast->Next!=NULL)//为了保证pnext=pnext->Next->Next有效,那么pnext->Next!=NULL是条件
    {
        plow=plow->Next;
        pfast=pfast->Next->Next;
        if(pfast==plow)
            return true;
    }
    return false;
}


int main()
{
    ListNode *head=CreateList();
    if(CircleInList(head))
    {
        printf("有环\n");
    }
    else
    {
        printf("无环\n");
    }

    ListNode *backKth=ReverseKthNode(head,2);
    printf("%d",backKth->data);
}

3、传入函数的参数是按值传递时,一定会调用类的拷贝构造函数。

4、深拷贝和浅拷贝,浅拷贝可能析构一块内存两次

#include<iostream>
#include<vector>
#include<string>
using namespace std;

class Base
{
public:
    Base(): str(NULL)
    {}
    ~Base(){
        if(str){
            static int i=0; 
            cout<<"&Base"<<i++<<"="<<(int *)this<<",str="<<(int *)str<<endl;//输出该对象的地址和str指针的值
            delete [] str;
        }
    }
    char *str;
};


int main()
{
    Base B;
    B.str=new char[32];
    strcpy(B.str,"trend micro");
    vector<Base> *vec=new vector<Base>() ;
    vec->push_back(B);  //B是按值传递的,调用默认了默认拷贝构造函数,从而发生了浅拷贝
    delete vec;
    return EXIT_SUCCESS;
}

bubuko.com,布布扣

20141025

标签:style   blog   http   color   io   os   ar   sp   div   

原文地址:http://www.cnblogs.com/yexuannan/p/4052472.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!