線性表 鍊錶

2021-06-18 20:02:01 字數 1797 閱讀 5506

線性表的adt

list.h

//線性表的c++抽象類宣告

templateclass list

;

單鏈表節點的定義

link.h

// 單鏈表節點類的定義

template class link

link(link* nextval = null)

};

鍊錶的實現宣告 & 成員函式的是實現

// 鍊錶的實現宣告

#include "stdafx.h"

#include "list.h"

#include "link.h"

template class llist: public list

void removeall() }

public:

llist()

~llist()

void clear()

bool insert(const elem&);

bool remove(elem&);

void setstart()

void setend()

void prev();

void next() }

int leftlength() const

int rightlength() const

bool setpos(int pos);

bool getvalue(elem& it) const

it = fence->next->element;

return true;

} void print() const;

};templatebool llist::insert(const elem& item)

rightcnt++;

return true;

}templatebool llist::remove(elem& it)

it = fence->next->element;

link* ltemp = fence->next;

fence->next = ltemp->next;

if(tail == ltemp)

delete ltemp;

rightcnt--;

return true;

}templatevoid llist::prev()

fence = temp;

leftcnt--;

rightcnt++;

}templatebool llist::setpos(int pos)

fence = head;

for(int i = 0; i < pos; i++)

return true;

}templatevoid llist::print() const

cout << "| ";

while(temp->next != null)

cout << ">\n ";

}

測試:

#include "stdafx.h"

#include #include "llist.h"

using namespace std;

int _tmain(int argc, _tchar* argv)

mylist.print();

cout << endl;

return 0;

}

線性表 鍊錶

include include typedef int elemtype typedef struct node lnode,linklist linklist createlinklist1 頭插法 linklist createlinklist2 尾插法 void lengthlinklist ...

線性表,鍊錶

資料的儲存結構分為鏈式儲存結構,線性儲存結構。不管什麼型別的資料結構,都會以這兩種儲存方式在計算機中儲存。線性儲存結構就是開闢一段連續的記憶體 記憶體大小已經確定 將資料儲存在這段連續記憶體中,這種儲存結構的優點是可以快速地取出元素,時間複雜度為o 1 缺點是插入和刪除需要移動大量的元素,時間複雜度...

線性表 鍊錶

1.陣列長度和線性表長度區別?答 陣列的長度是存放線性表的儲存空間的長度,儲存分配後這個量是一般是不變的。有個別同學可能會問,陣列的大小一定不可以變嗎?我怎麼看到有書中談到可以動態分配的一維陣列。是的,一般高階語言,比如c.vb c 都可 人用程式設計手段實現動態分配陣列,不過這會帶來效能上的損耗。...