You are on page 1of 36

9

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;
};

student *p=(student*) malloc(sizeof(student));


student * p = new student;

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

for ( int i = 0; i < n - 1; i ++) {


cin >> num;
temp = new node ; //
if (temp == NULL) {
cout << "No memory available!";
return head;
}
else {
temp->data = num;
temp->next = NULL;
tail->next = temp;
tail = temp;
}
}
return head ;

} 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

node * findData(int n, node * head)


{
node *curNode = head;
while ( curNode ) {
if ( curNode->data == n) {
cout<<"Find "<<n<<" in the list."<<endl;
return curNode;
}
curNode = curNode->next;
}
cout<<"Can't find "<<n<<" in the list."<<endl;
return NULL;
15
} 2014/9/5

9.2.2

ac
(1) cptrc,aptra
(2) ac
cptr->nextaptr->next
(3) ca
aptr->next=cptr;

2014/9/5

16

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 << "No memory available!";
return head;
}
2014/9/5

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

node * deleteData(int n, node * head) {


node *curNode = head;//
node *preNode = NULL;//
while (curNode && curNode->data != n) {
preNode = curNode; //
curNode = curNode->next;
}
if (curNode == NULL) {
cout<<"Can't find "<<n<<" in the list"<<endl;
return head;
}
if (preNode == NULL) //
head = head->next;
else
preNode->next = curNode->next;
delete curNode;
return head; //
20
} 2014/9/5

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

node *createBidirList (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;
head->pre = NULL;
tail = head;
}
2014/9/5

25

for ( int i = 0; i < n - 1; i ++) {


cin >> num;
temp = new node ; //
if (temp == NULL) {
cout << "No memory available!";
return head;
}
else {
temp->data = num;
temp->next = NULL;
temp->pre = tail;
tail->next = temp;
tail = temp;
}
}
return head ;
2014/9/5

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

node * deleteData(int n, node * head) {


node *curNode = head; //
while ( curNode && curNode->data != n)

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

You might also like