單鏈表:單鏈表是一種鏈式訪問的資料結構#include
#include
using namespace std;
typedef int datatype;
class
seqlist
seqlist
(const
seqlist&s)
/*seqlist&operator(seqlist
s) */
seqlist&operator=(seqlist
s)
~seqlist
()
}void print
()
cout << endl;
}void cheakcapacity
()
}void swap
(seqlist& s)
void pushback
(datatype
x) void popback
() void pushfront
(datatype
x)void popfront
() void insert
(size_t
pos, datatype
x) _array[0] = x;
}else
_array[pos] = x;
}_size++;
}void erase
(size_t
pos)
_size--;
}//popback的實現
else if(pos == _size)
else
_size--;}}
return;
}}datatype& operator(size_t
pos)
private:
datatype* _array;
size_t* _size;
size_t* _capacity;
};
tail指標是為了更方便的查詢到末尾,在末尾插入元素。
單鏈表則是一次只開闢乙個結點的空間,用來儲存當前要儲存的資料及指向下乙個結點或null的指標
#include
#include
#include
using namespace std;
typedef int datatype;
struct slistnode
};class slist
slist(const slist& s)
:_head(null)
, _tail(null)
}slist& operator=(const slist& s)
return
*this;
}~slist()
void clear()
_head = _head =
null;
}void pushback(datatype x)
else
}void popback()
else
if(_head->_next==
null)
else
_tail = tmp;
tmp->_next =
null;
}}void pushfront(datatype x)
else
}void popfront()
else
}node* find(datatype x)
tmp = tmp->_next;
}return
null;
}void insert(node* pos, datatype x)
else
node* tmp =
new node(x);
tmp->_next = pos;
cur->_next = tmp;
}}void erase(node* pos)
else
cur->_next = pos->_next;
delete pos;}}
void print()
cout<
}private:
node* _head;
node* _tail;
};
順序表和單鏈表的比較 c 實現刪除單鏈表結點
順序表和單鏈表的比較 訪問方式 順序表和順序訪問,也可以隨意訪問,鍊錶只能從表頭順序訪問元素。查詢,插入刪除操作 對於按值查詢,當順序表在無序情況下,兩者的時間複雜度均為o n 而當順序表有序時,可以採用折半查詢,此時時間複雜度為log2n.對於按序號查詢,順序表支援隨機訪問,時間複雜度為o 1 而...
單鏈表實現佇列 順序表實現佇列
一 sysutil.h 系統標頭檔案 include include include include include include include 記憶體洩漏工具的標頭檔案二 單鏈表實現佇列函式 include sysutil.h define queueelemtype int 鏈佇列typed...
資料結構 順序表和單鏈表c
include using namespace std constexpr auto maxsize 100 constexpr auto error 0 constexpr auto ok 1 typedef int elemtype 順序表的儲存結構 typedef struct sqlist ...