資料結構————線性表
(一)順序表
1.類宣告:
template class linearlist ; // 建構函式,最大長度預設為10
~linearlist () // 析構函式
bool isempty () const // 判斷表是否為空
bool isfull () const // 判斷表是否為滿
int length () const // 返回表的長度
bool find ( int k, t & item ) const ; //訪問:將下標為k的結點的字段值賦給item
int search ( const t & item ) const ; //查詢:在表中查詢字段值為item的結點並返回其下標
void delete ( int k, t & item ) ; // 刪除:刪除下標為k的結點並將其字段值賦給item
void insert ( int k, const t & item ) ; // 插入:在下標為k的結點後插入字段值為item的結點
} ;
2.插入演算法:(時間複雜性o(n))
// 插入: 在第k個結點後插入字段值為item的結點
void insert(int k, const t& item)
;
3.查詢演算法:
2.類定義://查詢: 在表中查詢字段值為item的結點並返回其所在位置; 若表中沒有item, 則返回0
int search(const t &item) const
; slnode(const t &item, slnode* nextnode=null)
};
template < class t >
class sllist;
// 建構函式,構造乙個只有哨位結點的空表
sllist ( t & item ) ;// 建構函式
~sllist ( void ) ; // 析構函式
bool isempty ( void ) const ; // 判斷鍊錶是否為空
int length ( void ) const ; // 返回表的長度
bool find ( int k, t & item ) const ; /*訪問:將鍊錶中第k個結點的字段值賦給item*/
int search ( const t & item ) const ; /*查詢:在鍊錶中查詢字段值為item的結點並返回其表中位置*/
void delete ( int k, t & item ) ; /* 刪除:刪除鍊錶中第k個結點並將其字段值賦給item*/
void insert ( int k, const t & item ) ; /* 插入:在鍊錶中第k個結點後插入字段值為item的結點*/
};
3.查詢演算法: 最好情況時間複雜度為o(1),最壞情況和平均情況下時間複雜度為o(n)
4.刪除演算法:// 令當前指標指向鍊錶中第k個結點,並將該結點值data返回給item
bool find(int k, t &item)
currptr=head;
for(int i=1; i<=k; i++)
currptr=currptr->next;
item=currptr->data;
return true;
};
5.插入演算法:// 刪除當前結點的後繼結點
bool delete (t &de_item)
slnode*temp=currptr->next;
currptr->next=temp->next;
size--;
de_item=temp->data;
// 考察被刪除結點是否為原表尾
if(temp==tail)
tail=currptr;
delete temp;
return true ;
};
(三)迴圈列表// 插入操作:在指定位置插入乙個data域值為item的結點
// 在當前結點後插入
void insert(const t &item)
; // 在表尾插入結點
void insertfromtail(const t &item)
; // 在哨位結點後插入
void insertfromhead(const t &item)
else
head->next=new slnode(item, head->next);
size++;
};
1.2. 判斷空表的的條件: next(head) == head
(四)雙向鍊錶
1.定義:指煉表中任一結點p都是由data域、左指標域left和右指標域right構成的,左指標域和右指標域分別存放p的左右兩邊相鄰結點的位址資訊,鍊錶中表頭結點的left指標和表尾結點的right指標均為null. 指標head和tail分別指向雙向鍊錶的頭結點和尾結點,雙向鍊錶也被稱為雙重鍊錶。
2.結構定義:
3.插入操作:雙向鍊錶結點(double-linked list node)dlnode的結構定義
template < class t >
struct dlnode ; // 建構函式
dlnode ( const t& item, slnode * l = null, slnode * r = null )
// 建構函式
};
4.刪除節點:// 在當前結點後插入
void insert(const t& item)
dlnode *p=new dlnode(item, currptr, currptr->right);
currptr->right->left=p;
currptr->right=p;
size++;
// 若currptr是表尾,令表尾指標指向新插入結點
if(currptr==tail)
tail=p;
}; // 在表尾插入結點
void insertfromtail(const t &item)
; // 在哨位結點後插入
void insertfromhead(const t &item)
dlnode *p=new dlnode(item, head, head->right);
head->right->left=p;
head->right=p;
size++;
};
// 刪除操作:刪除指定結點並將其data值返回給變數de_item
// 刪除哨位結點後的第乙個真正表結點
bool deletefromhead(t &de_item)
dlnode*temp=head->right;
head->right=temp->right;
size--;
de_item=temp->data;
// 若原表中除了哨位結點外只有乙個表結點
if(temp==tail)
tail=head;
delete temp;
return true;
}; // 刪除表尾結點
bool deletefromtail(t &de_item)
setend();
// 令當前指標指向表尾結點的前趨結點
prev();
de_item=tail->data;
currptr->right=null;
size--;
delete tail;
tail=currptr;
return true;
}; //刪除當前結點的右結點
bool delete(t &de_item)
;
資料結構(線性表)
1.試寫一演算法,在無頭結點的動態單鏈表上實現線性表操作insert l,i,b 並和在帶頭結點的動態單鏈表上實現相同操作的演算法進行比較。status insert linklist l,int i,int b 在無頭結點鍊錶l的第 i個元素之前插入元素 belse insert 2.已知線性表中...
資料結構 線性表
參考 一 線性表 順序表 單鏈表 迴圈鍊錶 雙鏈表 順序表 1.表的初始化 void initlist seqlist l 2.求表長 int listlength seqlist l 3.取表中第i個結點 datatype getnode l,i 4.查詢值為x的結點 5.插入 具體演算法描述 v...
資料結構 線性表
線性表是最基礎的一種資料結構,這樣的資料物件包含的資料元素具有一對一的前驅後繼關係。按其邏輯儲存方式的不同可分為兩類線性表 順序表和鏈式表。其中鏈式表又可分為線性鍊錶 迴圈鍊錶和雙向鍊錶。下面分別介紹下這幾種線性表的資料結構 1.順序表 typedef struct sqlist 插入演算法 i到n...