複習 線性表

2021-06-27 23:29:03 字數 3272 閱讀 5210

快要考試了,所以準備這幾天把資料結構都過一遍,也就是對那些基本演算法進行一次溫習吧,加油!!!

首先先從線性表開始,線性表有2種儲存方式,順序儲存和鏈式儲存。

順序儲存:

l順序儲存方法:

用一組位址連續

的儲存單元依次儲存線性表的元素,可通過

陣列來實現。

**:

#define maxsize 100

#define ok 1

#define error -1

#include #includeusing namespace std;

typedef int elemtype;

typedef struct

sqlist;

//初始化

void initlist(sqlist &l)

//獎勵乙個順序儲存的線性表

void creatsqlist(sqlist &l, int n)

//輸出順序表

void output(sqlist l)

//定位,返回l中第乙個與x相等的資料元素位置

int locateelem(sqlist l, elemtype x)

return ok;

}//刪除第i個資料元素

int delete(sqlist &l, int i)

} while (i> m;

getchar();

switch (m)

case 2:

case 3:

case 4:

case 5:

case 6:

case 7:

default:

return 0;

} cout << "繼續執行y(1)/n(0): ";

cin >> k;

if (!k)

return 0;

}}

鏈式儲存:結點在儲存器中的位置是

任意的,即

邏輯上相鄰的資料元素在物理上不一定相鄰 

實現**:

//這一次補上鍊表的注釋,**是空閒的時候敲出來的,如果有錯,希望幫忙糾正

//部分給出了詳細說明,這裡只選取了基本操作,因為更複雜的鍊錶操作太繁瑣,這裡就不寫了

//如果有什麼不懂的地方,可以隨時詢問

#include using namespace std;

typedef int elemtype;

struct node

;void init(node *l) //主函式裡面定義node型指標變數plist,儲存單鏈表的頭指標

//這裡有兩種方式表達,

//1.初始化函式形參要給二級指標,相應的主函式裡面實參則是對頭指標的引用

//2.初始化函式形參直接給頭指標,主函式裡的實參就是頭指標

void clear_list(node *head) //清空函式

while (head->next != null)

cout << "鍊錶已經清空!\n";

}//加乙個標記,移動頭指標後賦值flag儲存下乙個位置,然後刪除當前節點

node *creat_list(node *head) //賦值函式

//coutreturn head;

}//後來想了想p2還是不能刪,如果刪了雖然不會報錯,但是顯示程式停止,所以我覺得p2的作用應該是儲存之前的節點

//可以想一下,如果刪除p2,那麼程式首先讀入p1,在判斷p1資料域非0後,要用p2儲存下p1節點,然後p1在向後更新,如此反覆。。。

//讀入鍊錶元素,在while迴圈裡面每次將當前指標儲存下來,然後不斷向後更新

void list_length(node *head)

cout << "鍊錶長度length: " << i << endl;

}void out_list(node *head)

cout << endl; }}

elemtype get_elem(int index, node *head) //給定下標,求節點元素

if (jdata;

}//給定的下標index,只需遍歷鍊錶,當節點數符合要求時即可

int locate_elem(node *head, elemtype _first) //給定節點元素,求第乙個下標(也可以擴充套件求多個點的下標或者位址)

cout << "單鏈表裡不存在 " << _first << " 這個元素" << "!\n";

return 0;

}//同理,遍歷查詢出給定元素,然後直接返回下標

bool insert_elem(node *head, elemtype insert_elem, int index)

int i = 1;

while (head != null) }

return false;

}//插入步驟稍微多些,要另外定義兩個指標,思路就是遍歷到給定下標的前乙個下標,同時指標q記錄下位置

bool delete_node(node *head, int index)

while (head != null) }

return false;

}//操作類似於插入,不多說

bool change_node_elem(node *head, int index, elemtype example3)

while (head != null) }

return false;

}//也是遍歷鍊錶找到給定位置,然後更新資料

int main()

else

cout << "insert is not ok!\n";

cout << endl;

cout << "將鍊錶第七個節點刪除:" << endl;

if (delete_node(plist, 7))

else

cout << "delete is not ok!\n";

cout << endl;

cout << "將鍊錶plist中第三個節點的元素換成 " << example3 << endl;

if (change_node_elem(plist, 3, example3))

else

cout << "數值改變失敗!\n";

cout << endl;

cout << "執行清空函式:" << endl;

clear_list(plist);

return 0;

}

複習 線性表習題

1.順序儲存方式插入和刪除時效率太低,因此它不如鏈式儲存方式好。x 二者各有優缺點 取元素和插入刪除 應看具體場景 2.鍊錶中的頭結點僅起到標識的作用。x 頭結點還有簡化操作的作用 3.靜態鍊錶中指標表示的是 下一元素的陣列下標 4.設乙個鍊錶最常用的操作是在末尾插入結點和刪除尾結點,則選用 d 最...

考研複習(1) 線性表

還有5個月考研,還是得敲。環境 ubuntu10.10 ide code blocks 線性表的基本操作,還有合併,比較演算法 include include define list init size 100 define list increase 10 typedef char elemtyp...

考研複習(1) 線性表

還有5個月考研,還是得敲。環境 ubuntu10.10 ide code blocks 線性表的基本操作,還有合併,比較演算法 include include define list init size 100 define list increase 10 typedef char elemtyp...