#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student{
int num;
double score;
struct student *next;
};
//創建一個鏈表
struct student * create(void){
struct student *p1,*p2,*head;
int n = 0;
p1 = p2 = (struct student *)malloc(LEN);
scanf("%d%lf",&(p1->num),&(p1->score));
while(p1->num!=0){
n++;
if(n==1){
head = p1;
}else{
p2 = p1;
}
p1 = (struct student *)malloc(LEN);
scanf("%d%lf",&(p1->num),&(p1->score));
p2->next = p1;
}
p2->next = NULL;
return head;
}
struct student *delw(struct student *start,int num){
struct student *p1,*p2;
//鏈表為空
if(start==NULL){
printf("\nlist null!\n");
return NULL;
}
p1 = start;
p2 = NULL;
//鏈表不為空
//鏈表只有一個元素,且即為所要找的元素
/*if(p1->next==NULL&&p1->num == num){
printf("there is only one element and that is it!");
return NULL;
}*/
while(p1->next!=NULL&&p1->num!=num){
p2 = p1;
p1 = p1->next;
}
if(num == p1->num){
if(p1 == start){
return start->next;
}else{
p2->next = p1->next;
}
}else{
printf("number not found!");
}
return start;
}
struct student *del(struct student *head,long num){
struct student *p1, *p2;
//鏈表為空
if(head == NULL){
printf("\nlist null!\n");
return head;
}
//鏈表不為空
p1 = head;
while(p1->next!=NULL&&num!=p1->num){
p2 = p1;
p1 = p1->next;
}
if(num == p1->num){
if(p1 == head){
head = p1->next;
}else{
p2->next = p1->next;
printf("delete:%ld\n",num);
}
}else
printf("%ld not been found!\n",num);
return head;
}
//刪除一個節點
struct student * deleteNode(struct student *start,int num){
struct student *p1, *p2,*before,*after;
//空表
if(start==NULL){
printf("the linktable is null");
return NULL;
}
p1 = p2 = start;
//只有一個節點
if(start->next==NULL){
if(start->num==num){
return NULL;
}
}
//鏈表不為空(兩個以上的節點)
//1:鏈表的第一個即為所要找的
if((start->num == num)&&(start->next!=NULL)){
return start->next;
}
while(p1!=NULL){
if(p1->num==num){
before = p2;
after = p1->next;
}
p2 = p1;
p1 = p1->next;
}
before->next = after;
return start;
}
struct student * insert(struct student *head,struct student *stu){
struct student *p1,*p2, *p0;
p1 = head;
p0 = stu;
//鏈表為空
if(head == NULL){
head = p0;
p0->next = NULL;
printf("the link is null\n");
return NULL;
//鏈表不為空,比較num,(如22, 33, 55),44應插入至33后面
}else{
//一個元素
//printf("head.next is not null");
//兩個以上元素: 22 55 88
while(p1->num<p0->num&&p1->next!=NULL){
p2 = p1;
p1 = p1->next;
}
if(p1->num>p0->num){//
if(p1==head){//p0排到最前
head = p0;
p0->next = p1;
}else{
//p0排到中間
p2->next = p0;
p0->next = p1;
}
}else{//p0排到最后
p1->next = p0;
p0->next = NULL;
}
return head;
}
}
void printLink(struct student *p){
struct student *p_afterDeal = p;
p_afterDeal = p;
while(p_afterDeal!=NULL){
printf("num = %d, score = %lf\n",p_afterDeal->num,p_afterDeal->score);
p_afterDeal = p_afterDeal->next;
}
}
int main(void){
struct student * p_std, * p_afterDel,*p,*p_afterInsert;
struct student newstd = {44,44.4,NULL};
//創建一個鏈表
printf("創建一個鏈表:\n");
p_std = create();
printf("創建的鏈表如下:\n");
printLink(p_std);
//插入一個節點
printf("插入一個節點:44\n");
p = &newstd;
p_afterInsert = insert(p_std,p);
printf("插入一個節點后的列表如下:\n");
printLink(p_afterInsert);
//刪除一個節點
printf("刪除一個節點:44\n");
p_afterDel = deleteNode(p_afterInsert,44);
printf("刪除一個節點后的列表如下:\n");
printLink(p_afterDel);
system("pause");
}