常用排序代码

本文详细介绍了使用C++实现队列操作的步骤,包括初始化队列、队列入队、队列出队和获取队首元素的方法。通过具体代码实例,展示了如何使用结构体和指针来高效地进行队列的增删查操作。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

typedef struct node
{
    int data;
    struct node *next;
};
typedef struct
{
    struct node *front;
    struct node *rear;
} LinkQueue;

InitQueue(LinkQueue *Q)
{
    Q->front=(struct node *)malloc(sizeof( struct node));
    Q->rear=Q->front;
    Q->front->next=NULL;
}

EnQueue(LinkQueue *Q,int e)
{
    struct node *p;
    p=(struct node *)malloc(sizeof(struct node ));
    p->data=e;
    p->next=NULL;
    Q->rear->next=p;
    Q->rear=p;
}

DeQueue(LinkQueue *Q,int e)
{
    struct node *p;
    if(Q->front==Q->rear)
        return 0;

    else
    {
        p=Q->front->next;
        Q->front->next=p->next;
        if(p->next=NULL)Q->rear=Q->front;
        return(p->data);
        free(p);
    }
}

OutputQueue(LinkQueue *Q)
{
    struct node *p;
    p=Q->front->next;
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
}

GetFront(LinkQueue *Q)
{
    struct node *p;
    p=Q->front->next;
    printf("%d",p->data);
}

void Fail(char str, int *next){  //KMP失败函数的编写。

    int k = -1, j = 0;

    while(j < strlen(str)){

        if(k ==- 1 || str[j] == str[k]){

            j++;
            k++;
            if(str[j] == str[k]) {  //写的是失败函数,当匹配失败后要怎样跳转。

                next[j] = next[k];
            }else
                next[j] = k;
        }else {

            k = next[k];
        }
    }
}

int KMP(string s1, string, s2, int pos){

    int i = pos, j = 0;
    int n = s1.size();
    int m = s2.size();
    while(i < len && j < m){

        if(j == -1 || s1[i] == s2[j]){
            i++;
            j++;
        }else
        j = next[i];
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值