算法

1.排序方法

1.1 冒泡排序

#include <stdio.h>

void sort(int *a,int len)

{

 int i=0;

 int j;

 int t;

    for(i=0;i<len;i++)

    {

        for(j=0;j<len-i-1;j++)

        {

            if(a[j]>a[j+1])

            {

                t=a[j];

                a[j]=a[j+1];

                a[j+1]=t;

            }

        }

    }

}

 

int main(int argc, char *argv[])

{

    int a[10]={ -999,2,3,77,12,88,0,-8,99,100};

    int i=0;

    sort(a,10);

    for(i=0;i<10;i++)

    {

        printf("%d ",a[i]);

    }

    return 0;

}

1.2 插入排序

有一个已经有序的数据序列,要求在这个已经排好的数据序列中插入一个数,但要求插入后此数据序列仍然有序,这个时候就要用到一种新的排序方法——插入排序法,插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个数加一的有序数据,算法适用于少量数据的排序,时间复杂度为O(n^2)。是稳定的排序方法。插入算法把要排序的数组分成两部分:第一部分包含了这个数组的所有元素,但将最后一个元素除外(让数组多一个空间才有插入的位置),而第二部分就只包含这一个元素(即待插入元素)。在第一部分排序完成后,再将这个最后元素插入到已排好序的第一部分中。

插入排序的基本思想是:每步将一个待排序的纪录,按其关键码值的大小插入前面已经排序的文件中适当位置上,直到全部插入完为止。

voi dinsert_sort(int *array,unsigned int n)

{

int i , j;

int temp;

for(i = 1 ; i < n ; i++)

{

temp = array[i];

for(j = i ; j > 0 && array[j-1] > temp; j--)

{

array[j] = array[j-1];

}

array[j]=temp;

}

}

}

1.3 快速排序

int partition(int *a,int left,int right)

{

    int i=left-1; int j = right;int key = a[right];int temp;

    while(1)

    {   

        while(a[++i]<key);//找到左边大于key的值

        while(a[--j]>key)//找到右边小于key的值

        {   

            if(j == i)break;

        }   

        if(j > i)

        {   

            temp = a[i];  a[i] = a[j];  a[j] = temp;

        }   

        else

           break;

      }   

    temp = a[i];  a[i] = a[right];  a[right] = temp;

    return i;

}

quick_sort(int *a,int left,int right)

{

    int q;

    if(left < right)

    {

        q = partition(a,left,right);

        quick_sort(a,left,q-1);

        quick_sort(a,q+1,right);

    }

}

int main()

{

    int an[10]={4,7,3,2,9,0,1,5,7,5};

    int i;

    quick_sort(an,0,10);

    for(i = 0;i<10;i++)

    {

        printf("%d ",an[i]);

    }

    printf("\n");

    return 0;

}

2.单链表

1)增加(head为链表的首地址,pt指向要插入结构体)

首插法:

//返回链表首地址

Struct student *add(Struct student *head,Struct student *pt)

{

    if(NULL == head)

    {

        return pt;

    }

    pt->next = head;

   return pt;

 

}

尾插法:

//返回首地址

Struct student *add(Struct student *headStruct student *pt)

{

Struct student *alhead;

Alhead = head;

    if(NULL == head)

    {

        return pt;

}

WhileNULL != head->next

{

head= head->next;

}

   Head ->next = pt;

Pt->next = NULL;

   return alhead;

}

按顺序插入:

//返回首地址

Struct student *add(Struct student *headStruct student *pt)

    Struct student *alhead = head;

    Struct student *pbuf= NULL;

 if(NULL == head)

    {

        return pt;

}

If(pt->id < head->id)

{

Pt->next = head;

Return pt;

}

WhileNULL != head && pt->id > =head->id

{

If(pt->id ==head->id)

{

Printf(the id have been.\n);

Return alhead;

}

Pbuf = head;

Head = head->next;

}

Pbuf->next = pt;

pt->next = head;

Return  alhead;

 

2)删除

//入参分别为链表首地址和要删除学生的地址,返回链表首地址

Struct student *del(Struct student *headstruct student *pstu)

     struct student *pt = head;

     struct student *pbuf;

    if(NULL == head)

    {

        printf("no data input\n");

        return NULL;

    }

   if(head == pstu)

   {

       pbuf = pstu->next;

       return pbuf;

   }

   while(NULL != head)

   {

        if(pstu == head)

        {

            break;

        }

        pbuf = head;

        head = head->next;

   }

   pbuf->next = pstu->next;

   return pt;

3)查找

struct student *findbyid(struct student *head,int id)

{

    while((NULL != head)&&(id >= head->id))

    {

        if(head->id == id)

        {

            return head;

        }

        head = head->next;

    }

 

    return NULL;

}

 

struct student *findbyname(struct student *head,char *name)

{

    int sn;

    while(NULL != head)

    {

        sn = struct studentmp(alhead->name,name);

        if(sn == 0)

        {

           return head;

        }

        head = head->next;

    }

    return NULL;

}

 

4)遍历

void showall(struct student *head)

{

    printf("\n");   

    while(NULL != head)

    {

       printf("id:%d name:%s old:%d \n",head->id,head->name,head->old);

       head = head->next;

    }

    printf("\n");   

}

 

5)倒置

struct student *revert(struct student *alhead)

{

    struct student *buf ;

    struct student *nhead = NULL;

    while(NULL != alhead)

    {

        buf = alhead;

        alhead = alhead -> next;

        buf->next = nhead;

        nhead = buf;

    }

    return nhead;

}

3.双向链表

4.双向循环链表

5.二叉树

6.哈希表

7.堆栈

堆栈(英语:stack),也可直接称栈。台湾作堆叠,在计算机科学中,是一种特殊的串列形式的数据结构,它的特殊之处在于只能允许在链接串列或阵列的一端(称为堆叠顶端指标,英语:top)进行加入资料(英语:push)和输出资料(英语:pop)的运算。另外堆叠也可以用一维阵列或连结串列的形式来完成。堆叠的另外一个相对的操作方式称为伫列。

由于堆叠数据结构只允许在一端进行操作,因而按照后进先出(LIFO, Last In First Out)的原理运作。

堆叠数据结构使用两种基本操作:推入(push)和弹出(pop):

推入:将数据放入堆叠的顶端(阵列形式或串列形式),堆叠顶端top指标加一。

弹出:将顶端数据资料输出(回传),堆叠顶端资料减一。

8.队列

队列,又称为伫列(queue),是先进先出(FIFO, First-In-First-Out)的线性表。在具体应用中通常用链表或者数组来实现。队列只允许在后端(称为rear)进行插入操作,在前端(称为front)进行删除操作。

队列的操作方式和堆栈类似,唯一的区别在于队列只允许新数据在后端进行添加。

例:使用类和结构体来实现

#ifndef _QUEUE_H_

#define _QUEUE_H_

struct node

{

    void *data;

    struct node *next;

};

class myqueue

{

    public:

        myqueue(void);

        ~myqueue(void);

        void put(void *);

        void *get(void);

        bool empty(void);

    private:

        struct node *head;

        struct node *tail;

};

#endif

 

#include <iostream>

#include "myqueue.h"

using namespace std;

myqueue::myqueue(void)

{

    head = NULL;

    tail = NULL;

}

myqueue::~myqueue(void)

{

    struct node *p;

    while(NULL !=head)

    {

        p = head->next;

        delete head;

        head = p;

}

head = tail = NULL;

}

void myqueue::put(void *data)

{

    struct node *p;

    p = new struct node;

    p->data = data;

    p->next = NULL;

    if(NULL == tail)

    {

        head = p;

        tail = p;

    }

    else

    {

        tail->next = p;

        tail = p;

    }

}

void * myqueue::get(void)

{

    void *p;

    struct node* pt;

    if(NULL == head)

    {

        throw string("the queue is null");

    }

    if(head == tail)

    {

        tail = NULL;

    }

    pt = head;

    p = head->data;

    head=head->next;

    delete pt;

    return p;

}

bool myqueue::empty(void)

{

     return NULL==head;

 }

 

#include <iostream>

#include "myqueue.h"

using namespace std;

int  main()

{

    myqueue st;

    char * data;

    st.put((void *)"1");

    st.put((void*)"2");

    st.put((void*)"3");

    st.put((void *)"4");

    while(1)

    {

        try

        {

            data = (char *)st.get();

            cout<<data<<" "<<endl;

        }

        catch(string e)

        {

            cout<<e<<endl;

            break;

        }

    }

} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值