数据结构与算法分析-C语言(英文版)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

takes O(1) time. ... an
NULL
a1 pre->next =
...
ai
ai+1
node->next
free ( node )
Question: How can we delete the first node from a list? Answer: We can add a dummy head node to a list.
§2 The List ADT
S5
C1
C2
C3
C4
§2 The List ADT
3. Cursor Implementation of Linked Lists (no pointer) Features that a linked list must have:
a) The data are stored in a collection of structures. Each structure contains data and a pointer to the next structure. b) A new structure can be obtained from the system‟s global memory by a call to malloc and released by a call to free. Cursor Element Space Next
Head pointer ptr = 0110
ZHAO
QIAN
NULL
Insertion
ptr node

b temp
§2 The List ADT
takes O(1) time. ... an
NULL
a1
...
ai
ai+1
temp->next =
node->next
node->next = temp
a
am1 em1
……
a0
e0
NULL
Multilists
§2 The List ADT
〖Example〗 Suppose that we have 40,000 students and 2,500
courses. Print the students‟ name list for each courses, and print the registered classes‟ list for each student.
list_ptr N1, N2 ; N1 = (list_ptr)malloc(sizeof(struct list_node)); N2 = (list_ptr)malloc(sizeof(struct list_node)); Locations of the nodes may N1->data = ‘ZHAO’ ; Initialization: change onN2->data = ‘QIAN’ ; different runs. N1->next = N2 ; typedef struct list_node *list_ptr; N2->next = NULL ; typedef struct list_node { ptr = N1 ; char data [ 4 ] ; list_ptr next ; }; ptr list_ptr ptr ;
Coefficient Exponent Next

typedef struct poly_node *poly_ptr; struct poly_node { int Coefficient ; /* assume coefficients are integers */ int Exponent; poly_ptr Next ; }; typedef poly_ptr a ; /* nodes sorted by exponent */
1. Simple Array implementation of Lists array[ i ] = itemi Sequential mapping
Address Content …… …… array+i itemi array+i+1 itemi+1 …… ……
§2 The List ADT
§2 The List ADT
【Representation 2】 Given: A( x ) am 1 x em1 a0 x e0 We represent each term as a node Declaration:
§2 The List ADT
where em 1 em 2 e0 0 and ai 0 for i 0, 1, , m 1.
【Representation 1】 typedef struct { int CoeffArray [ MaxDegree + 1 ] ; int HighPower; } *Polynomial ;
Try to apply MultPolynomial (p.53) I likeN *N1000 easy to complexity O( =it! 2 ) +5x time Really? WhatIt’s the 14+1 and is 1 On P1(x) 10x of the operations, implement mostwith that? polynomials What’s wrong 2x1492+11x+5 for finding the product of two P2(x) = 3x1990 suchof degree N Multiplication. as Add and and N ? 1 2 -- now do you see my point?
Question: What will happen if the order of the two steps is reversed?
Question: How can we insert a new first item?
Deletion
ptr pre

b node
§2 The List ADT
Baidu Nhomakorabea
2. Linked Lists
Address 0010 0011 0110 1011 Data SUN QIAN ZHAO LI Pointer 1011 0010 0011 NULL
ptr ZHAO SUN
§2 The List ADT
QIAN LI
NULL
To link „ZHAO‟ and „QIAN‟:
§2 The List ADT
ADT: Objects: ( item0, item1, , itemN1 ) Operations:
Finding the length, N, of a list. Why after? Printing all the items in a list. 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. Finding next of the current item from a list. Finding previous of the current item from a list.
Read programs in Figures 3.6-3.15 for detailed implementations of operations.
Doubly Linked Circular Lists
§2 The List ADT
Suppose the I‟ll go a list the 1st node have from find youprevious node? the ask me? :-) Why do you doubly linked lists? 1->2->3->…->m. to delete A doubly linked circular list with head the m-th node. Maybe you wantta node: Now how would you Then you are asked to find the m-th node? get the m-th node? 1? its previous node m 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. Operations:
【Representation 1】 int Array[40000][2500];
1 if student i is registeredfor course j Array[i ][ j ] 0 otherwise
【Representation 2】
S1 S2 S3 S4

MaxSize has to be estimated. Find_Kth takes O(1) time. Insertion and Deletion not
only take O(N) time, but also involve a lot of data movements which takes time.
llink rlink typedef struct node *node_ptr ; item typedef struct node { node_ptr llink; Uhhh ... Then I‟ll have to Don‟tptr->llink->rlink ptr = we have element item; go from the 1st node again. enough headache already? node_ptr rlink; ptr->rlink->llink But hey, why do I=wantta }; Why do we need
CHAPTER 3
Lists, Stacks, and Queues
§1 Abstract Data Type (ADT)
【Definition】Data Type = { Objects } { Operations } 〖Example〗 int = { 0, 1, 2, , INT_MAX, INT_MIN } { , , , , , } 【Definition】An Abstract Data Type (ADT) is a data type that is organized in such a way that the specification on the objects and specification of the operations on the objects are separated from the representation of the objects and the implementation on the operations.
Finding degree, max { ei }, of a polynomial. Addition of two polynomials. Subtraction between two polynomials. Multiplication of two polynomials. Differentiation of a polynomial.
相关文档
最新文档