線性表實驗

順序表

#include<iostream>  
using namespace std;  
const int Maxsize = 30;  
class SeqList  
{  
public:  
    SeqList(){ length = 0; }  
    SeqList(int a[], int n);  
    ~SeqList(){}  
    void Insert(int i, int x);  
    int Delete(int i);  
    int Locate(int x);  
    void PrintList();  
private:  
    int data[Maxsize];  
    int length;  
};  
  
SeqList::SeqList(int a[], int n)  
{  
    if (n > Maxsize) throw "參數非法";  
    for(int i=0;i<n;i++)  
    data[i] = a[i];  
    length = n;  
}  
void SeqList::Insert(int i, int x)  
{  
    if (length >= Maxsize) throw "上溢";  
    if (i<1 || i>length + 1) throw"位置非法";  
    for (int j = length; j >= i; j--)  
        data[j] = data[j - 1];  
    data[i - 1] = x;  
    length++;  
}  
int SeqList::Delete(int i)  
{  
    if (length == 0) throw"下溢";  
    if (i<1 || i>length) throw "位置非法";  
    int x = data[i - 1];  
    for (int j = i; j < length; j++)  
        data[j - 1] = data[j];  
    length--;  
    return x;  
}  
int SeqList::Locate(int x)  
{  
    for (int i = 0; i<length; i++)  
    if (data[i] == x) return i + 1;  
    return 0;  
}  
void SeqList::PrintList()  
{  
    for (int i = 0; i < length; i++)  
        cout << data[i] << " ";  
    cout << endl;  
}  
  
void main(){  
    int a[5] ={ 1, 2, 3, 4, 5};  
    SeqList A(a, 5);  
    cout << "執行插入前的數據爲:" << endl;  
    A.PrintList();  
    
    A.Insert(2, 9);   
    cout << "執行插入操作後的數據爲:" << endl;  
    A.PrintList();  
    cout << "值爲9的元素的位置爲:";  
    cout <<A.Locate(9) << endl;  
    cout << "執行刪除第一個元素操作,刪除前的數據爲:" << endl;  
    A.PrintList();   
    A.Delete(1);  
    cout << "刪除後的數據爲:" << endl;  
    A.PrintList();  
  
}  
   


2單鏈表

#include<iostream>         
using namespace std;    
    
template<class T>    
struct Node       
{     
    T data;  
    Node<T>* next;    
};      
    
     
template<class T>     
class LinkList     
{    
public:      
    LinkList ();   
    LinkList(T a[],int n);      
    ~LinkList();   
    void Insert (int i,T x);      
    int Locate(T x) ;    
    T Delete(int i);      
    void PrintList();      
private:  
    Node<T>* first;  
    
};      
    
     
template<class T>    
LinkList<T>::LinkList()    
{     
    first = new Node<T>;  
    first->next=NULL;  
}     
  
template<class T>  
LinkList<T>::LinkList(T a[],int n)  
{  
Node <T>  *r,*s;  
first = new Node<T>;  
r = first;  
for(int i=0;i<n;i++)  
{  
s = new Node<T>;  
s->data=a[i];  
r->next =s;  
r=s;  
}  
r->next= NULL;  
}  
  
template<class T>  
LinkList<T>::~LinkList()  
{  
Node<T> *q = NULL;  
while(first != NULL)  
{  
q = first;  
first= first->next;  
delete q;  
}  
}  
template<class T>  
void LinkList<T>::Insert(int i,T x)  
{  
Node<T> *p = first,*s=NULL;  
int count = 0;  
while(p!=NULL&&count< i-1)  
{  
p = p->next;  
count++;  
}  
if(p==NULL) throw "位置非法";  
else  
{  
s = new Node<T>;  
s->data =x;  
s->next=p->next;  
p->next= s;  
}  
}  
  
template<class T>  
T LinkList<T>::Delete(int i)  
{  
Node<T> *p =first,*q=NULL;  
T x;  
int count = 0;  
while(p!=NULL && count<i-1)  
{  
p = p->next;  
count++;  
}  
if(p==NULL ||p->next==NULL) throw"位置";  
else  
{  
q=p->next;  
x=q->data;  
p->next=q->next;  
return x;  
}  
}  
  
template<class T>  
int LinkList<T>::Locate(T x)  
{  
Node<T>*p =first->next;  
int count =1;  
while(p!=NULL)  
{  
if(p->data==x) return count;  
p=p->next;  
count++;  
  
}  
return 0;  
}  
  
template<class T>  
void LinkList<T>::PrintList()  
{  
Node<T>*p =first-> next;  
while(p!=NULL)  
{  
cout<<p->data<<" ";  
p=p->next;  
}  
cout<<endl;  
}  
  
void main(){  
    int a[5] = {1, 2, 3, 4, 5};  
    LinkList<int> A(a, 5);  
    cout << "執行插入前的數據爲:" << endl;  
   A.PrintList();    
   A.Insert(2, 9);  
    cout << "執行插入操作後的數據爲:" << endl;  
    A.PrintList();  
    cout << "值爲9的元素的位置爲:";  
    cout <<A.Locate(9) << endl;  
    cout << "執行刪除第一個元素操作,刪除前的數據爲:" << endl;  
    A.PrintList();  
      
    A.Delete(1);  
    cout << "刪除後的數據爲:" << endl;  
   A.PrintList();  
  
}  






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章