当前位置:
文档之家› 数据结构浙江工业大学(ppt)
数据结构浙江工业大学(ppt)
8
Main Index
Contents
2. Creating a Linked List
将1,2,3,4,5顺序插入链表(正向插入) node<int> *nodeptr1,*nodeptr2; for(int i=1;i<6;i++) {
nodeptr1=new node<int>(i,null); nodeptr1? } nodeptr1=new node<int>(1,null); for(int i=2;i<6;i++) { nodeptr2=new node<int>(i,null); nodeptr1->next=nodeptr2; nodeptr1=nodeptr1->next; }
Before
After front
front
back item
Before
front
(a)
newNode
20
55
front
After
front
(b)
item
20
55
front
12
Main Index
Contents
3.2 Handling the Back of the List
top D
C B A
public: T nodeValue; // data held by the node node<T> *next; // next node in the list
node() : next(NULL) {}
node(const T& item, node<T> *nextNode = NULL) : nodeValue(item), next(nextNode)
{} };
7
Main Index
Contents
1.如何生成链表
node<int> *nodeptr1,*nodeptr2; nodeptr1=new node<int>(1,null); nodeptr2=new node<int>(2,null); 思考:如何连接两个结点? nodeptr1->next=nodeptr2; nodeptr3=new node<int>(3,null); nodeptr2->next=nodeptr3;
10
Main Index
Contents
2. Creating a Linked List
node<int> *front=Null, *newNode,*back;
int i=0;
while(i<5)
{
if(i==0){
front=new node<int>(i,NULL);back=front;Di s c o n n e c t
R e c o n n e c t
3
Main Index
Contents
Linked List Nodes Removal is like Insertion in reverse.
Disconnect
Reconnect
4
Main Index
Contents
Stack
front
D
C
B
A
LinkedList
13
Main Index
Contents
3.3 Handling the Back of the List
node<T> *curr=front; while(curr!=null){
curr=curr->next; }
14
Main Index
Contents
3.4 Handling the Back of the List – with back pointer
front back
... //
//
item newNode
The initial list is empty: front=NULL, back=NULL.
0xbfffaa80
2 0xbfffaa28
4 0xbfffaa60
6 0xbfffaa80
8 Null
first 0xbfffaa20
first是指向node类型的指针: node<int> *first; first=new node<int>(2,NULL);
Node结点设计
Template <typename T> class node {
nodeValue
next
nodeValue next
5
Main Index
Contents
Abstract Model of a List Object
data next first
data next
data next
data null
0xbfffaa20
0xbfffaa28
0xbfffaa60
}
else{
newNode=new node<int>(i,NULL);
back->next=newNode;
back=back->next;
}
i++;
}//不同的编程风格
11
Main Index
Contents
3. 1 Inserting at the Front of a Linked List
Main Index
Contents
2. Creating a Linked List
将1,2,3,4,5顺序插入链表(反向插入) node<int> *front=Null, *newNode; int i;
for(i=1;i<=5;i++) {
front=new node<int>(i,front); }
Node Composition l An individual Node is composed of two parts, a
Data field containing the data stored by the node, and a Pointer field that marks the address of the next Node in the list.
数据结构浙江工业大 学(ppt)
1
(优选)数据结构浙江工业大学
Linked List Nodes Each Node is like a piece of a chain
Individual P iece
P opC hain
l To insert a new link, break the chain at the desired location and simply reconnect at both ends of the new piece.