数据结构-代码(自用)

第二章:动态分配顺序表:

#include <iostream>

using namespace std;

#define InitSize 10
#define ElemType int

typedef struct
{
    ElemType *data;
    int MaxSize;    //表示当前最大长度
    int length;     //表示当前字符串长度
} SeqList;

/*
* 初始化顺序表
*/
void InitList(SeqList &L)
{
    L.data=(ElemType *)malloc(sizeof(ElemType)*InitSize);
    L.length=0;
    L.MaxSize=InitSize;
}

/*
*   求顺序表长度
*/
int Length(SeqList &L)
{
    return L.length;
}

/*
*   查找顺序表元素
*/
int LocateElem(SeqList L,ElemType e)
{
    for(int i=0; i<L.length; i++)
    {
        if(e==L.data[i])
        {
            return i;
        }
    }
    return -1;
}

ElemType getElem(SeqList L,int i)
{
    return L.data[i-1];
}


bool listInsert(SeqList &L,int i,ElemType e)
{
    if(i<1||i>L.length+1||i>L.MaxSize)
    {
        return false;
    }

    //ElemType *p=L.data;

    for(int k=L.length; k>=i; k--)
    {
        L.data[k]=L.data[k-1];
    }
    L.data[i-1]=e;
    L.length++;
    return true;
}

void IncreaseSize(SeqList &L,int len)
{
    int *p=L.data;
    L.data=(ElemType *)malloc(sizeof(ElemType)*(L.MaxSize+len));
    for(int i=0; i<L.MaxSize; i++)
    {
        L.data[i]=p[i];
    }
    L.MaxSize=L.MaxSize+len;
    free(p);
}

bool Empty(SeqList L)
{
    if(L.length==0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
void Transpose(SeqList&L){
    for(int i=0;i<L.length/2;i++){
        ElemType temp=L.data[i];
        L.data[i]=L.data[L.length-i-1];
        L.data[L.length-i-1]=temp;
    }
}

void DeleteAllNum(SeqList&L,ElemType e){
    int num=0;
    for(int i=0;i<L.length;i++)
    {
        if(L.data[i]!=e)
        {
            L.data[num]=L.data[i];
            num++;
        }
    }
    L.length=num;
}
bool DeleteMinElm(SeqList&L,ElemType &e)
{
    if(L.length<=0)
    {
        return false;
    }
    int pos=0;
    e=L.data[0];
    for(int i=0; i<L.length; i++)
    {
        if(e>L.data[i])
        {
            e=L.data[i];
            pos=i;
        }
    }
    L.data[pos]=L.data[L.length-1];
    L.length--;
    return true;
}

void PrintList(SeqList L)
{
    for(int i=0; i<L.length; i++)
        cout<<L.data[i]<<endl;
}

void DestoryList(SeqList &L)
{
    free(L.data);
}
int main()
{
    SeqList L;
    InitList(L);
    IncreaseSize(L,5);
    listInsert(L,1,3);
    listInsert(L,1,3);
    listInsert(L,1,3);
    listInsert(L,1,45);
    PrintList(L);
    ElemType temp;
    DeleteAllNum(L,3);
    //Transpose(L);
    //DeleteMinElm(L,temp);
    PrintList(L);
    return 0;
}

 第三章:栈与队列

顺序栈

#include <iostream>

using namespace std;
#define Maxsize 10      //栈中元素最大容量
#define ElemType int
typedef struct
{
    ElemType data[Maxsize];     //静态数组存放元素
    int top;    //栈顶指针
} SqStack;

void InitStack(SqStack &S)
{
    S.top=-1;
}

bool StackEmpty(SqStack S)
{
    if(S.top==-1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void Push(SqStack &S,ElemType X)
{
    if(S.top<Maxsize)
    {
        S.data[++S.top]=X;
    }
    else
    {
        cout<<"栈溢出"<<endl;
    }
}

void Pop(SqStack &S,ElemType &X)
{
    if(S.top>-1)
    {
        X=S.data[S.top--];
    }
    else
    {

        cout<<"已经到达栈底"<<endl;
    }
}

void GetTop(SqStack &S,ElemType &X)
{
    if(S.top>-1)
    {
        X=S.data[S.top];
    }
    else
    {

        cout<<"栈已经空"<<endl;
    }
}

void Destory(SqStack &S)
{
    S.top=-1;
}

int main()
{
    SqStack S;
    InitStack(S);
    Push(S,6);
    int x,y;
    Pop(S,x);
    cout<<x<<","<<y<<endl;
    cout<<StackEmpty(S);
    return 0;
}

 循环队列:

#include <iostream>

using namespace std;
#define Maxsize 3
#define ElemType int
typedef struct
{
    ElemType data[Maxsize];
    int front;
    int rear;
} SeqQueue;

void InitQueue(SeqQueue &Q)
{
    Q.front=0;      //表示队头    出队
    Q.rear=0;       //表示队列尾     入队
}

/*判断队空*/
bool QueueEmpty(SeqQueue Q)
{
    if(Q.front==Q.rear)
    {
        return true;
    }
    else
    {
        return false;
    }
}


bool EnQueue(SeqQueue &Q,ElemType X)
{
    if(Q.front==(Q.rear+1)%(Maxsize))     //留一个位置
    {
        cout<<"c出错了;"<<Q.front<<";"<<Q.rear<<endl;
        return false;
    }
    else
    {
        cout<<Q.front<<";"<<Q.rear<<endl;
        Q.data[Q.rear++]=X;
        return true;
    }

}

bool DeQueue(SeqQueue &Q,ElemType &X)
{
    if(Q.rear==Q.front)
    {
        cout<<"队列为空"<<endl;
        return false;
    }
    else
    {
        X=Q.data[Q.front++];
        return true;
    }

}

bool GetHead(SeqQueue Q,ElemType &X)
{
    if(Q.rear==Q.front)
    {
        return false;
    }
    else
    {
        X=Q.data[Q.front];
        return true;
    }
}


int main()
{
    SeqQueue S;
    InitQueue(S);
    ElemType a,b,c;

    cout<<QueueEmpty(S);
    EnQueue(S,1);
    EnQueue(S,2);
    cout<<QueueEmpty(S);
    EnQueue(S,3);
    DeQueue(S,a);
    DeQueue(S,b);
    DeQueue(S,c);
    cout<<a<<b<<c;
    return 0;
}

 括号匹配

#include <iostream>

using namespace std;
#define MaxSize 10
#define Elemtype char


typedef struct
{
    Elemtype data[MaxSize];
    int top;
} SeqStack;

void InitStack(SeqStack &S)
{
    S.top=-1;
}

bool Push(SeqStack &S,Elemtype x)
{
    if(S.top==MaxSize)
    {
        return false;
    }
    else
    {
        S.data[++S.top]=x;
        return true;
    }
}

bool Pop(SeqStack &S,Elemtype &x)
{
    if(S.top==-1)
    {
        return false;
    }
    else
    {
        x=S.data[S.top--];
        return true;
    }
}

bool Gettop(SeqStack &S,Elemtype &x)
{
    if(S.top==-1)
    {
        return false;
    }
    else
    {
        x=S.data[S.top];
        return true;
    }
}

bool StackEmpty(SeqStack S)
{
    if(S.top==-1)
        return true;
    else
        return false;
}

bool bracketCheck(char str[],int length)
{
    SeqStack S;
    InitStack(S);
    for(int i=0; i<length; i++)
    {
        if(str[i]=='('||str[i]=='{'||str[i]=='[')
            Push(S,str[i]);
        else
        {
            if(StackEmpty(S))
                return false;
            char topElem;
            Pop(S,topElem);
            if(str[i]==')'&&topElem!='(')
                return false;
            if(str[i]==']'&&topElem!='[')
                return false;
            if(str[i]=='}'&&topElem!='{')
                return false;
        }
    }
    return StackEmpty(S);
}
int main()
{
    cout<<bracketCheck("({[]})",6)<<endl;
    return 0;
}

 二叉树

#include <iostream>

using namespace std;
#define ElemType char
typedef struct BiTNode
{
    ElemType data;
    struct BiTNode *lchild,*rchild;
} BiTNode,*BiTree;


//先序建立二叉树
void CreateBTree(BiTree &S)
{
    ElemType ch;
    cin>>ch;
    //如果输入为#,表示无结点
    if(ch=='#')
    {
        S=NULL;
    }
    else
    {
        S=(BiTNode*)malloc(sizeof(BiTNode));
        S->data=ch;
        cout<<S->data<<endl;
        CreateBTree(S->lchild);
        CreateBTree(S->rchild);
    }
}

void prePrint(BiTree S)
{
    if(S!=NULL)
    {
        cout<<S->data;
        print(S->lchild);
        print(S->rchild);
    }
    else
    {
        return;
    }
}

int main()
{

    //定义二叉树根
    BiTree root=NULL;
    //创建二叉树
    CreateBTree(root);
    //先序输出二叉树
    prePrint(root);
    return 0;
}

 快速排序:

分治思想,选择基准,分成大于基准的与小于基准的(进行元素交换),对于分成的俩队再递归调用

#include <iostream>

using namespace std;
void QuickSort(int *array,int left,int right)
{
    if(left>=right)
    {
        return;
    }
    else
    {
        int Base=array[left];
        int low=left;
        int high=right;

        while(low<high)
        {
            while(array[high]>=Base&&low<high)
            {
                high--;
            }
            array[low]=array[high];

            while(array[low]<=Base&&low<high)
            {
                low++;
            }
            array[high]=array[low];

        }
        array[low]=Base;
        QuickSort(array,left,low-1);
        QuickSort(array,low+1,right);
    }

}

int main()
{
    int array[]= {2,5,8,9,1,5,6,2};
    QuickSort(array,0,7);
    for(int i=0; i<=7; i++)
        cout<<array[i];
    return 0;
}

 插入排序:

#include <iostream>

using namespace std;

#define Elemtype int

void InsertSort(Elemtype A[],int n) //A表示待排序序列(下标从零开始)  n表示序列长度
{
    int i,j,temp;
    for(i=1; i<n; i++)  //扫描未排序序列,i前面已经排序
    {
        if(A[i]<A[i-1]) //看是否小于已排序序列最大项目    123456[4]  //此时6最大
        {
            temp=A[i];

            for(j=i-1; j>=0&&temp<A[j]; j--)    //从已排序序列中 找合适位置插入,该算法具有稳定性
            {
                A[j+1]=A[j];
            }
            A[j+1]=temp;     //循环完后还会执行j--
        }
    }
}
int main()
{
    int a[]= {1,12,4,8,6,9,2,5,8,4,126,7,4,5,9,6,5,8,8,5,8,7,9,6,2};

    cout<<"排序前:";
    for(int i=0; i<(int)sizeof(a)/4; i++)
    {
        cout<<a[i]<<" ";
    }
    InsertSort(a,sizeof(a)/4);
    cout<<"
排序后:";
    for(int i=0; i<(int)sizeof(a)/4; i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/handsometaoa/p/15001625.html