學習了數據結構後對線性表有了更好的瞭解,所謂線性表有兩種實現方法,一種是用順序結構,一種是用鏈式結構。具體說來就是用動態數組與鏈表的方式來實現。
這便是數據結構的兩種儲存方式,順序存儲結構與鏈式存儲結構,同時也是數據結構研究的存儲結構的具體體現,因此線性表的實現對於初步學習數據結構的我們有着重要的意,這裏我們用數組實現順序線性表。
#include <iostream>
#include <stdlib.h>
#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int status;
typedef int Elemtype;
using namespace std;
typedef struct
{
Elemtype *elem; //線性表基址
int length; //線性表當前長度
int listsize; //線性表分配的存儲容量
}Sqlist;
int InitList(Sqlist &L);
int DestoryList(Sqlist &L);
int ClearList(Sqlist &L);
int ListEmpty(Sqlist L);
int ListLength(Sqlist L);
int Getelem(Sqlist L,int i,status &e);
int LocateElem(Sqlist L,status e);
int ListInsert(Sqlist &L,int i,status e);
int ListDelete(Sqlist &L,int i,status &e);
int ListTraverse(Sqlist &L);
void MergeList(Sqlist La,Sqlist Lb,Sqlist &Lc);
//排序
int Partition(Elemtype *a,int low,int high);
void QuickSort(Elemtype *a,int low,int high);
int main()
{
int a[10]={0,1,5,11,2,6,7,88,6,55};
int e;
Sqlist L;
InitList(L);
L.elem=a;
L.length=10;
L.listsize=LIST_INIT_SIZE;
cout<<"輸出第五位數字"<<endl;
if(!Getelem(L,5,e))
cout<<"找數字出錯"<<endl;
else
cout<<"第五位數字是"<<e<<endl;
cout<<"排序前:";
for(int i=0;i<10;i++)
{
cout<<a[i]<<" ";
}
QuickSort(a,0,9);
cout<<"\n"<<"排序後:";
for(int i=0;i<10;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
system("PAUSE");
return 0;
}
//初始化線性表
int InitList(Sqlist &L)
{
//構造一個空的線性表
L.elem=(Elemtype *)new Elemtype[LIST_INIT_SIZE*sizeof(Elemtype)];
if(!L.elem)
exit(OVERFLOW);
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
//銷燬線性表
int DestoryList(Sqlist &L)
{
if(L.elem)
delete[] L.elem;
L.length=0;
L.listsize=0;
return OK;
}
//清空線性表
int ClearList(Sqlist &L)
{
if(L.elem)
{
for(int i=0;i<L.length;i++)
{
L.elem[i]=0;
}
L.length=0;
return OK;
}
}
//判斷線性表是否爲空
int ListEmpty(Sqlist L)
{
if(L.elem)
return 0;
else
return 1;
}
//返回線性表中元素個數
int ListLength(Sqlist L)
{
if(!L.elem)
return ERROR;
else
return L.length;
}
//搜索第i個元素
int Getelem(Sqlist L,int i,status &e)
{
if(i<1||i>L.length)
return ERROR;
e=L.elem[i-1];
return OK;
}
//返回L中第一個與e相等的元素
int LocateElem(Sqlist L,status e)
{
int i;
for(i=0;i<L.length;i++)
{
if(e=L.elem[i])
{
cout<<"e在線性表中的第"<<i+1<<"位"<<endl;
return OK;
}
}
if(i==L.length)
{
cout<<"沒有這個數據\n";
return 0;
}
}
//往線性表插入元素
int ListInsert(Sqlist &L,int i,status e)
{
if(i<1||i>(L.length+1))
{
cout<<"輸入i錯誤"<<endl;
return ERROR;
}
if(L.length>=L.listsize)
{
Elemtype *newbase=(Elemtype *)realloc(L.elem,(L.listsize+LIST_INCREMENT)*sizeof(Elemtype));
if(!newbase)
exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LIST_INCREMENT;
}
Elemtype *q,*p;
q=&(L.elem[i-1]); //q爲插入位置
for(p=&(L.elem[L.length-1]);p>=q;p++)
{
*(p+1)=*p;
}
*q=e;
++L.length;
return OK;
}
//在線性表刪除一個元素
int ListDelete(Sqlist &L,int i,status &e)
{
if(i<1||i>L.length)
{
cout<<"輸入錯誤"<<endl;
exit(OVERFLOW);
}
Elemtype *p,*q;
p=L.elem+i-1;
e=*p;
q=L.elem+L.length-1;
for(;p<=q;p++)
{
*p=*(p+1);
}
--L.length;
return OK;
}
//反轉線性表
int ListTraverse(Sqlist &L)
{
Elemtype temp;
Elemtype *low,*high;
low=L.elem;
high=L.elem+L.length-1;
while(low!=high)
{
temp=*low;
*low=*high;
*high=temp;
low++;high++;
}
return OK;
}
//合併兩個線性表
void MergeList(Sqlist La,Sqlist Lb,Sqlist &Lc)
{
InitList(Lc);
int a,b;
a=0,b=0;
int i,j,k;
i=j=1;
k=0;
while(i<=La.length&&j<=Lb.length)
{
Getelem(La,i,a);
Getelem(Lb,j,b);
if(a<=b)
{
ListInsert(Lc,++k,a);
++j;
}
else
{
ListInsert(Lc,++k,b);
++j;
}
}
while(i<=La.length)
{
Getelem(La,i++,a);
ListInsert(Lc,++k,a);
}
while(j<=Lb.length)
{
Getelem(Lb,j++,b);
ListInsert(Lc,++k,b);
}
}
//找出樞軸位置
int Partition(Elemtype *a,int low,int high)
{
while(low<high)
{
while(low<high&&a[high]>=a[low])
{
high--;
}
Elemtype temp=a[low];
a[low]=a[high];
a[high]=temp;
while(low<high&&a[low]<=a[high])
{
low++;
}
temp=a[low];
a[low]=a[high];
a[high]=temp;
}
return low;
}
//快速排序實現
void QuickSort(Elemtype *a,int low,int high)
{
if(low<high)
{
int pivotloc=Partition(a,low,high);
QuickSort(a,low,pivotloc-1);
QuickSort(a,pivotloc+1,high);
}
}