順序表
順序表中基本操作的實現,包括初始化順序表,建表,順序表的輸出、查找、插入及刪除操作
該實驗約定表中的元素類型爲整型。
請自行在此實驗的基礎上補充順序表其它基本操作的實現(例如:ClearList、PutElem、DestroyList、PriorELem、NextElem、GetElem等)
#include <iostream>
#include <cstdlib>
#include <ctime>
#define MAXSIZE 100 //順序表初始化時分配的存儲空間長度
#define OVER -2
using namespace std;
typedef struct{
int *elem;
int length;
}Sqlist; //順序表結構
//初始化順序表L
void InitList(Sqlist &L)
{
//請同學們在此處把該操作補充完整,並調試
L.elem=new int[MAXSIZE];
if(!L.elem) exit(OVER);
L.length=0;
}
//向順序表L中填充數據,本例中使用[1,100]內的隨機整數填充順序表L,參數n表示要向表中填充幾個數據
void CreateList(Sqlist &L,int n)
{
int i;
srand(time(NULL)); //設置隨機數種子
for(i=0;i<n;i++) //向順序表L中填充n個[1,100]內的隨機整數
{
L.elem[i]=rand()%100+1;
}
L.length =n; //修改順序表L的長度
}
//輸出順序表L 中的元素
void PrintList(Sqlist L)
{
int i;
for(i=0;i<L.length ;i++)
{
cout<<L.elem[i]<<" ";
}
cout<<endl;
}
//在順序表L 中查找元素e的位置,查找成功,返回e在L中的位序,否則返回0
int LocateElem(Sqlist L,int e)
{
//請同學們在此處把該操作補充完整,並調試
for(int i=0;i<MAXSIZE;i++){
if(L.elem[i]==e) return i;
}
return 0;
}
//在順序表L的第i個元素前插入一個新元素e,插入成功,返回true,插入失敗,返回false
bool ListInsert(Sqlist &L,int i,int e)
{
//請同學們在此處把該操作補充完整,並調試
for(int m=L.length;m>i;m--){
L.elem[m]=L.elem[m-1];
}
L.elem[i]=e;
L.length++;
return true;
}
//刪除順序表L中第i 個位置處的元素,被刪元素用參數e返回,刪除成功,函數返回true,刪除失敗,函數返回false
bool ListDelete(Sqlist &L,int i,int &e)
{
//請同學們在此處把該操作補充完整,並調試
e=L.elem[i];
for(int m=i+1;m<L.length;m++){
L.elem[m-1]=L.elem[m];
}
L.length--;
return true;
}
int main()
{
Sqlist L; //定義順序表變量L
int n,e,pos,flag;
InitList(L); //初始化順序表L
cout<<"順序表L初始化成功"<<endl;
cout<<"請輸入要創建的順序表的長度"<<endl;
cin>>n; //變量n表示要輸入到順序表中的元素個數
CreateList(L,n); //向L中填充n個隨機整數
cout<<"順序表L創建成功,表中元素爲:"<<endl;
PrintList(L); //輸出順序表L中的元素
cout<<"請輸入要查找的元素的值"<<endl;
cin>>e; //變量e表示要查找的元素
pos=LocateElem(L,e); //在L中查找元素e的位置,pos表示查找結果,或爲0,或爲非零值
cout<<"元素"<<e<<"在表中的位序爲"<<pos<<endl;
cout<<"請輸入插入位置及插入的值"<<endl;
cin>>pos>>e; //輸入向L中插入元素的位置及值
flag=ListInsert(L,pos,e); //在順序表L的第pos個位置插入值e,flag表示插入操作結果,或爲true,或爲false
if(flag) //判斷插入操作是否成功
{
cout<<"插入操作成功,表中元素爲:"<<endl;
PrintList(L); //輸出插入操作成功後,L中的值
}
else
cout<<"插入失敗!"<<endl;
cout<<"請輸入刪除元素的位置"<<endl;
cin>>pos; //輸入刪除位置
flag=ListDelete(L,pos,e); //刪除順序表L中第pos個元素,被刪元素的值用e返回,flag表示刪除操作結果,或爲true,或爲false
if(flag) //判斷刪除操作是否成功
{
cout<<"刪除操作成功,被刪元素爲:"<<e<<"表中元素爲:"<<endl; //輸出被刪元素的值
PrintList(L); //輸出刪除操作成功後,L中的值
}
else
cout<<"刪除位置錯誤,操作失敗!"<<endl;
return 0;
}
單鏈表
單鏈表基本操作的實現,包括單鏈表的初始化操作,前(後)插法建表,取值,單鏈表的查找,單鏈表的插入與刪除操作(約定單鏈表中存儲的是整型數據)。
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList; //單鏈表表結構
LNode *p;
//初始化單鏈表L
void InitList(LinkList &L)
{
//請同學們在此處把該操作補充完整
L=new LNode;
L->next=NULL;
}
//前插法建表,要求使用[1,100]內的隨機整數填充單鏈表L,參數n表示要向表中填充幾個數據
//提示:生成[1,100]內的隨機整數公式爲: rand()%100+1 ,在使用rand()函數前要先使用srand(time(0))設置隨機數種子
void CreateListF(LinkList &L,int n)
{
//請同學們在此處把該操作補充完整,然後再main裏面調用並調試
srand(time(0));
L->next=NULL;
for(int i=0;i<n;i++){
p=new LNode;
p->data=rand()%100+1;
p->next=L->next;
L->next=p;
}
}
//選做
//後插法建表,要求使用[1,100]內的隨機整數填充單鏈表L,參數n表示要向表中填充幾個數據
void CreateListR(LinkList &L,int n)
{
//請同學們在此處把該操作補充完整,然後再main裏面調用並調試
srand(time(0));
p=L;
for(int i=0;i<n;i++){
p->next=new LNode;
p=p->next;
p->data=rand()%100+1;
p->next=NULL;
}
}
//輸出單鏈表L 中各結點的值
void PrintList(LinkList L)
{
//請同學們在此處把該操作補充完整,並調試
p=L->next;
while(p){
cout<<p->data<<' ';
p=p->next;
}
cout<<'\b'<<endl;
}
//在單鏈表L 中查找元素e的位置,查找成功,返回指向e的指針
LNode *LocateElem(LinkList L,int e)
{
LNode *x,*s;
//請同學們在此處把該操作補充完整,並調試
x=NULL;
s=L->next;
while(s){
if(s->data==e){
x=s;
break;
}
s=s->next;
}
return x;
}
//在單鏈表L的第i個結點前插入一個新元素e,插入成功,返回true,插入失敗,返回false
bool ListInsert(LinkList &L,int i,int e)
{
//請同學們在此處把該操作補充完整,並調試
p=L;
for(int x=0;x<i;x++){
p=p->next;
}
LNode *s=new LNode;
s->next=p->next;
s->data=e;
p->next=s;
return true;
}
//刪除單鏈表L中第i 個結點,被刪元素用參數e返回,刪除成功,函數返回true,刪除失敗,函數返回false
bool ListDelete(LinkList &L,int i,int &e)
{
//請同學們在此處把該操作補充完整,並調試
p=L;
for(int x=0;x<i;x++){
p=p->next;
}
LNode *s=p->next;
e=s->data;
p->next=s->next;
delete s;
return true;
}
int main()
{
LinkList L; //定義單鏈表的頭指針L
int n,e,pos,flag;
InitList(L); //初始化單鏈表L
cout<<"單鏈表L初始化成功"<<endl;
cout<<"請輸入要創建的單鏈表的長度"<<endl;
cin>>n; //變量n表示要輸入到單鏈表中的元素個數
CreateListF(L,n); //向L中填充n個隨機整數
cout<<"單鏈表L創建成功,表中元素爲:"<<endl;
PrintList(L); //輸出順序表L中的元素
cout<<"請輸入要查找的元素的值"<<endl;
cin>>e; //變量e表示要查找的元素
p=LocateElem(L,e); //在L中查找元素e的位置,p表示查找結果,或爲NULL,或爲非NULL
if(p) cout<<"查找成功"<<endl;
else cout<<"表中沒有值爲"<<e<<"的結點"<<endl;
cout<<"請輸入插入位置及插入的值"<<endl;
cin>>pos>>e; //輸入向L中插入元素的位置及值
flag=ListInsert(L,pos,e); //在單鏈表L的第pos個位置插入值e,flag表示插入操作結果,或爲true,或爲false
if(flag) //判斷插入操作是否成功
{
cout<<"插入操作成功,表中元素爲:"<<endl;
PrintList(L); //輸出插入操作成功後,L中的值
}
else
cout<<"插入失敗!"<<endl;
cout<<"請輸入刪除元素的位置"<<endl;
cin>>pos; //輸入刪除位置
flag=ListDelete(L,pos,e); //刪除單鏈表L中第pos個元素,被刪元素的值用e返回,flag表示刪除操作結果,或爲true,或爲false
if(flag) //判斷刪除操作是否成功
{
cout<<"刪除操作成功,被刪元素爲:"<<e<<"表中元素爲:"<<endl; //輸出被刪元素的值
PrintList(L); //輸出刪除操作成功後,L中的值
}
else
cout<<"刪除位置錯誤,操作失敗!"<<endl;
return 0;
}