字元單鏈表識別數字,字母,其它字元,並分為三個迴圈鏈表的演算法c++實現

來源:互聯網
上載者:User

標籤:std   函數   first   als   space   stdio.h   資料   har   迴圈鏈表   

已知一個單鏈表中的資料元素含有三類字元(即字母字元,數字字元和其它字元),試編寫演算法,構造三個迴圈鏈表,使每個迴圈鏈表中只含有同一類的字元,且利用原表中的結點空間作為這三個表的結點空間。

實現原始碼:

#include<iostream>

#include<stdio.h>

#include<string.h>

using namespace std;

struct node

{

    char ch;

    node*link;

};

//為了方便輸出,定義一個輸出字元的函數,適用於單鏈表

void printlist(node*h)

{

    node *first=h;

    while(first!=NULL)

    {

        cout<<first->ch;

        first=first->link;

    }

}

void printcirclelist(node*p)

{

    node *first=p;

    while(true)

    {

        cout<<first->ch;

        first=first->link;

        if(first==p)break;

    }

}

bool is_num(char c)

{

    if(c>=‘0‘&&c<=‘9‘)return true;

    else return false;

}

bool is_eng(char c)

{

    if((c>=‘a‘&&c<=‘z‘)||(c>=‘A‘&&c<=‘Z‘))return true;

    else return false;

}

bool is_else(char c)

{

    if(!is_num(c)&&!is_eng(c))return true;

    else return false;

}

int main()

{

    //下面構造一個字元單鏈表

    node *list_head=new node();

    node *p=list_head;

    char ch[100]="235543kj45i##%GGG%%&&hd7&&&";

    for(int i=0;i<27;i++)

    {

        if(i!=0)

        {

            p->link=new node();

            p=p->link;

        }

        p->ch=ch[i];

    }

    p=NULL;

    printlist(list_head);

    //數字迴圈鏈表

    node*head1=new node();

    //字母迴圈鏈表

    node*head2=new node();

    //其它字元迴圈鏈表

    node*head3=new node();

    //下面從"235543kj45i##%GGG%%&&hd7&&&"中提取數字,字母和其他字元

    node *pointer=list_head;

    node*p1=head1;

    node*p2=head2;

    node*p3=head3;

    while(pointer!=NULL)

    {

        if(is_num(pointer->ch)){p1->link=pointer;p1=p1->link;}

        if(is_eng(pointer->ch)){p2->link=pointer;p2=p2->link;}

        if(is_else(pointer->ch)){p3->link=pointer;p3=p3->link;}

        pointer=pointer->link;

    }

    //下面將進行首尾相接

    p1->link=head1;

    p2->link=head2;

    p3->link=head3;

    //至此迴圈鏈表建立完成

    cout<<endl;

    printcirclelist(head1);

    cout<<endl;

    printcirclelist(head2);

    cout<<endl;

    printcirclelist(head3);

    return 0;

}

運行結果:

(可見該演算法可以將三種不同的字元識別出來並構造迴圈鏈表)

字元單鏈表識別數字,字母,其它字元,並分為三個迴圈鏈表的演算法c++實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.