Character single-linked list identifies numbers, letters, other characters, and is divided into three loops linked list algorithm C + + implementation

Source: Internet
Author: User

It is known that the data elements in a single-linked list contain three types of characters (i.e. alphabetic characters, numeric characters and other characters), try to write the algorithm, construct three circular linked lists, make each loop list contain only the same class of characters, and use the node space in the original table as the node space of the three tables.

Implementing the Source code:

#include <iostream>

#include <stdio.h>

#include <string.h>

using namespace Std;

struct node

{

Char ch;

Node*link;

};

To facilitate the output, define an output character function for single-linked lists

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 ()

{

The following constructs a single-link list of characters

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);

Digital Loop Linked List

Node*head1=new node ();

Letter Loop Linked List

Node*head2=new node ();

Other character loop linked list

Node*head3=new node ();

The following extracts numbers, letters, and other characters from "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;

}

The end-to-end phase is

p1->link=head1;

p2->link=head2;

p3->link=head3;

At this point, the loop linked list is established

cout<<endl;

Printcirclelist (HEAD1);

cout<<endl;

Printcirclelist (HEAD2);

cout<<endl;

Printcirclelist (HEAD3);

return 0;

}

Operation Result:

(This algorithm can recognize three different characters and construct a circular list)

Character single-linked list identifies numbers, letters, other characters, and is divided into three loops linked list algorithm C + + implementation

Related Article

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.