c_cpp 在链表中排列辅音和元音节点

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 在链表中排列辅音和元音节点相关的知识,希望对你有一定的参考价值。

//https://www.geeksforgeeks.org/arrange-consonants-vowels-nodes-linked-list/
#include <iostream>
using namespace std;

struct LL {
    char data;
    struct LL *next;
};
typedef struct LL node;

void push (node **headref, char c) {
    node *temp= (node*)malloc(sizeof(node));
    temp->data= c;
    temp->next= NULL;

    if (*headref== NULL) {
        *headref= temp;
        return;
    }
    node *last= *headref;
    while (last->next)
        last= last->next;
    last->next= temp;
}
void print (node *n) {
    while (n) {
        cout<< n->data<< " ";
        n= n->next;
    }
    return;
}
bool isVowel(char x)  {
    return (x == 'a' || x == 'e' || x == 'i' ||
            x == 'o' || x == 'u');
}
node* func(node *head) {
    node *newHead= head;

    node *lastVowel;
    node *curr= head;
    if (head== NULL)
        return NULL;
    if (isVowel(head->data))
        lastVowel= head;
    else {
        while (curr->next && !isVowel(curr->next->data))
            curr= curr->next;
        if (curr->next== NULL)
            return head;
        lastVowel= newHead= curr->next;
        curr->next= curr->next->next;
        lastVowel->next= head;
    }
    while (curr && curr->next) {
        if (isVowel(curr->next->data)) {
            if (curr== lastVowel)
                lastVowel= curr= curr->next;
            else {
                node *temp= lastVowel->next;
                lastVowel->next= curr->next;
                lastVowel= lastVowel->next;
                curr->next= curr->next->next;
                lastVowel->next= temp;
            }
        }
        else {
            curr= curr->next;
        }
    }
    return newHead;
}
int main() {
    node *head= NULL;
    push (&head, 'a');
    push (&head, 'e');
    push (&head, 'g');
    push (&head, 'h');
    push (&head, 'i');
    push (&head, 'm');
    push (&head, 'p');
    push (&head, 'e');
    push (&head, 'r');
    push (&head, 'm');
    push (&head, 'u');

    print(head);
    head= func(head);
    cout<<endl;
    print(head);
}

以上是关于c_cpp 在链表中排列辅音和元音节点的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 在链表中查找循环的长度

c_cpp 该程序从用户获取一个字符串,并查找该字符串中存在的元音,辅音,数字和空白的总数。

在链表中递归插入节点,给定位置和值

javascript中在链表中向前(向后)移动n个节点

在链表中添加节点时使用双指针的原因是啥?

在链表中删除该节点时,Node* 接下来会发生啥?