?????? - PowerPoint PPT Presentation

1 / 88
About This Presentation
Title:

??????

Description:

Title: Data Structure - CH2 Author: Jiang Yuan Last modified by: wujun Created Date: 8/20/2004 9:00:43 AM Document presentation format: (8.5x11 ) – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 89
Provided by: Jian143
Category:
Tags: doubly | link | linked | list

less

Transcript and Presenter's Notes

Title: ??????


1
??? ???
  • ???
  • ???
  • ???
  • ????
  • ????
  • ???

????????
????????
?????
2
??? ???
2.1 ???
  • ??
  • n( ? 0)????????
  • L (a1, a2, , an)
  • ai???,n?????
  • ????????,???????

3
  • ??????
  • ?????,?????????????
  • ????,??????????????
  • ???????
  • ????????
  • ?????? ???
  • ?????? ??

4
2.2 ???
  • ???????????

LOC ( i ) LOC ( i -1 ) l a il
5
  • ??????
  • ???????????????????????
  • ??????
  • ???????????????
  • ???????????,???????

6
?????????????
  • define maxSize 100
  • typedef int T
  • typedef struct
  • T datamaxSize //??????????
  • int last
  • SeqList
  • typedef int T
  • typedef struct
  • T data //??????????
  • int maxSize, last
  • SeqList

7
?????(????)
  • ????????????????,???????union
  • typedef union
  • int val //?data.val??
  • char ch //?data.ch??
  • float dir //?data.dir??
  • union data link //?data.link??
  • data //????????data

8
???(SeqList)????
  • include ltiostream.hgt //???seqList.h?
  • include ltstdlib.hgt
  • include linearList.h"
  • const int defaultSize 100
  • template lt class Egt
  • class SeqList public LinearListltEgt
  • protected
  • E data //????
  • int maxSize //??????????
  • int last //???????????
  • void reSize(int newSize) //????????

9
  • public
  • SeqList(int sz defaultSize) //????
  • SeqList(SeqListltEgt L) //??????
  • SeqList() delete data //????
  • int Size() const return maxSize //??????
  • int Length() const return last1 //?????
  • int Search(E x) const
  • //??x?????,????????
  • int Locate(int i) const
  • //??? i ???,????????
  • bool getData(int i, E x) const //??i?????
  • bool Insert(int i, E x) //??
  • bool Remove(int i, E x) //??

10
????????
  • include ltstdlib.hgt //??exit????
  • include seqList.h //??????seqList.cpp
  • template ltclass Egt
  • SeqListltEgtSeqList(int sz)
  • if (sz gt 0)
  • maxSize sz last -1
  • data new EmaxSize //???????
  • if (data NULL) //??????
  • cerr ltlt "??????!" ltlt endl
  • exit(1)

11
??????
  • template ltclass Egt
  • SeqListltEgtSeqList ( SeqListltEgt L )
  • maxSize L.Size() last L.Length()-1 E
    value
  • data new EmaxSize //??????
  • if (data NULL) //??????
  • cerr ltlt "??????!" ltlt endl exit(1)
  • for (int i 1 i lt last1 i) //??????
  • L.getData(i, value) datai-1 value

12
????????
  • template ltclass Egt
  • int SeqListltEgtSearch(E x) const
  • //??????????? x ?????,???
  • //?????????????,??????0
  • for (int i 0 i lt last i) //????
  • if ( datai x ) return
    i1 //??????????1
  • return 0 //????

13
??????(??)
x 48 x 50
14
???????????(??????)
  • ACN(Average Comparing Number) (???????n,?n
    last 1)

??????i?????pi,????ci
??????
?????pi ??,?
?????????n?
15
??????
16
???????
  • template ltclass Egt
  • bool SeqListltEgtInsert (int i, E x)
  • //????x??????i (1ilast2) ????
  • //?? if (last maxSize-1) return false
    //??
  • if (i lt 1 i gt last2) return false
    //??i???
  • for (int j last j gt i-1 j--)
    //????
  • dataj1 dataj
  • datai-1 x //??(? i ???datai-1?)
  • last return true //????

17
??????????(????)
???? i ?????,?datai-1 ?data last
????,??n-1-(i-1)1 n-i1? (???????n,?n last
1)
????????AMN(Average Moving Number)????????????
?????n1?????,????n/2?
18
????????
19
???????
  • template ltclass Egt
  • bool SeqListltEgtRemove (int i, E x)
  • //?????? i (1ilast1) ???,?????
  • //?? x ??????? if (last -1) return false
    //??
  • if (i lt 1 i gt last1) return
    false//??i???
  • x datai-1
  • for (int j i j lt last j)
    //????,??
  • dataj-1 dataj
  • last-- return true

20
????????????(????)
??? i ???,??? i1 ??? last1?????,???????
n-(i1)1 n-i (???????n,?n last 1)
????????AMN(Average Moving Number)?n???????????
?????n?????,????(n-1)/2?
21
??????
????????????
void Union ( SeqListltintgt LA,
SeqListltintgt LB ) int n
LA.Length ( ) int m LB.Length ( )
int x for ( int i 1 i lt m i )
LB.getData(i, x) //?LB????? int k
LA.Search (x) //?LA???? if ( k 0 )
//??????? LA.Insert (n,
x) n
22
????????????
void Intersection ( SeqListltintgt LA,
SeqListltintgt LB )
int n LA.Length ( ) int m LB.Length (
) int i 1 int x while ( i lt n )
LA.getData (i, x) //?LA????? int k
LB.Search (x) //?LB???? if ( k 0 )
LA.Remove (i,x) n-- //???,?LA???? else
i
23
2.3 ???
  • ??????(???)
  • ????????,?????
  • ???????????????????
  • ??????(??)
  • ?????????????
  • ?????????????

24
  • ??????
  • ????(??)???(Node)???
  • ????
  • ?????????
  • ?????????

25
????????
26
???????
  • ?????????(???)?
  • ????(ListNode)?
  • ??(List)?

27
  • ????
  • ????
  • ????
  • ????
  • ????

28
?????(????)
struct ListNode //??????
int data //????, ?? ListNode
link //???? class List
//??? public private
ListNode first //????
29
?????(????)
class List //???? class
ListNode //????? friend class List
//???????? private int data
//????, ?? ListNode link
//???? class List
//??? private ListNode first
//????
30
?????(????)
class List //???
public private class ListNode
//????? public int data
//????, ?? ListNode link
//???? ListNode first
//????
31
?????(????)
class ListNode //?????
protected int data //????,
?? ListNode link
//???? class List pubic class LinkNode
//??? public
private ListNode first
//????
32
????????????
  • ??
  • ?????????????
  • newNode-gtlink first
  • first newNode

(???) (???)
33
  • ????????????
  • newNode-gtlink current-gtlink
  • current-gtlink newNode

(???) (???)
34
  • ????????????
  • newNode-gtlink current-gtlink
  • current-gtlink newNode

(???) (???)
35
????????
  • bool ListInsert(int i, int x)
  • //???? x ???? i ??????i ?1??,
  • //i 0 ????????????
  • if (first NULL i 0) //????????
  • LinkNode newNode new LinkNode(x)
    //???????
  • newNode-gtlink first first newNode
  • //?????????
  • else //??,??????
  • LinkNode current first int k 1

36
  • while (k lt i current ! NULL) //??i??
  • current current-gtlink k
  • if (current NULL first ! NULL)
    //??
  • cerr ltlt ???????!\n return false
  • else //????????
  • LinkNode newNode new LinkNode(x)
  • newNode-gtlink current-gtlink
  • current-gtlink newNode
  • return true

37
  • ??
  • ????? ?????????
  • ????? ?????????

????????ai???
38
????????
  • bool ListRemove (int i, int x)
  • //?????? i ?????, i ?1???
  • LinkNode del //????????
  • if (i lt 1) del first first
    first-gtlink
  • else
  • LinkNode current first k 1
    //?i-1???
  • while (k lt i-1 current ! NULL)
  • current current-gtlink k
  • if (current NULL current-gtlink
    NULL) cout ltlt ???????!\n return false

39
  • del current-gtlink //???/???
  • current-gtlink del-gtlink
  • x del-gtdata delete del //????????
  • return true
  • ?????????????,???????,????????,???????
  • ????,???????????????????
  • ???????????????????

40
??????(????)????
  • ???????????,??????,??????
  • ?????????????????????,??????????

an
a1
first
first
0
0
??? ??
41
??????????????????
newNode-gtlink current-gtlink
current-gtlink newNode
42
???????????????????
(???)
del current-gtlink current-gtlink
del-gtlink delete del
(??)
43
2.4 ?????????
  • ????
  • ????

44
???? (Circular List)
????
??????????
45
  • ???????
  • ??????????? link ??? ? 0 (NULL),??????????
  • ?????????????,???????????????

46
????????
  • template ltclass Egt
  • struct CircLinkNode //???????
  • E data
  • CircLinkNodeltEgt link
  • CircLinkNode ( CircLinkNodeltEgt next
  • NULL ) link next
  • CircLinkNode ( E x, CircLinkNodeltEgt next
  • NULL ) data x link next
  • bool Operator(CircLinkNodeltEgt node)
    return data node.data
  • bool Operator!(CircLinkNodeltEgt node)
    return data ! node.data

47
  • template ltclass Egt //?????
  • class CircList public LinearListltEgt
  • private
  • CircLinkNodeltEgt first, last //???, ???
  • public
  • CircList(const E x) //????
  • CircList(CircListltEgt L) //??????
  • CircList() //????
  • int Length() const //??????
  • bool IsEmpty() return first-gtlink first

  • //????
  • CircLinkNodeltEgt getHead() const

  • //????????

48
  • void setHead ( CircLinkNodeltEgt p )

  • //????????
  • CircLinkNodeltEgt Search ( E x ) //??
  • CircLinkNodeltEgt Locate ( int i ) //??
  • E getData ( int i )
    //??
  • void setData ( int i, E x ) //??
  • bool Insert ( int i, E x )
    //??
  • bool Remove ( int i, E x)
    //??
  • ?????????????,?????????????,?????NULL,?????

49
?????????
?????
??25
50
?????????
template ltclass Egt CircListNodeltEgt
CircListltEgtSearch( E x ) //????????????? x
??? current first-gtlink while (
current ! first current-gtdata ! x )
current current-gtlink return current
51
?????????????
  • ????? (?n 8 m 3)

52
??Josephus?????
  • include ltiostream.hgt
  • include CircList.h
  • template ltclass Egt
  • void Josephus(CircListltEgt Js, int n, int m)
  • CircLinkNodeltEgt p Js.getHead(),
  • pre
    NULL
  • int i, j
  • for ( i 0 i lt n-1 i ) //??n-1?
  • for ( j 1 j lt m j) //?m-1??
  • pre p p p-gtlink
  • cout ltlt ????? ltlt p-gtdata ltlt endl

53
  • pre-gtlink p-gtlink delete p
    //??
  • p pre-gtlink
  • void main()
  • CircListlt intgt clist
  • int i, n, m
  • cout ltlt ????????????
  • cin gtgt n gtgt m
  • for (i 1 i lt n i ) clist.insert(i,
    i) //????
  • Josephus(clist, n, m)
    //???????

54
????(doubly linked list)
  • ???????

???? ? ? ????
  • ???????????

55
  • ??????????
  • p p?lLink?rLink p?rLink?lLink

56
??????????
  • template ltclass Egt
  • struct DblNode //???????
  • E data //??????
  • DblNodeltEgt lLink, rLink //???????
  • DblNode ( DblNodeltEgt l NULL,
  • DblNodeltEgt r NULL )
  • lLink l rLink r
    //????
  • DblNode ( E value, DblNodelt Egt l NULL,
    DblNodeltEgt r NULL)
  • data value lLink l rLink r
    //????

57
  • template ltclass Egt
  • class DblList //?????
  • public
  • DblList ( E uniqueVal ) //????
  • first new DblNodeltEgt (uniqueVal)
  • first-gtrLink first-gtlLink first
  • DblNodeltEgt getFirst () const return first
  • void setFirst ( DblNodeltEgt ptr ) first ptr
  • DblNodeltEgt Search ( E x, int d)
  • //?????d???????????x???,
  • //d0?????,d?0?????

58
  • DblNodeltEgt Locate ( int i, int d )
  • //?????????i(0)???, d0????
  • //?,d?0?????
  • bool Insert ( int i, E x, int d )
  • //??i????????????x????,d0
  • //?????,d?0?????
  • bool Remove ( int i, E x, int d ) //???i???
  • bool IsEmpty() return first-gtrlink first
  • //??????
  • private
  • DblNodeltEgt first //????

59
???????????
60
???????????
  • template ltclass Egt
  • DblNodelt Egt DblListltEgtSearch (E x, int d)
  • //??????????????x????
  • DblNodeltEgt current (d 0)?
  • first-gtlLink first-gtrLink
    //?d??????
  • while ( current ! first current-gtdata ! x
    ) current (d 0) ?
  • current-gtlLink current-gtrLink
  • if ( current ! first ) return current
    //????
  • else return NULL //????

61
??????????? (???)
newNode-gtrLink current-gtrLink current-gtrLink
newNode newNode-gtrLink-gtlLink newNode
newNode-gtlLink current
62
??????????? (??)
first
first
25
???25
newNode
current
current
newNode-gtrLink current-gtrLink
(newNode-gtrLink first) current-gtrLink
newNode newNode-gtrLink -gtlLink newNode
( first-gtlLink newNode )
newNode-gtlLink current
63
???????????
  • template ltclass Egt
  • bool DblListltEgtInsert ( int i, E x, int d )
  • //????????x????, ???? d ???
  • //??????i??????
  • DblNodelt Egt current Locate(i, d)
  • //?d???????i???
  • if ( current NULL ) return false
    //????
  • DblNodeltEgt newNd new DblNodeltEgt(x)
  • if (d 0) //???????i?????
  • newNd-gtlLink current-gtlLink //??lLink?
  • current-gtlLink newNd

64
  • newNd-gtlLink-gtrLink newNd //??rLink?
  • newNd-gtrLink current
  • else //???????i?????
  • newNd-gtrLink current-gtrLink //??rLink?
  • current-gtrLink newNd
  • newNd-gtrLink-gtlLink newNd //??lLink?
  • newNd-gtlLink current
  • return true //????

65
???????????
???
first
31
48
15
??48
current
first
31
15
current
current-gtrLink-gtlLink current-gtlLink
current-gtlLink-gtrLink current-gtrLink
66
???????????
  • template lt class Egt
  • bool DblListlt EgtRemove( int i, E x, int d )
  • //?????????d???????i????
  • DblNodelt Egt current Locate (i, d)
  • if (current NULL) return false
    //????
  • current-gtrLink-gtlLink current-gtlLink
  • current-gtlLink-gtrLink current-gtrLink
  • //?lLink??rLink????
  • x current-gtdata delete current
    //??
  • return true //????

67
2.5 ???????
A(x) 1 - 10x6 2x8 7x14
  • n???? Pn(x) ? n1 ??
  • ?? a0, a1, a2, , an
  • ?? 0, 1, 2, , n??????

68
??????????
??????????
  • ??? (??????)
  • private
  • int degree
  • float coef maxDegree1
  • Pn(x)?????
  • pl.degree n
  • pl.coefi ai, 0 ? i ? n


69
  • ??? (??????)
  • private
  • int degree
  • float coef
  • Polynomial Polynomial (int sz)
  • degree sz
  • coef new float degree 1
  • ???????????????????????????????????????,? P101(x)
    35x50-4x101, ????

70
??? struct term //???????
float coef //?? int exp
//?? static term termArraymaxTerms
//??? static int free, maxTerms
//????????
71
??? // term PolynomialtermArrayMaxTerms //
int Polynomialfree 0 class Polynomial
//????? public private int
start, finish //???????
72
?????????? A(x) 2.0x10001.8 B(x) 1.2
51.3x50 3.7x101
A.start A.finish B.start B.finish free
maxTerms
coef exp
1.8 2.0 1.2 51.3 3.7 0
1000 0 50 101
????????termArray?
73
?????????????
A(x) 1 - 10x6 2x8 7x14
???????
???
???
74
  • ????????????
  • ???????????????,?????????
  • ?????????????
  • ????????????
  • ?????????????,??????????
  • ???????,?????

75
???(polynomial)??????
  • struct Term //???????
  • float coef //??
  • int exp //??
  • Term link //????
  • Term (float c, int e, Term next NULL)
  • coef c exp e link next
  • Term InsertAfter ( float c, int e)
  • friend ostream operator ltlt (ostream,
  • const Term )

76
  • class Polynomial //???????
  • public
  • Polynomal() first new Term(0, -1) //????
  • Polynomal (Polynomal R) //??????
  • int maxOrder() //??????
  • private
  • Term first
  • friend ostream operator ltlt (ostream,
  • const Polynomal )
  • friend istream operator gtgt ( istream,
  • Polynomal )

77
  • friend void Add ( Polynomial A, Polynomial
    B,
  • Polynomial C )
  • friend void Mul ( Polynomial A, Polynomial
    B,
  • Polynomial C )

78
??????????
  • ????????????,????pa?pb??????????????,???????????C,
    ?????pc,?????C??????
  • ?pa?pb???????????,????????????
  • ????????C?,??????pa??pb?1
  • ????????????????????,?????C?,pa?pb?1?
  • ?pa?pb???????NULL,???????????????C??

79
????????
  • AH 1 - 3x6 7x12
  • BH - x4 3x6 - 9x10 8x14
  • CH 1 - x4 - 9x10 7x12 8x14

80
(No Transcript)
81
(No Transcript)
82
(No Transcript)
83
(No Transcript)
84
(No Transcript)
85
(No Transcript)
86
(No Transcript)
87
2.6 ????
  • ?????????????????,??????????
  • ?????????????????data?????,link????????
  • ????????????????,????????????????????
  • ?????????,????????????????????

88
???????
0 1 2 3 4 5
data a3 a4 a1 a5 a2
link 3 2 4 5 -1(0) 1
  • 0??????,link?????????
  • ???????link 0,???????????????,??????link -1?
  • link???????,??????
Write a Comment
User Comments (0)
About PowerShow.com