自写的顺序表的一些基本处理

来源:互联网 发布:java编程案例 编辑:程序博客网 时间:2024/05/03 11:36
#include<iostream>using namespace std;#define MAXSIZE 30struct List{int array[MAXSIZE];int length;};//定义结构体void display(List *pl){for(int i=0;i<pl->length;i++)cout<<pl->array[i]<<",";cout<<"size:"<<pl->length<<endl;}//显示函数void Init(List *pl,int n){for(int i=0;i<n;i++){pl->array[i]=i+10;}pl->length=n;}//初始化函数void insert(List *pl,int n,int x){for(int i=pl->length-1;i>=n;i--)pl->array[i+1]=pl->array[i];pl->array[i+1]=x;pl->length++;}//插入单元函数void del(List *pl,int n){for(int i=n;i<pl->length-1;i++)pl->array[i]=pl->array[i+1];pl->length--;}//删除单元函数int size(List *pl){return pl->length;}//计算长度函数void sortlist(List *pl){for(int j=0;j<pl->length-1;j++)for(int i=j+1;i<pl->length;i++)if(pl->array[i]<pl->array[j]){int temp=pl->array[i];pl->array[i]=pl->array[j];pl->array[j]=temp;}}//冒泡排序函数int main(){//初始化并显示List* p=(List*)malloc(sizeof(List));Init(p,10);cout<<"初始化并显示:"<<endl;display(p);//插入insert(p,3,77);cout<<"插入:insert(p,3,77);"<<endl;display(p);//删除del(p,5);cout<<"删除:del(p,5);"<<endl;display(p);//冒泡排序sortlist(p);cout<<"冒泡排序:sortlist(p);"<<endl;display(p);return 0;}