1. 程式人生 > >數據結構-線性表順序存儲(c++)

數據結構-線性表順序存儲(c++)

構造 locate esp str 狀態碼 using list() 順序 頭文件

數據結構-線性表順序存儲(c++) 2018-09-06

List.h //頭文件

 1 #define OK 1
 2 #define ERRO0R 0
 3 #define TRUE 1
 4 #define FALSE 0
 5 #define MAXSIZE 20  //存儲空間初始分配量
 6 
 7 typedef int Status;  //Status 函數結果狀態碼
 8 typedef int ElemType;  //ElemType 據具體情況而定
 9 
10 class SqList
11 {
12 public:
13     SqList();  //構造函數
14     ~SqList();  //
析構函數 15 ElemType m_data[MAXSIZE]; //線性表數據 16 int m_nLength; //線性表長度 17 Status Visit(ElemType c); //輸出c 18 Status InitList(); //初始化線性表長度 19 Status ListEmpty(); //判斷線性表是否為空 20 Status ClearList(); //設置線性表長度為0 21 int ListLength(); //線性表長度 22 Status GetElem(int i,ElemType *e); //取i位子元素到e
23 int LocateElem(ElemType e); //元素e的位子 24 Status ListInsert(int i,ElemType e); //在i處插入e 25 Status ListDelete(int i,ElemType *e); //刪除i處元素,元素賦值到e 26 Status ListTraverse(); //遍歷輸出鏈表 27 void UnionL(SqList Lb); //鏈表並集 28 }

List.h //源文件

  1 #include <iostream>
  2 #include "List.h
" 3 4 using namespace std; 5 SqList::SqList() 6 { 7 m_nLength=0; 8 } 9 10 SqList::~SqList() 11 { 12 13 } 14 15 Status SqList::Visit(ElemType c) 16 { 17 cout<<c<<endl; 18 return OK; 19 } 20 21 Status SqList::InitList() 22 { 23 m_nLength=0; 24 return OK; 25 } 26 27 Status SqList::ListEmpty() 28 { 29 if(m_nLength==0) 30 return TRUE; 31 else 32 return FALSE; 33 } 34 35 Status SqList::ClearList() 36 { 37 m_nLength=0; 38 return OK; 39 } 40 41 Status SqList::ListLength() 42 { 43 return m_nLength; 44 } 45 46 Status SqList::GetElem(int i,ElemType *e) 47 { 48 if(m_nLength==0||i<1||i>m_nLength) 49 return FALSE; 50 *e=m_data[i-1]; 51 return OK; 52 } 53 54 Status SqList::LocateElem(ElemType e) 55 { 56 int i; 57 if(m_nLength==0) 58 return 0; 59 for(i=0;i<m_nLength;i++) 60 { 61 if(m_data[i]==e) 62 break; 63 } 64 if(i>=m_nLength) 65 return 0; 66 return i+1; 67 } 68 69 Status SqList::ListInsert(int i,ElemType e) 70 { 71 int k; 72 if(m_nLength==MAXSIZE) 73 return FALSE; 74 if(i<1||i>m_nLength+1) 75 return FALSE; 76 if(i<=m_nLength) 77 { 78 for(k=m_nLength-1;k>=i-1;k--) 79 m_data[k+1]=m_data[k]; 80 } 81 m_data[i-1]=e; 82 m_nLength++; 83 return OK; 84 } 85 86 Status SqList::ListDelete(int i,ElemType *e) 87 { 88 int k; 89 if(m_nLength==0) 90 return FALSE; 91 if(i<1||i>m_nLength) 92 return FALSE; 93 *e=m_data[i-1]; 94 if(i<m_nLength) 95 { 96 for(k=i;i<m_nLength;k++) 97 m_data[k-1]=m_data[k]; 98 } 99 m_nLength--; 100 return OK; 101 } 102 103 Status SqList::ListTraverse() 104 { 105 int i; 106 for(i=0;i<m_nLength;i++) 107 Visit(m_data[i]); 108 cout<<"\n"<<endl; 109 return OK; 110 } 111 112 void SqList::UnionL(SqList Lb) 113 { 114 int La_len,Lb_len,i; 115 ElemType e; 116 La_len=ListLength(); 117 Lb_len=Lb.ListLength(); 118 for(i=1;i<=Lb_len;i++) 119 { 120 Lb.GetElem(i,&e); 121 if(!LocateElem(e)) 122 ListInsert(++La_len,e); 123 } 124 }


實例:

 1 #include "List.h"
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 void main()
 7 {
 8     SqList L;
 9     ElemType e;
10     Status i;
11     int j,k;
12     i=L.InitList();
13     cout<<"初始化L後:L.length="<<L.m_nLength<<endl;
14     for(j=1;j<5;j++)
15         i=L.ListInsert(1,j);
16     cout<<"在L的表頭依次插入 1-5後:L.m_data="<<endl;
17     L.ListTraverse();
18     cout<<L.m_nLength<<endl;
19     i=L.ListEmpty();
20     cout<<"L是否空 i="<<i<<"(1:是 0:否)"<<endl;
21     i=L.ClearList();
22     cout<<"清空L後L.m_length="<<L.m_nLength<<endl;
23     L.ListEmpty();
24     for(j=1;j<=5;j++)
25         i=L.ListInsert(j,j);
26     cout<<"在L的表尾依次插入 1-10後:L.m_data="<<endl;
27     L.ListTraverse();
28     L.GetElem(5,&e);
29     SqList Lb;
30     for(j=0;j<10;j++)
31         i=Lb.ListInsert(1,j);
32     cout<<"在L的表頭依次插入 1-15後:L.m_data="<<endl;
33     Lb.ListTraverse();
34     L.UnionL(Lb);
35     L.ListTraverse();
36     system("pause");
37 }

數據結構-線性表順序存儲(c++)