数据结构和算法分析C语言英文版专题培训课件
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
ai+1
... an
NULL
temp->next =
node->next
b temp
node->next = temp
Question: What will happen if the order of the two steps is reversed?
Question: How can we insert a new first item?
§2 The List ADT
typedef struct node *node_ptr ; typedef struct node {
llink
rlink
item
A} ;donenulooebddmleeye__nppltttirrnklriltlieeinndmkk;;;circMuTiltaWNhsSgyreogupBebhtlnwotprihe1uyUsetpfy-efttyhhvid>rhoonhwomioeoo2uhsoduewmei-m-euhyytt>attnwhyhs,ohwh-r3.toow.tueaehneWh-.honuu>neohdTIeaaupog…n’tdhayhoss1thldrlhtdkokdoeyluaaesDeed-pdetghvbnm?>tdynmodotdeohienelromoooney?IaIeeout=af=dul’’d?w.oredllwtmeslip1plotaene:i:fweaht?tnm-sc-irkratnn)ntaheo--he>g>tdevdethdtlarheenlealaaiidien?lonntlviokrdsk1.ee-te-s>sa>t.?rldllniiynno?kkde
§2 The List ADT
ADT:
Objects: ( item0, item1, , itemN1 )
Operations:
Finding the length, N, of a list. Printing all the items in a list.
Why after?
Making an empty list.
Finding the k-th item from a list, 0 k < N.
Inserting a new item after the k-th item of a list, 0 k < N.
Deleting an item from a list.
array[ i ] = itemi
Address Content
……
……
Sequential mapping
array+i array+i+1
……
itemi itemi+1 ……
MaxSize has to be estimated.
Find_Kth takes O(1) time.
Insertion and Deletion not
char data [ 4 ] ;
N1->next = N2 ; N2->next = NULL ; ptr = N1 ;
list_ptr next ;
来自百度文库
}; list_ptr ptr ;
ptr
ZHAO
QIAN
NULL
Insertion
ptr
a1
node
... ai
§2 The List ADT takes O(1) time.
Initialization:
Locations oNNf12-t=>hd(elaistant_=opdt‘Zr)eHmsAaOmllo’ ;ac(ysizeof(struct list_node)); change onNd2-i>fdfaetrae=n‘Qt IrAuNn’ ;s.
typedef struct list_node *list_ptr; typedef struct list_node {
Answer: We can add a dummy head node to a list.
Read programs in Figures 3.6-3.15 for detailed implementations of operations.
Doubly Linked Circular Lists
Deletion
ptr
a1
pre
... ai
§2 The List ADT takes O(1) time.
ai+1
... an
NULL
pre->next =
b
node->next
free ( node )
Questnioonde: How can we delete the first node from a list?
only take O(N) time, but also involve a lot of data movements which takes time.
2. Linked Lists
Address
0010 0011 0110 1011
Data
SUN QIAN ZHAO
LI
Pointer
1011 0010 0011 NULL
Finding next of the current item from a list.
Finding previous of the current item from a list.
1. Simple Array implementation of Lists
§2 The List ADT
H
item1
item2
item3
An empty list : H
Two Applications
§2 The List ADT
The Polynomial ADT
Objects : P ( x ) = a1 x e1 + + an x en ; a set of ordered pairs of < ei , ai > where ai is the coefficient and ei is the exponent. ei are nonnegative integers.
ptr ZHAO
§2 The List ADT
QIAN
SUN
LI
NULL
To link ‘ZHAO’ and ‘QIAN’:
Head pointer ptr = 0110
list_ptr N1, N2 ;
N1 = (list_ptr)malloc(sizeof(struct list_node));