?? 7-2-1.cpp
字號:
#include <iostream.h>
struct student {
int num;
float score;
struct student *next;
}; // 學生節點定義
int n = 0;
struct student *create(void)
{ struct student *head, *p1, *p2;
p1 = p2 = new struct student;
cin >> p1->num >> p1->score;
head = NULL;
while(p1->num != 0) {
n ++;
if(n == 1) head = p1;
else p2->next = p1;
p2 = p1;
p1 = new struct student;
cin >> p1->num >> p1->score;
}
p2->next = NULL;
return(head);
}
void print(struct student *head)
{ struct student *p = head;
cout << "鏈表數據如下:" << endl;
if(head != NULL)
do {
cout << p->num << ", " << p->score << endl;
p = p->next;
} while(p!=NULL);
}
struct student *deletelist(struct student *head, int num)
{ struct student *p1, *p2;
if(head == NULL) {
cout << "This list is NULL" << endl;
return(NULL);
}
p1 = head;
while(p1->num != num && p1->next != NULL){
p2 = p1;
p1 = p1->next;
}
if(p1->num == num) {
if(p1 == head) head = p1->next; // 刪除首結點
else p2->next = p1->next; // 刪除中間或尾結點
cout << "delete:" << num << endl;
delete p1;
} else cout << num << " not been found !" << endl;
return(head);
}
struct student *insert(struct student *head, struct student *stud)
{ struct student *p0, *p1, *p2;
p1 = head; p0 = stud;
if(head == NULL) {
head = p0; p0->next = NULL; return(head);
}
while((p0->num > p1->num) &&
(p1->next != NULL)) {
p2 = p1; p1 = p1->next;
}
if(p0->num <= p1->num) {
if(head == p1) head = p0; // 插在頭部 //
else p2->next = p0; // 插在 p2和p1之間 //
p0->next = p1;
} else { // 插到最后 //
p1->next = p0; p0->next = NULL; }
return(head);
}
main()
{ struct student *head, *p0;
head = create();
print(head);
deletelist(head, 1006);
print(head);
p0 = new struct student;
cin >> p0->num >> p0->score;
insert(head, p0);
print(head);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -