Ch03 List,Stacks,and Queues课件

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

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
Head pointer ptr = 0110
ZHAO
QIAN
Baidu NhomakorabeaNULL
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
•methods available for list only:
•Void push_front(const Object &x) •Void pop_front()
•methods available for vector only:
•Object &operator[](int idx) •Object &at(int idx) •Int capacity() const •Void reserve(int new capacity)
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 */
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
a
am1 em1
……
a0
e0
NULL
Vector and list in the STL
•STL:Standard Template Library
•Collection or container •Two popular implementations of the List ADT:
• Vector : provides a growable array implementation • list : provides a doubly linked list implementation
ptr ZHAO SUN
§2 The List ADT
QIAN LI
NULL
To link „ZHAO‟ and „QIAN‟:
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 ;
1. Simple Array implementation of Lists array[ i ] = Ai Sequential mapping
Address Content …… …… array+i Ai array+i+1 Ai+1 …… ……
§2 The List ADT

MaxSize has to be estimated. Find_Kth takes O(1) time. Insertion and Deletion not
【Representation 2】
§2 The List AD
Given: A( x) am1 xem1 a0 xe0
We represent each term as a node Declaration:
where em1 em2 e0 0 and ai 0 for i 0, 1, , m 1.
§2 The List ADT
ADT: Objects: ( A0, A1, , AN1 ) Ai follows(or succeeds) Ai-1, Ai-1 precedes Ai The Position of element Ai in a list is i Operations:
Operations:
Finding degree, max { ei }, of a polynomial. Addition of two polynomials. Subtraction between two polynomials. Multiplication of two polynomials. Differentiation of a polynomial.
•3 methods available for all the STL containers:
•Int size()
•Void clear()
•Bool empty()
•methods available for vector and list:
•Void push_back(const Object &x) •Void pop_back() •Const Object &back() const •Const Object &front() const
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.
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
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
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.
§2 The List ADT
【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?
Finding the length, N, of a list. 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.
An empty list :
H


Application
§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.
相关文档
最新文档