Professional Documents
Culture Documents
2014/9/5
2014/9/5
9.1
--
= + +
2014/9/5
9.2
2014/9/5
NULL
(0)
9.2.1
struct node
{
int data;
node * next;
};
nextnodenode
28
head
2014/9/5
52
96
NULL
5
9.2.1
struct student {
int num;
char name[20];
char sex;
float score;
student * next;
};
2014/9/5
9.2.2
2014/9/5
head
NULL(0)
1n
#include <iostream.h>
struct node {
int data;
node * next;
};
node * createList(int n);
int main() {
int n;
node * listHead = NULL;
cout << "Please enter the number of nodes:";
cin >> n;
if (n > 0)
listHead = createList(n);
return 0;
} 2014/9/5
8
1n
node *createList(int n) {
node *temp, *tail = NULL, *head = NULL ;
int num;
cin >> num;
head = new node ; //
if (head == NULL) {
cout << "No memory available!";
return NULL;
}
else {
head->data = num;
head->next = NULL;
tail = head;
}
2014/9/5
1n
} 2014/9/5
10
head
tail
temp
NULL
NULL
NULL
head
tail
temp
1
NULL
2014/9/5
11
tail
head
2
NULL
tail
hea
3
d
1
2014/9/5
tem
p
tem
p
3
NULL
12
9.2.2
head->data = 15;
head->next->data = 15;
node * curNode = head;
while (curNode )
curNode = curNode->next;
2014/9/5
13
21
data
void outputList(node * head)
{
cout << "List: ";
node *curNode = head;
while ( curNode ) {
cout << curNode->data;
if (curNode ->next)
cout << " -> ";
curNode = curNode ->next;
}
cout << endl;
return;
} 2014/9/5
14
31
9.2.2
ac
(1) cptrc,aptra
(2) ac
cptr->nextaptr->next
(3) ca
aptr->next=cptr;
2014/9/5
16
17
newNode->data = n;
if (preNode == NULL) //
{
newNode->next = curNode;
return newNode;
}
else {
preNode->next = newNode;
newNode->next = curNode;
return head;
}
}
2014/9/5
18
9.2.2
c
(1)ccptr
c;
(2)c(d,dptr
d)dc
dptr->next=cptr->next
(3)c
2014/9/5
19
9.3
2014/9/5
21
9.3.1
struct node
{
int data;
node * next;
node * pre;
};
2014/9/5
//
//
22
9.3.1
28
hea
d
2014/9/5
NUL
L
52
96
NUL
L
23
9.3.2
2014/9/5
24
25
26
9.3.2
2014/9/5
27
7
data
void outputBidirList(node * head) {
cout << "List: ";
node *curNode = head;
while ( curNode ) {
if (curNode ->pre)
cout << " <- ";
cout << curNode->data;
if (curNode ->next)
cout << " -> ";
curNode = curNode ->next;
}
cout << endl;
return;
}
2014/9/5
28
9.3.2
2014/9/5
29
8n
()
node * insertData(int n, node * head) {
node *curNode = head; //
node *preNode = NULL; //
node *newNode = NULL; //
//
while((curNode != NULL)&&(curNode->data < n)) {
preNode = curNode;
curNode = curNode->next;
}
newNode = new node ; //
if (newNode == NULL)
{//
cout << "Not memory available!";
return head;
2014/9/5
30
}
8n
()
newNode->data = n;
if(preNode==NULL) { //
newNode->next = curNode;
newNode->pre = NULL;
if (curNode != NULL)
curNode->pre = newNode;
return newNode;
}
if (curNode == NULL)
{ //
newNode->pre = preNode;
preNode->next = newNode;
newNode->next = NULL;
return head;
2014/9/5
}
31
8n
()
else
{//
preNode->next = newNode;
newNode->next = curNode;
newNode->pre = preNode;
curNode->pre = newNode;
return head;
}
}
2014/9/5
32
curNode = curNode->next;
if (curNode == NULL) {
cout<<"Can't find "<< n << endl;
return head;
}
2014/9/5
33
if (curNode->pre == NULL) {
head = head->next;
head->pre = NULL;
}
else {
curNode->pre->next = curNode->next;
if (curNode->next != NULL)
curNode->next->pre=curNode->pre;
}
delete curNode;
return head;//
} 2014/9/5
34
2014/9/5
35
2014/9/5
9.1
9.2
9.6
36