?? sqlist.cpp
字號:
#include "iostream.h"
#include "stdlib.h"
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 2
typedef int ElemType;
//順序表結構
typedef struct {
ElemType *elem;
int length;
int listsize;
}SL;
//順序表初始化
void listinit(SL &L)
{L.elem=new ElemType[LIST_INIT_SIZE];
L.length=0;
L.listsize=LIST_INIT_SIZE;
}
//建立順序表
void listcreate(SL &L)
{ElemType x;
int k=0;
cout<<"enter x, 0 to end"<<endl;
cin>>x;
while(x)
{L.elem[k++]=x;
cin>>x;}
L.length=k;
}
//順序表輸出
void print(SL &L)
{int i;
for(i=0;i<L.length;i++)
cout<<L.elem[i]<<" ";
cout<<endl;
}
//順序表置空
void listnull(SL &L)
{ L.length=0;
}
//求順序表長度
int listlen(SL L)
{return L.length;
}
//判斷順序表是否為空
int listempty(SL L)
{ if(L.length==0) return 1;
else return 0;
}
//銷毀順序表
void listdestory(SL &L)
{L.length=0;
L.listsize=0;
delete []L.elem;
}
//求順序表中第i個元素
void getlist(SL L,int i,ElemType &e)
{ if(listempty(L)) e=L.elem[i-1];
else e=-1;
}
//求順序表中元素e的直接前驅
void prior(SL L,ElemType e,ElemType &pre)
{int i;
for(i=0;i<L.length;i++)
if(L.elem[i]==e) break;
if((i!=0)&&(i<=L.length-1))pre=L.elem[i-1];
else cout<<"無前驅或元素不存在!"<<endl;
}
//求順序表中元素e的直接后繼
void next(SL L,ElemType e,ElemType &net)
{int i;
for(i=0;i<L.length;i++)
if(L.elem[i]==e) break;
if(i<L.length-1)net=L.elem[i+1];
else cout<<"無后驅或元素不存在!"<<endl;
}
//在順序表的第i個位置插入元素x;
void listinsert(SL &L,int i,ElemType x)
{int j;ElemType *newbase;
if(L.length>=L.listsize)
newbase =(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase) exit(0);
L.elem=newbase;
L.listsize=L.listsize+LISTINCREMENT;
if(i<0||i>=L.length) cout<<"插入位置錯誤"<<endl;
else
{
for(j=L.length;j>i-1;j--)
L.elem[j]=L.elem[j-1];
L.elem[j]=x;
L.length++;
}
}
//在順序表中元素e的后面插入元素y
void listafterinsert(SL &L,ElemType e,ElemType y)
{
int i,j;
for(i=0;i<L.length;i++) if(L.elem[i]==e) break;
if(i>=L.length) cout<<"元素"<<e<<"不存在,插入失敗!"<<endl;
else
{for(j=L.length;j>i+1;j--)
L.elem[j]=L.elem[j-1];
L.elem[j]=y;
L.length++;
}
}
//在順序表中元素e的前面插入元素y
void listbeforeinsert(SL &L,ElemType e,ElemType y)
{
int i,j;
for(i=0;i<L.length;i++) if(L.elem[i]==e) break;
if(i>=L.length) cout<<"元素"<<e<<"不存在,插入失敗!"<<endl;
else
{for(j=L.length;j>i;j--)
L.elem[j]=L.elem[j-1];
L.elem[j]=y;
L.length++;
}
}
//刪除順序表中第i個元素
void listdelete(SL &L,int i,ElemType &e)
{int j;
if(i<0||i>=L.length) cout<<"刪除位置錯誤"<<endl;
else
{e=L.elem[i-1];
for(j=i-1;j<L.length-1;j++)
L.elem[j]=L.elem[j+1];
L.length--;
}
}
//刪除順序表中元素e
void listlocatdelete(SL &L,ElemType e)
{int j,i;
for(i=0;i<L.length;i++) if(L.elem[i]==e) break;
if(i>=L.length) cout<<"被刪除元素不存,刪除失敗"<<endl;
else
{
for(j=i;j<L.length-1;j++)
L.elem[j]=L.elem[j+1];
L.length--;
}
}
//刪除順序表中元素e的直接前驅
void listpriordelete(SL &L,ElemType e)
{int j,i;
for(i=0;i<L.length;i++) if(L.elem[i]==e) break;
if(i==0||i>=L.length) cout<<"無前驅或被刪除元素不存,刪除失敗"<<endl;
else
{
for(j=i-1;j<L.length-1;j++)
L.elem[j]=L.elem[j+1];
L.length--;
}
}
//刪除順序表中元素e的直接后驅
void listnextdelete(SL &L,ElemType e)
{int j,i;
for(i=0;i<L.length;i++) if(L.elem[i]==e) break;
if(i>=L.length-1) cout<<"無后驅或被刪除元素不存,刪除失敗"<<endl;
else
{
for(j=i+1;j<L.length-1;j++)
L.elem[j]=L.elem[j+1];
L.length--;
}
}
//將順序表LA逆置并存放到順序表LB中。即如果LA={a1,a2,..,an}則LB={an,…,a2,a1}
void invers(SL LA,SL &LB)
{
int i;
if(LA.length==0) cout<<"空表,不能置逆"<<endl;
else
{for(i=LA.length-1;i>=0;i--) LB.elem[LA.length-i-1]=LA.elem[i];
LB.length =LA.length;}
}
//順序表L就地逆置,即利用原順序表的存儲單元,把L={a0,a1,…,an-1}逆置為{an-1,an-2,…,a1,a0}
void inversyuan(SL &LA)
{
int i,k,n;
if(LA.length==0) cout<<"空表,不能置逆"<<endl;
else
{ n=LA.length/2;
for(i=0;i<n;i++)
{k=LA.elem [i];LA.elem [i]=LA.elem [LA.length-i-1];LA.elem [LA.length-i-1]=k;}
}
}
void main()
{ ElemType y=100;
SL L1,L2;
listinit(L1);
listcreate(L1);
print(L1);
listinsert(L1,3,y);
print(L1);
/*listnull(L1);
print(L1);
listdestory(L1);
//
// listbeforeinsert(L1,45,212);
next(L1,34,pr);
cout<<pr<<endl;
//print(L1);
ElemType pr=0;
listnextdelete(L1,34);
print(L1);
listinit(L2);
invers(L1,L2);
print(L2);
inversyuan(L1);
print(L1);*/
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -