C++ 记录字符串和链表的简单操作

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

/*将字符串中的大写字母转换成小写字母*/
void RevertBig2Small(string &str){
    if(str.empty())
        return;
    for(int i = 0 ;i<str.length();i++){
        if((str[i]>'A')&& (str[i] < 'Z')){
            str[i] +=32;/*小写字母(a-z)的ASCII码比大些字母(A-Z)的ASCII码大32*/
        }
    }
}

/*字符串反转输出,不增加内存的基础上实现,原地反转
 输入:["h","e","l","l","o"]
 输出:["o","l","l","e","h"]
 解题思路:将字符串二分,然后从两头往中间进行交换
 */
void RevertChar(string &str){
    if(str.empty())
        return;
    char tmp;
    for(int i = 0; i < str.length()/2;i++){
        tmp=str[i];
        str[i]=str[str.length()-1-i];
        str[str.length()-1-i]=tmp;
    }
}

/*给定一个字符串,逐个翻转字符串中的每个单词。
 示例:
 输入: “the sky is blue”,
 输出: “blue is sky the”.
 使用 O(1) 空间复杂度的原地解法
 思路:首先将原字符串逆转,然后挨个逆转连续的子串,特别要注意,多个空格在一起的情况
 */
void WordRevert(string &str){
    if(str.empty())
        return;
    RevertChar(str);
    //str.clear();
    string tempStr = "";
    for(int i=0;i<str.length() ;i++){
        if(str[i]==' ')
            continue;
        string word;
        /*确定单词*/
        while((str[i] != ' ') && (i<str.length())){
            word+=str[i++];
        }

        word +=' ';/*单词间增加空格*/
        RevertChar(word);/*反转单个单词*/
        tempStr+=word;
    }
    str=tempStr.substr(0);/*重新从临时str中获取整个子串内容*/
}

/*实现 strStr() 函数:strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL
 思路就是:就是将子串从区间的第一个字符开始比较,如果有就返回,否则返回NULL*/
char*  StrStr(char *str,char* subStr){
    for(int i = 0; i <= strlen(str);i++,str++){
        char *p=str;
        for(char *q=subStr;;p++,q++){
            if(*q == '\0')
                return str;
            if(*p != *q)
                break;
            
        }
    }
    return NULL;
}

/*逆序构造单链表
 例如:输入数据:1 2 3 4 5 6,构造单链表:6->5->4->3->2->1。
 输入 -1 链表结束
 思路:pre指针,cur指针,cur赋当前值,
 **/

class ConstructPostList{
public:
    struct ListNode{
        string data;
        struct ListNode *next;
    };
    
    ConstructPostList(){
        head.next = NULL;
    }

    void CreatePostList(string &str){
        struct ListNode *pre = NULL;/*第一次的话表示做后一个节点,第一次设置为空*/
        for(int i =0; i<str.length();i++)
        {
            struct ListNode *node=new(ListNode);
            node->data = str[i];
            node->next = pre;
            pre = node;
        }
        head.next=pre;
    }

    
    void CreateNormalList(string &str){
        ListNode *next=&head;
        for(int i = 0; i < str.length();i++){
            ListNode *cur = new(ListNode);
            cur->data=str[i];
            next->next=cur;
            next = cur;
        }
    }
    
    void ShowList(ListNode *h){
        struct ListNode *node = h->next;
        while(node){
            cout<<node->data<<" ";
            node=node->next;
        }
        cout<<""<<endl;
    }
    
    void DestroyList(){
        struct ListNode *node = head.next;
        int i = 0;
        while(node){
            ListNode *cur=node;
            node=cur->next;
            delete cur;
            i++;
        }
        cout<<"Destroy list,node num="<<i<<endl;
    }
    
    /*链表反转*/
    ListNode* ListRevert(ListNode *head){
        if(NULL == head|| head->next == NULL){
            return head;
        }
        
        ListNode *pre = NULL;
        ListNode *next = NULL;
        while(head){
            next = head->next;
            head->next = pre;
            head = next;
        }
        
        return pre;
        
    }
    
    ListNode* reverseByRecursion(ListNode *head)
    {
        //第一个条件是判断异常,第二个条件是结束判断
        if(head == NULL || head->next == NULL)
            return head;

        ListNode *newHead = reverseByRecursion(head->next);

        head->next->next = head;
        head->next = NULL;

        return newHead;    //返回新链表的头指针
    }
    
    ListNode* GetList(){
        return &head;
    }

private:
    ListNode head;
};


struct Node{
    int data;
    struct Node *next;
    Node(){
        data=0;
        next=NULL;
    }
};

class List{
public:

    List(){
        head = new(Node);
        head->data = 0;
        head->next= NULL;
    }
    
    Node* CreateList(int num){
        Node *cur=head;
        for(int i = 0; i<num;i++){
            Node* node=new(Node);
            node->data=i;
            node->next = NULL;
            
            cur->next = node;
            cur = node;
        }
        return head;
    }
    
    Node* AddTail(int value){
        if(!head){
            return head;
        }
        
        Node *cur=head;
        Node *next = cur->next;
        while(next){
            cur = next;
            next = next->next;
        }
        Node *node = new(Node);
        node->data = value;
        node->next=NULL;
        cur->next = node;
        
        return head;
    }
    
    Node *RevertList(Node* head){
        if(!head->next){
            return head;
        }
        Node *pre,*cur,*next;
        pre=head;
        head->next=NULL;
        cur=head->next;
        while(cur){
            next=cur->next;
            cur->next=pre;
            
            pre=cur;
            cur=next;
        }
        
        head=pre;
        
        return head;
    }
    
    bool IsLoopList(Node *head){
        Node *fast=head->next->next;
        Node *slow=head->next;
        while(fast && slow){
            fast=fast->next->next;
            slow=slow->next;
            if(fast == slow){
                return true;
            }
        }
        return false;
    }
    
    
    void Show(Node *h){
        Node *cur = h->next;
        while(cur){
            cout<<cur->data<<" ";
            cur= cur->next;
        }
        cout<<endl;
        
    }
    
private:
    Node *head;
};



int main()
{
    List l;
    Node* head=l.CreateList(10);
    l.Show(head);
    l.Show(l.RevertList(head));
    
    
    
    return 0;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值