線性表的鏈結表示

2021-10-07 03:16:20 字數 4780 閱讀 9787

單鏈表以及兩個集合「交」的程式:

#include

#include

using

namespace std;

const

int size =20;

template

<

class

t>

class

singlelst

;template

<

class

t>

class

linearlist

;template

<

class

t>

class

node

;template

<

class

t>

class

singlelist

:public linearlist

~singlelist()

;bool

isempty()

const

;int

length()

const

;bool

find

(int i, t& x)

const

;int

search

(t x)

const

;bool

insert

(int i, t x)

;bool

delete

(int i)

;bool

update

(int i, t x)

;void

clear()

;void

output

(ostream& out)

const

;private

: node

* first;};

template

<

class

t>

singlelist::~

singlelist()

}template

<

class

t>

bool singlelist

::isempty()

const

template

<

class

t>

int singlelist

::length()

const

template

<

class

t>

bool singlelist

::find

(int i, t& x)

const

node

* p = first;

for(iny j =

0; j < i; j++

) p = p -

> link;

x = p-

>element;

return

true;}

template

<

class

t>

int singlelist

::search

(t x)

const

template

<

class

t>

bool singlelist

::insert

(int i, t x)

node

* q =

new node

; q-

>element = x;

node

* p=first;

for(iny j =

0; j < i; j++

) p = p -

> link;

if(i >-1

)else

n++;return

true;}

template

<

class

t>

bool singlelist

::delete

(int i)

if(i<

0|| i>n -1)

node

* p = first,

* q = first;

for(

int j =

0; j < i -

1; j++

) q = q-

>link;

if(i ==0)

first = first-

>link;

else

delete p;

n--;return

true;}

template

<

class

t>

bool singlelist

::update

(int i, t x)

node

* p = first;

for(

int j =

0; j < i; j++

) p = p-

>link;

p->element = x;

return

true;}

template

<

class

t>

void singlelist

::output

(ostream& out)

const

out << endl;

}template

<

class

t>

void

intersection

(singlelist

& la, singlelist

& lb)

}void

main()

for(

int i =

5; i <

10; i++

) lb.

insert

(i -

6,i)

;//插人5~9,構造集合b=

lb.insert(-

1,0)

;//lb中插入0,lb=

lb.insert(3

,2);

//lb中插入2,lb=

lb.insert

(lb.

length()

-1,4

);//lb中插人4, lb=

intersection

(la,lb)

;//兩集合並,結果放人la中

la.output

(cout)

;//輸出最終結果la=

//system("pause");

}

帶表頭結點單鏈表的構造、插入和刪除函式:

template

<

class

t>

headerlist

::headerlist()

template

<

class

t>

headerlist

::insert

(int i,t x)

node

* p = first;

for(

int j =

0; j <= i; j++

) p = p-

>link;

node

* q =

new node

; q-

>element = x;

q->link = p-

>link;

p->link = q;

n++;return

true

;}

template

<

class

t>

headerlist

::delete

(int i)

if(i<

0|| i>n -1)

node

* q = first,

*p;for

(int j =

0; j < i; j++

) q = q-

>link;

p - q-

>link;

q->link = p-

>link;

delete p;

n--;return

true

;}

雙向鍊錶:

template

class

doublelist

;template

class

dnode

;插入操作:(在p結點前插入)

dnode

*q=new dnode;q-

>element=x;

q->llink=p-

>llink;

q->rlink=p;

p->llink-

>rlink=q;

p->llink=q;

刪除操作:

p->llink-

>rlink=p-

>rlink;

p->rlink-

>llink=p-

>llink;

delete p;

棧的實現 鏈結表示

include include struct node typedef struct node pnode 指向結點的指標型別 struct node 鏈棧型別定義 struct linkstack typedef struct linkstack plinkstack plinkstack cre...

線性表續篇 線性表的鏈式表示

public class 04linearlist02 初始化指標域和資料域 private node t obj,node n 得到當前節點的資料域 public t getdata 得到當前節點的指標域 public node getnext 鍊錶的長度 private int length 鍊...

線性表順序表示

include include include define listlength 100 typedef struct datatype typedef struct initlist,list 建立並返回乙個空的線性表 list createlist void else printf out o...