顺序链表的实现,头插,头删,尾插,尾删,任意位置删除,任意位置插入,任意位置替换等基本函数

定义一个头文件

#ifndef _Sequlist_H_
#define _Sequlist_H_

#pragma once

#include<stdlib.h>  

#include<assert.h>  


typedef int Datatype;  
#define Max 2  
typedef struct Seqlist  
{  
    Datatype *arr;  
    size_t size;// 有效数据个数   
    size_t capicity;//容量  
}Seqlist;  
  
void Seqprin(Seqlist *pSeq);//读出  
void SeqInit(Seqlist* pSeq); //初始化  
void SeqDestory(Seqlist* pSeq); //销毁创建的空间  
Seqlist* SeqCheckFull(Seqlist *pSeq);//若空间不够则进行扩容  
  
  
void SeqPushBack(Seqlist* pSeq,Datatype x);//尾插  
void SeqPopback(Seqlist*pSeq);//尾删  
void SeqPushFront(Seqlist* pSeq,Datatype x);//头插  
void SeqPopFront(Seqlist* pSeq);//头删  
void SeqInsert(Seqlist *pSeq,size_t pos,Datatype x);//任意位置插入数据  
void SeqErase(Seqlist *pSeq,size_t pos);//把pos位置的数据删除  
int SeqFind(Seqlist *pSeq,Datatype x);//查找一个数,若找到并返回它  
void SeqAt(Seqlist *pSeq,size_t pos,Datatype x);//替换指定位置的值  
void BubbleSort(Seqlist *pSeq);//用冒泡排序法排列  

void SelectSort(Seqlist *pSeq);//用选择排序法排序  

#endif

#include"Sequlist.h"  
  
int main()  
{  
    Seqlist Seqlist;//定义结构体名  
  
  
    SeqInit(&Seqlist);  
    SeqPushBack(&Seqlist,1);//尾插  
    SeqPushBack(&Seqlist,6);//尾插  
    SeqPushBack(&Seqlist,3);//尾插  
    SeqPushBack(&Seqlist,4);//尾插  
//  SeqPopback(&Seqlist);//尾删(测试专用)  
//  SeqPushFront(&Seqlist,4);//头插(测试专用)  
//  SeqPopFront(&Seqlist);//头删(测试专用)  
  
  
  
//  SeqInsert(&Seqlist,2,0);  
//  SeqErase(&Seqlist,2);  
//  SeqFind(&Seqlist,2);  
//  SeqAt(&Seqlist,2,0);  
    BubbleSort(&Seqlist);  
  
    Seqprin(&Seqlist);//读出所有数据  
    system("pause");  
    return 0;  
}  
  
void Seqprin(Seqlist *pSeq)//读写  
{  
    int i;  
    for(i=0;i<pSeq->size;i++)  
    printf("%d ",pSeq->arr[i]);  
}  
void SeqInit(Seqlist* pSeq)//初始化  
{  
    pSeq->size=0;  
    pSeq->capicity=Max;  
    pSeq->arr=(Datatype *)malloc(pSeq->capicity*sizeof(Datatype));  
    if(NULL==pSeq->arr)  
    {  
        printf("创建空间失败");  
    }  
}  
void SeqDestory(Seqlist *pSeq)//销毁所创建的空间  
{  
    assert(pSeq);//断言判断是否为空  
    pSeq->size=0;  
    pSeq->capicity=0;  
    free(pSeq->arr);  
}  
Seqlist* SeqCheckFull(Seqlist *pSeq)//进行扩容  
{  
    Datatype *tem=(Datatype *)realloc(pSeq->arr,sizeof(Datatype)*pSeq->capicity*2);//进行扩容  
        pSeq->capicity*=2;  
        pSeq->arr=tem;  
        return pSeq;  
}  
  
void SeqPushBack(Seqlist* pSeq,Datatype x)//尾插  
{  
    assert(pSeq);  
    if(pSeq->size==pSeq->capicity)  
    {  
        SeqCheckFull(pSeq);  
    }  
    pSeq->arr[pSeq->size++]=x;  
}  
void SeqPopback(Seqlist*pSeq)//尾删  
{  
    assert(pSeq);  
    if(pSeq->size==0)  
    {  
        printf("顺序表里没有数据");  
        return;  
    }  
    pSeq->size--;  
}  
void SeqPushFront(Seqlist* pSeq,Datatype x)//头插  
{     
    int i=pSeq->size;  
    assert(pSeq);  
  
    for(;i>0;i--)  
    pSeq->arr[i]=pSeq->arr[i-1];  
    pSeq->arr[i]=x;  
    pSeq->size++;  
}  
void SeqPopFront(Seqlist* pSeq)//头删  
{  
    int i=0;  
    assert(pSeq);  
      
    if(pSeq->size==0)  
    {  
        printf("顺序表里没有数据");  
        return;  
    }  
    for(;i<pSeq->size;i++)  
        pSeq->arr[i-1]=pSeq->arr[i];  
    pSeq->size--;  
}  
void SeqInsert(Seqlist *pSeq,size_t pos,Datatype x)//在pos位置插入数据  
{  
    int end;  
    assert(pSeq);  
    assert(pos<=pSeq->size);  
    end=pSeq->size-1;  
    while(end>=pos)  
    {  
        pSeq->arr[end+1]=pSeq->arr[end];  
        --end;  
    }  
    pSeq->arr[pos]=x;  
    pSeq->size++;  
}  
void SeqErase(Seqlist *pSeq,size_t pos)//把pos位置的数据删除  
{  
    assert(pSeq);  
    assert(pos<=pSeq->size);  
    while(pos<pSeq->size)  
    {  
        pSeq->arr[pos]=pSeq->arr[pos+1];  
        pos++;  
    }  
    pSeq->size--;  
}  
int SeqFind(Seqlist *pSeq,Datatype x)//查找一个数,若找到并返回它  
{  
    int i=0;  
    assert(pSeq);  
    for(;i<pSeq->size;i++)  
    {  
        if(pSeq->arr[i]==x)  
            return i+1;  
    }  
    return -1;  
}  
void SeqAt(Seqlist *pSeq,size_t pos,Datatype x)//替换指定位置的值  
{  
    assert(pSeq);  
    assert(pos<=pSeq->size);  
    pSeq->arr[pos-1]=x;  
}  
void Swap(Datatype *x,Datatype *y)//交换函数  
{  
    Datatype tem=*x;  
    *x=*y;  
    *y=tem;  
}  
void BubbleSort(Seqlist *pSeq)//用冒泡排序法排列  
{  
    int i,j,flage;  
    assert(pSeq);  
    for(i=0;i<pSeq->size-1;i++)  
    {  
        flage=0;  
        for(j=0;j<pSeq->size-i-1;j++)  
        {  
                if(pSeq->arr[j]>pSeq->arr[j+1])  
                {  
                    Swap(&(pSeq->arr[j]),&(pSeq->arr[j+1]));  
                    flage=1;  
                }  
        }  
        if(flage==0)  
                break;  
    }  

猜你喜欢

转载自blog.csdn.net/sd116460/article/details/80667662