将做工程过程比较常用的代码片段珍藏起来,如下资料是关于C++顺序表操作演示的代码,希望对大伙也有用处。
include
include "malloc.h"
include <stdio.h>
using namespace std;
struct SqList{
int length;
int listsize;
}L={NULL,0,0};
enum Status
{
};
Status InitList_Sq(SqList &L,int n=LIST_INT_SIZE)
{
if(0n)return FAILED;
if(!L.elem) exit(OVERFLOW1);
L.length=0;
L.listsize=n;
return OK;
}
Status ListInsert_Sq(SqList &L,int i,ElemType e)
if(L.length>=L.listsize)
{
L.elem=newbase;
L.listsize++;
}
for(;p>q;p--)
L.length++;
return OK;
}
{
if(L.length<0||i>L.length||i<=0) return FAILED;
if(0L.length) return EMPTY;
if(e)
for(;p<=L.elem+L.length;p++)
{
}
L.length--;
return OK;
}
Status ListInput_Sq(SqList &L,int n)
{
for(int i=0;i<n;i++)
{
L.length++;
}
return OK;
}
Status ListOutput_Sq(SqList &L)
{
p=L.elem;
cout<<"顺序表中的元素为:";
for(int i=0;i<L.length;i++)
{
}
cout<<endl;
return OK;
}
{
int max;
p=L.elem;
for(int i=0;i<L.length;i++)
{
}
return OK;
}
Status ListSort_Sq(SqList &L)
{
int tmp;
int i,j;
p=L.elem;
for(i=0;i<L.length-1;i++)
{
for(j=0;j<L.length-i-1;j++)
{
{
}
}
}
return OK;
}
Status ListRevorder_Sq(SqList &L)
{
int tmp;
p=L.elem;
for(int i=0;i<L.length/2;i++)
{
}
return OK;
}
Status ListRevorder1_Sq(SqList &L)
{
p=L.elem;
for(int i=0;i<L.length/2;i++)
{
}
return OK;
}
int main()
{
int opt;
int i,res,e,n;
n=0;
while(1)
{
if(n)
{
while(getchar()!='n'&&getchar()!=' ');
while(getchar()!='n'&&getchar()!=' ');
}
cout<<"1.建立一个顺序表,输入n个数字元素并输出"<<endl;
cout<<"2.查找线性表中的最大元素并输出"<<endl;
cout<<"3.在线性表的第i个元素前插入一个正整数x"<<endl;
cout<<"4.删除线性表中的第j个元素"<<endl;
cout<<"5.将线性表中的元素按升序排列"<<endl;
cout<<"6.将线性表中的元素就地逆序(只允许用一个暂存单元)"<<endl;
cout<<"7.将线性表中的元素就地逆序(不使用暂存单元)"<<endl;
cout<<"8.退出"<<endl<<endl;
cin>>opt;
switch (opt)
{
case 1:
cin>>n;
break;
case 2:
if(!L.elem)
{
cout<<"顺序表未初始化,请初始化顺序表!"<<endl<<endl;
break;
}
ListMax_Sq(L,&e);
cout<<"最大元素为:"<<e<<endl;
break;
case 3:
if(!L.elem)
{
cout<<"顺序表未初始化,请初始化顺序表!"<<endl<<endl;
break;
}
cout<<"请分别输入要插入的元素和要插入元素的位置"<<endl;
cout<<"元素:";
cin>>e;
cout<<"位置:";
cin>>i;
ListInsert_Sq(L,i,e);
break;
case 4:
if(!L.elem)
{
cout<<"顺序表未初始化,请初始化顺序表!"<<endl<<endl;
break;
}
cout<<"请输入要删除的元素的位置"<<endl;
ListDelete_Sq(L,i,&res);
cout<<"删除的元素为:"<<res<<endl;
break;
case 5:
if(!L.elem)
{
cout<<"顺序表未初始化,请初始化顺序表!"<<endl<<endl;
break;
}
break;
case 6:
if(!L.elem)
{
cout<<"顺序表未初始化,请初始化顺序表!"<<endl<<endl;
break;
}
break;
case 7:
if(!L.elem)
{
cout<<"顺序表未初始化,请初始化顺序表!"<<endl<<endl;
break;
}
break;
case 8:
return 0;
default:
cout<<"ERROR"<<endl;
}
}
return 0;
}