备用

///链表
#include <bits/stdc++.h>
using namespace std;

typedef struct aa
{
    int date;
    struct aa *next;
} sq,*List;


void tailcreat(List &l,int n)//尾插
{
    int i;
    List rr=l;
    List p=l->next;
    for(int i=0; i<n; i++){
        List node=(List)malloc(sizeof(sq));
        node->next=NULL;
        scanf("%d",&node->date);
        rr->next=node;
        rr=rr->next;
    }
    return ;
}

void headcreat(List &l,int n)//头插
{
    int i;
    List h;
    List p;
    for(i=0; i<n; i++){
        p=(List)malloc(sizeof(sq));
        p->next=NULL;
        scanf("%d",&p->date);
        p->next=l->next;
        l->next=p;
    }
    return ;
}


void _insert(List &h)
{
    int n;
    List l=h->next;
    List node=(List)malloc(sizeof(sq));
    printf("
请输入要插入的位置:");
    scanf("%d",&n);
    printf("
请输入要插入的数值:
");
    scanf("%d",&(node->date));
    for(int i=0;i<n-2;i++){
        l=l->next;
    }
    node->next=l->next;
    l->next=node;
    return ;
}

///删除链表里的某个数值
void delval(List &h,int num)
{
    List p=h;
    while(p->next){
        bool ok=true;
        while(p->next->date==num){
            if(p->next->next==NULL){
                p->next=NULL;
                ok=false;
                break;
            }
            else{
                List as=p->next;
                p->next=as->next;
            }
        }
        if(!ok) break;
        p=p->next;
    }
    return ;
}

void length(List &h)
{
    List p=h;
    int num=-1;
    while(p){
        p=p->next;
        num++;
    }
    printf("
链表的长度为:%d
",num);
    return ;
}
int main()
{
    List head=(List)malloc(sizeof(sq));
    head->next=NULL;
    int n;
    scanf("%d",&n);
    tailcreat(head,n);
//    headcreat(head,n);
    _insert(head);
    int num;
    printf("
输入删除的数字
");
    scanf("%d",&num);
    delval(head,num);
    length(head);
    return 0;
}


///链表栈
#include <bits/stdc++.h>
using namespace std;
typedef struct aa
{
    int date;
    struct aa *next;
} sq,*List;

void creatstk(List &l,int n)//尾插
{
    int i;
    List rr=l;
    List p=l->next;
    for(int i=0; i<n; i++)
    {
        List node=(List)malloc(sizeof(sq));
        node->next=NULL;
        scanf("%d",&node->date);
        rr->next=node;
        rr=rr->next;
    }
    List h=l->next;
    return ;
}

void length(List &h)
{
    List p=h;
    int num=-1;
    while(p){
        p=p->next;
        num++;
    }
    printf("
栈的长度为:%d
",num);
    return ;
}

void push(List &h,int val)
{
    List p=h;
    while(p->next) p=p->next;
    List node=(List)malloc(sizeof(sq));
    p->next=node;
    node->next=NULL;
    node->date=val;
    List l=h->next;
    return ;
}

void pop(List &h)
{
    List p=h;
    while(p->next->next) p=p->next;
    p->next=NULL;
    List l=h->next;
    return ;
}

int main()
{
    List head=(List)malloc(sizeof(sq));
    head->next=NULL;
    int n;
    scanf("%d",&n);
    creatstk(head,n);
    push(head,9);
    pop(head);
    pop(head);
    length(head);
    return 0;
}


///链表队列
#include <bits/stdc++.h>
using namespace std;

typedef struct aa
{
    int date;
    struct aa *next;
} sq,*List;

void creatstk(List &l,int n)//尾插
{
    int i;
    List rr=l;
    List p=l->next;
    for(int i=0; i<n; i++){
        List node=(List)malloc(sizeof(sq));
        node->next=NULL;
        scanf("%d",&node->date);
        rr->next=node;
        rr=rr->next;
    }
    List h=l->next;
    return ;
}

void length(List &h)
{
    List p=h;
    int num=-1;
    while(p){
        p=p->next;
        num++;
    }
    printf("
队列的长度为:%d
",num);
    return ;
}

void push(List &h,int val)
{
    List p=h;
    while(p->next) p=p->next;
    List node=(List)malloc(sizeof(sq));
    p->next=node;
    node->next=NULL;
    node->date=val;
    List l=h->next;
    return ;
}

void pop(List &h)
{
    h=h->next;
    List l=h->next;
    return ;
}

int main()
{
    List head=(List)malloc(sizeof(sq));
    head->next=NULL;
    int n;
    scanf("%d",&n);
    creatstk(head,n);
    push(head,9);
    pop(head);
    pop(head);
    length(head);
    return 0;
}


/*
线性表顺序查找,
二分查找为数组方式,用法相同,只用在前面加线性表的创建,用法和数组类似
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
typedef struct LIST
{
    int *elem;
    int length;
    int list_size;
}List;

bool InitList_Sq(List *L)
{
     L->elem = (int *)malloc(100*sizeof(int));
     L->length = 0;
     L->list_size = 100;
    return true;
}
int stk[maxn];
int top;
int main()
{
    List  L;
    InitList_Sq(&L);
    List  a;
    InitList_Sq(&a);
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&L.elem[i]);
        a.elem[i]=L.elem[i];
    }
    int val;
    scanf("%d",&val);
    printf("顺序查找:");
    bool ok=true;
    int k=-1;
    for(int i=0;i<n;i++){
        if(L.elem[i]==val){
            ok=false;
//            k=i;
            stk[top++]=i;
        }
    }
    if(!ok){
        for(int i=0;i<top;i++){
            printf("L.elem[%d]==%d
",stk[i],val);
        }
    }
    else puts("no search");
    return 0;
}


/*
数组二分查找,注意题目上是否说明数据时有序的,无序时需要将数据从小到大排序
如果需要输出排序前的位置,就用一个结构体把之前的位置保留下来,输出时输出原来位置
排序对num排序
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
/*
struct node
{
    int num;
    int pos;
}a[maxn];
*/
int a[maxn];
int n;

void insertsort()
{
    for(int i=1;i<n;i++){
        if (a[i-1]>a[i]){
            int temp=a[i];
            int j=i;
            while(j&&a[j-1]>temp){
                a[j] = a[j-1];
                j--;
            }
            a[j]=temp;
        }
    }
    return ;
}

int main()
{
    while(~scanf("%d",&n)){
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        int val;///查询的数
        scanf("%d",&val);
        insertsort();
        int l=1,r=n,mid;
        while(l<=r){
            mid=(l+r)>>1;
            if(a[mid]>=val){
                r=mid-1;
            }
            else{
                l=mid+1;
            }
        }
        if(a[l]==val){
            printf("pos==%d
",l);
        }
        else{
            printf("no search
");
        }
    }
    return 0;
}

/*
线性表存储的排序,数组方式只需要把c.elem全部替换成数组名即可
注意在新建线性表的时候申请内存时要大,不然会Runtime Error
*/
#include <bits/stdc++.h>
using namespace std;
int n;
typedef struct LIST
{
    int *elem;
    int length;
    int list_size;
}List;

bool InitList_Sq(List *L)
{
     L->elem = (int *)malloc(100000*sizeof(int));
     L->length = 0;
     L->list_size = 100000;
    return true;
}

///冒泡
void maopao(List c)
{
    for(int i=0;i<n;i++){
        for(int j=0;j<n-i-1;j++){
            if(c.elem[j]>c.elem[j+1]){int t=c.elem[j];c.elem[j]=c.elem[j+1];c.elem[j+1]=t;}
        }
    }
    return ;
}

///插入
void insertsort(List c)
{
    for(int i=1;i<n;i++){
        if (c.elem[i-1]>c.elem[i]){
            int temp=c.elem[i];
            int j=i;
            while(j&&c.elem[j-1]>temp){
                c.elem[j] = c.elem[j-1];
                j--;
            }
            c.elem[j]=temp;
        }
    }
    return ;
}

///选择
void choosesort(List c)
{
    for(int i=0;i<n;i++){
            int minn=9999,k=-1;
            for(int j=i;j<n;j++){
                if(c.elem[j]<minn){
                    minn=c.elem[j],k=j;
                }
            }
//            swap(b.elem[i],b.elem[k]);
            int t=c.elem[i];c.elem[i]=c.elem[k];c.elem[k]=t;
    }
}
///快排
void quicksort(List c,int p,int q)
{
    int i=p;
    int j=q;
    int temp=c.elem[p];
    while(i<j)
    {
        while(c.elem[j]>=temp&&j>i) j--;
        if(j>i){
            c.elem[i]=c.elem[j];
            i++;
            while(c.elem[i]<=temp&&i<j)i++;
            if(i<j){
                c.elem[j]=c.elem[i];
                j--;
            }
        }
    }
    c.elem[i]=temp;
    if(p<(i-1)) quicksort(c,p,i-1);
    if(j+1<q) quicksort(c,j+1,q);
}

int main()
{
    List  L;
    InitList_Sq(&L);
    List  a;
    InitList_Sq(&a);
    List  b;
    InitList_Sq(&b);
    List  c;
    InitList_Sq(&c);
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&L.elem[i]);
        a.elem[i]=L.elem[i];
        b.elem[i]=L.elem[i];
        c.elem[i]=L.elem[i];
    }
    maopao(L);
    insertsort(a);
    choosesort(b);
    quicksort(c,0,n-1);
    return 0;
}


/*
图dfs,用临接矩阵存储
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e4+7;
int mp[maxn][maxn];
const int inf=0x3f3f3f3f;
bool vis[maxn];
int n,m;
void dfs(int now,int pre)
{
    for(int i=1;i<=n;i++){
        if(!vis[i]&&mp[now][i]){
            int v=mp[now][i];
            vis[i]=true;
            printf("%d",i);
            dfs(i,now);
        }
    }
    return ;
}
int main()
{
    while(~scanf("%d%d",&n,&m)){
        memset(mp,false,sizeof(mp));
        memset(vis,false,sizeof(vis));
        for(int i=1;i<=n;i++) mp[i][i]=1;
        int t1,t2;
        for(int i=0;i<m;i++){
            scanf("%d%d",&t1,&t2);
            mp[t1][t2]=1;
            mp[t2][t1]=1;
        }
        vis[1]=true;
        printf("1");
        dfs(1,1);
        puts("");
    }
    return 0;
}


/*
图Bfs,用邻接矩阵存储
*/
#include<bits/stdc++.h>
const int maxn = 1e4;
using namespace std;

int mpp[maxn][maxn];
int vis[maxn];

int que[maxn];
int l=0,r=0;

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        memset(que,0,sizeof(que));
        l=0,r=0;
        memset(mpp,0,sizeof(mpp));
        memset(vis,0,sizeof(vis));
        for(int i = 0;i < m;i ++){
            int a,b;
            scanf("%d%d",&a,&b);
            mpp[a][b]=1;
            mpp[b][a]=1;
        }
        vis[1] = 1;
        que[r++]=1;
        while((r-l)){
            int t = que[l];
            printf("%d",t);
            l++;
            for(int i = 1;i <= n;i ++){
                if(mpp[t][i] == 1 && vis[i] == 0){
                    que[r++]=i;
                    vis[i] = vis[t] + 1;
                }
            }
        }
    }
    return 0;
}

/*
kmp算法
*/
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define maxe 1000002

int next[maxe];
char S[maxe], T[maxe];
int slen, tlen;

void getNext()
{
    int j, k;
    j = 0;
    k = -1;
    next[0] = -1;
    while(j < tlen)
        if(k == -1 || T[j] == T[k])
//这个地方先执行 ++k,然后执行++j 最后赋值
            next[++j] = ++k;
//表示T[j-1]和T[k-1]相匹配,当j处失配时,直接用next[j]处来匹配当前失配处
        else
            k = next[k];
}

/*
返回模式串T在主串S中首次出现的位置
返回的位置是从0开始的。
*/
int KMP_Index()
{
    int i = 0, j = 0;
    getNext();
    while(i < slen && j < tlen){
        if(j == -1 || S[i] == T[j]){
            i++;
            j++;
        }
        else
            j = next[j];
    }
    if(j == tlen)
        return i - tlen;
    else
        return -1;
}

/*
返回模式串在主串S中出现的次数
*/
int KMP_Count()
{
    int ans = 0;
    int i, j = 0;
    if(slen == 1 && tlen == 1){
        if(S[0] == T[0])
            return 1;
        else
            return 0;
    }

    getNext();
    for(i = 0; i < slen; i++){
        while(j > 0 && S[i] != T[j])
            j = next[j];
        if(S[i] == T[j])
            j++;
        if(j == tlen){
            ans++;
            j = next[j];
        }
    }
    return ans;

}

int main()
{
    int TT;
    int i, cc;
    scanf("%d",&TT);
    while(TT--)
    {
        scanf("%s %s",&S,&T);
        slen = strlen(S);
        tlen = strlen(T);
        getNext();
        for(int i=1; i<=tlen; i++)
            printf("%d ",next[i]);
        printf("
");
        cout<<"模式串T在主串S中首次出现的位置是: "<<KMP_Index()<<endl;
        cout<<"模式串T在主串S中出现的次数为: "<<KMP_Count()<<endl;
    }
    return 0;
}

/*
3
ababcabcacbabcac
abcac
*/
/*
二叉树遍历,想了很久也没有写dfs序,因为dfs序遍历是和先序遍历相同的,如果为其他的树的话,直接改递归函数就好了
*/
#include <bits/stdc++.h>
using namespace std;

typedef struct node
{
    char date;
    struct node *lchild,*rchild;
    //int vis;
}treeNode, *tree;
void create(tree &T)
{
    char c;
    cin >> c;
    if('#' == c)
        T = NULL;
    else{
        T = (tree)malloc(sizeof(treeNode));
        T->date=c;
        create(T->lchild);
        create(T->rchild);
    }
}
void pre(tree T)
{
    if(T){
        printf("%c",T->date);
        pre(T->lchild);
        pre(T->rchild);
    }
}
void mid(tree T)
{
    if(T){
        mid(T->lchild);
        printf("%c",T->date);
        mid(T->rchild);
    }
}
void post(tree T)
{
    if(T){
        post(T->lchild);
        post(T->rchild);
        printf("%c",T->date);
    }
}

int main()
{
    tree root;
    int n;
    while(~scanf("%d",&n)){
        create(root);
        pre(root);
        mid(root);
        post(root);
    }
    return 0;
}
/*
ab##c##
123####
*/

 给你完全二叉树的每个节点的标号,打印中序遍历

#include <bits/stdc++.h>
using namespace std;

const int maxn=1e5+7;
char a[maxn];
int len;

typedef struct tree
{
    tree *l,*r;
    char val;
}tree;
tree *root;
tree *creat(int x)
{
    tree *ss;
    ss=(tree *)malloc(sizeof(tree));
    ss->val=a[x];
//    printf("test %d
",x);
    if(x*2<=len)ss->l=creat(x*2);
    else ss->l=NULL;
    if(x*2+1<=len)ss->r=creat(x*2+1);
    else ss->r=NULL;

    return ss;
}
void solve(tree *h)
{
    stack<tree*> s;
    s.push(h);
    while(!s.empty()){
        while(s.top()->l!=NULL){
            s.push(s.top()->l);
        }
        while(!s.empty()){
            tree *ss=s.top();s.pop();
            if(ss->val!='#')printf("%c",ss->val);
            if(ss->r!=NULL){
                s.push(ss->r);
                break;
            }
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%s",a+1);
        len=strlen(a+1);
        bool ok=true;
        for(int i=1;i<=len;i++){
            if(a[i]!='#'){
                ok=false;break;
            }
        }
        if(ok){
            puts("null");
            continue;
        }
        root=NULL;
        root=creat(1);
//        printf("test
");
        solve(root);
        puts("");
    }
    return 0;
}
/*
2
ABC##DE####FGH
#

1
ABC##DE####FGH

*/

 快排

#include <bits/stdc++.h>
using namespace std;

int a[15];
void quicksort(int l, int r)
{
    if(l > r)
        return ;
    int i = l, j = r;
    int tmp_val = a[l];
    while(i < j) {
        while(a[j] > tmp_val && i < j) j--;
        while(a[i] <= tmp_val && i < j) i++;
        if(j > i)swap(a[i], a[j]);
    }
    swap(a[l], a[i]);
    quicksort(l, i - 1);
    quicksort(i + 1, r);
}

int main()
{
    int n;
    while(~scanf("%d", &n)) {
        for (int i = 1; i <= n; ++i)
            scanf("%d", &a[i]);
        quicksort(1, n);
        for (int i = 1; i <= n; ++i)
            printf("%d ", a[i]);
        puts("");
    }
    return 0;
}

堆排序

#include <bits/stdc++.h>
using namespace std;
int a[555];

//堆排序的核心是建堆,传入参数为数组,根节点位置,数组长度
void Heap_build(int root,int length)
{
    int tmp = root;
    if((root *2 + 1 < length) && a[tmp] < a[root * 2 + 1]) tmp =  root *2 + 1;
    if((root *2 + 2 < length) && a[tmp] < a[root * 2 + 2]) tmp =  root *2 + 2;
    if(tmp != root) {
        swap(a[tmp], a[root]);
        Heap_build(tmp, length);
    }
}

void Heap_sort(int len)
{
    for (int i = len/2; i >= 0; --i)
        Heap_build(i,len);
    for (int i = len-1; i > 0; --i){
        swap(a[0],a[i]);
        Heap_build(0,i);
    }
}
int main(int argc, char **argv)
{
    int n;
    while(cin >> n) {
        for(int i = 0; i < n; ++i)
            cin >> a[i];
        Heap_sort(n);
        for (size_t i = 0; i != n; ++i)
            cout<<a[i]<<" ";
        cout<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lalalatianlalu/p/8082923.html