1. 程式人生 > >數據結構第二篇——線性表的順序存儲

數據結構第二篇——線性表的順序存儲

sys 序表 順序 操作 大數 fine 存儲結構 ret ins

?註:未經博主同意,不得轉載。

線性表的順序表示指的是用一組地址連續的存儲單元依次存儲線性表的數據元素。

由於高級程序語言中的數組類型也有隨機存取的特性,因此,通常都用數組來描述數據結構中的書序存儲結構。

如下描述:

 1 const int MaxListSize=100;                //最大數據個數 
 2 //other way
 3 //define MaxListSize  100
 4 
 5 class List{
 6 
 7     private: 
 8     Data data[MaxListSize];          //存放數據 
 9     int size;                     //
數據個數 10 11 public: 12 List(){size=0; } //構造函數 13 //~List(); //析構函數 14 15 void Clear(){size=0;} //清空 16 bool IsEmpty(); //判斷是否為空 17 Data GetElem(int i); //返回第i個元素的值 18 int Locate(Data e); //返回第一個與e匹配的元素的位序 19 Data Prior(Data e); //
返回e的前驅 20 Data Next(Data e); //返回e的後繼 21 void Insert(Data e,int i); //第i個位置插入新元素e 22 Data Delete(int i); //刪除第i個元素,返回該元素 23 int Length(){return size;} //返回長度 24 void print(); //輸出元素 25 }; 26

這些基本操作在順序表中的實現如下:

  1 //插入元素 
  2 void List::Insert(Data e,int
i) 3 { 4 if(i<1||i>size+1||i>=MaxListSize) 5 { 6 cout<<"插入位置錯誤!"<<endl; 7 exit(0); 8 } 9 else 10 { 11 for(int j=size-1;j>=i-1;--j) //將i後的元素整體往後移一個單位 12 data[j+1]=data[j]; 13 14 data[i-1]=e; //將元素e插入 15 ++size; 16 } 17 } 18 19 //刪除元素 20 Data List::Delete(int i) 21 { 22 if(i<1||i>size) 23 { 24 cout<<"刪除位置錯誤!"<<endl; 25 exit(0); 26 } 27 else 28 { 29 Data e=data[i-1]; 30 for(int j=i-1;j<size-1;++j) 31 data[j]=data[j+1]; 32 --size; 33 return e; 34 } 35 } 36 37 //查找與e匹配的第一個元素 38 int List::Locate(Data e) 39 { 40 int i=1; 41 while(i<=size&&data[i-1]!=e) 42 ++i; 43 if(i>size) 44 return 0; 45 else 46 return i; 47 } 48 49 //判斷線性表是否為空 50 bool List::IsEmpty() 51 { 52 if(size==0) 53 return true; 54 else 55 return false; 56 } 57 58 //取元素操作 59 Data List::GetElem(int i) 60 { 61 if(i<1||i>size) 62 { 63 cout<<"位置錯誤!"<<endl; 64 exit(0); 65 } 66 else 67 return data[i-1]; 68 } 69 70 //輸出所有元素 71 void List::print() 72 { 73 if(size==0) 74 { 75 cout<<"空表,無元素!"<<endl; 76 exit(0); 77 } 78 else 79 { 80 for(int i=0;i<size;++i) 81 cout<<data[i]<< ; 82 cout<<endl; 83 } 84 } 85 86 //返回e的前驅 87 Data List::Prior(int e) 88 { 89 for(int i=0;i<size;++i) 90 { 91 if(e==data[i]) 92 { 93 if(i==0) 94 { 95 cout<<"這已是第一個元素"<<endl; 96 return -100; 97 } 98 else 99 return data[i-1]; 100 } 101 } 102 cout<<"表中無此元素"<<endl; 103 return -101; 104 } 105 106 //返回e的後繼 107 Data List::Next(int e) 108 { 109 for(int i=0;i<size;++i) 110 { 111 if(e==data[i]) 112 { 113 if(i==(size-1)) 114 { 115 cout<<"這已是最後一個元素"<<endl; 116 return -100; 117 } 118 else 119 return data[i+1]; 120 } 121 } 122 cout<<"表中無此元素"<<endl; 123 return -101; 124 }

對上述算法的調試則放在main函數裏:

 1 int main()
 2 {
 3     system("color 0A");
 4     int a[8]={3,6,9,12,15,18,21,24};
 5     int i,j;
 6     Data x;                                //    定義元素x 
 7     List list;                            //定義線性表對象
 8     
 9     if(list.IsEmpty())
10     cout<<"當前為空表"<<endl<<endl; 
11     
12     for(i=1;i<=8;++i)                    //將數組a的元素插入線性表中 
13     list.Insert(a[i-1],i);
14     list.print();
15     cout<<"當前線性表長度:"<<list.Length()<<endl;
16     cout<<endl;
17     
18     cout<<"輸入要插入的元素和位置:";
19     cin>>x>>i; 
20     list.Insert(x,i);                //插入元素 
21     list.print(); 
22     cout<<"當前線性表長度:"<<list.Length()<<endl;
23     cout<<endl;
24     
25     cout<<"輸入要插入的元素和位置:";
26     cin>>x>>i; 
27     list.Insert(x,i);                //插入元素 
28     list.print(); 
29     cout<<"當前線性表長度:"<<list.Length()<<endl;
30     cout<<endl;
31     
32     cout<<"輸入要插入的元素和位置:";
33     cin>>x>>i; 
34     list.Insert(x,i);                //插入元素 
35     list.print(); 
36     cout<<"當前線性表長度:"<<list.Length()<<endl;
37     cout<<endl;
38     
39     if(list.IsEmpty())
40     cout<<"當前為空表"<<endl; 
41     else
42     cout<<"當前非空"<<endl<<endl; 
43     
44     cout<<"輸入要查找的元素:";    //查找某一個元素 
45     cin>>x;
46     j=list.Locate(x);
47     if(j)
48     cout<<"查找成功,是第"<<j<<"個元素"<<endl;
49     else
50     cout<<"沒有此元素"<<endl;
51     cout<<endl;
52     
53     cout<<"輸入要查找的元素:";    //查找某一個元素 
54     cin>>x;
55     j=list.Locate(x);
56     if(j)
57     cout<<"查找成功,是第"<<j<<"個元素"<<endl;
58     else
59     cout<<"沒有此元素"<<endl;
60     cout<<endl;
61     
62     cout<<"當前表中元素:/t";
63     list.print();
64     cout<<"輸入想要查找元素的位置:";        //    輸出某個位置的元素 
65     cin>>i;
66     cout<<list.GetElem(i)<<endl;
67     cout<<endl;
68     cout<<"輸入想要查找元素的位置:";        //    輸出某個位置的元素 
69     cin>>i;
70     cout<<list.GetElem(i)<<endl;
71     cout<<endl;
72     
73     cout<<"輸入想要刪除元素的位置:";        //    刪除某個位置的元素 
74     cin>>i;
75     cout<<list.Delete(i)<<endl;
76     list.print(); 
77     cout<<"當前線性表長度:"<<list.Length()<<endl;
78     cout<<endl;
79     cout<<"輸入想要刪除元素的位置:";        //    刪除某個位置的元素 
80     cin>>i;
81     cout<<list.Delete(i)<<endl;
82     list.print();
83     cout<<"當前線性表長度:"<<list.Length()<<endl;
84     cout<<endl;
85     
86     cout<<"輸入想要查找前驅的元素:";        //    查找某元素前驅 
87     cin>>i;
88     if(list.Prior(i)>-100)
89     cout<<list.Prior(i)<<endl;
90     
91     cout<<"輸入想要查找後繼的元素:";        //    查找某元素後繼 
92     cin>>i;
93     if(list.Next(i)>-100)
94     cout<<list.Next(i)<<endl;
95     
96     return 0;
97 } 

最終的實現效果如下:

技術分享

技術分享

數據結構第二篇——線性表的順序存儲