编辑代码

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>


//1,1,2,3,5

struct A {
	int a;
	struct A* next;

};

struct A * creat(struct A *head) {

	struct A* p1,*p2;
	p1 = (struct A*)malloc(sizeof(struct A));
	p2 = (struct A*)malloc(sizeof(struct A));
	head = NULL;
  //  fflush(stdin);
	scanf("%d",&p1->a);
	//getchar();
	int n = 0;
	while (p1->a!=0) {
	
		if (n==0) {
			head = p1;
		}
		else {
			p2->next = p1;
			p2 = p1;
			p1 = (struct A*)malloc(sizeof(struct A));
		//	fflush(stdin);
			scanf("%d", &p1->a);
			//getchar();
		}
		n++;

	}
	p2->next = NULL;

return head;
}




//struct  A* creat(struct A* head) {
//	//p1(赋值),p2,head初始化
//	struct A* p1, * p2; int n = 0;
//	p1 = (struct A*)malloc(sizeof(struct A));
//	p2 = (struct A*)malloc(sizeof(struct A));
//	head = NULL;
//	scanf("%d", &p1->a);
//
//	//第一次:p1给head
//	//p1给p2的下一节点,p1给p2。p1重新初始化
//	while (p1->a != 0) {//p2指针,值给p1
//		if (n == 0) {//第一次:head=p1;
//			head = p1;
//		}
//		else {
//			p2->next = p1;//上一个p1指向本p1
//			p2 = p1;//保存上一个p1
//			p1 = (struct A*)malloc(sizeof(struct A));
//			scanf("%d", &p1->a);
//		}
//		n++;
//	}
//	p2->next = NULL;
//	return head;
//}

struct A * insert(struct A *head,struct A *p){

    struct A *p1,*p2;
    p2=(struct A*)malloc(sizeof(struct A*));
    p1=head;

    while(p1->next!=NULL&&p->a>p1->a){
        p2=p1;
        p1=p1->next;
    }

    if(p1->next==NULL){
        p1->next=p;
        p->next=NULL;
    }else{
        p2->next=p;
        p->next=p1;
    }
    return head;
}

struct A* del(struct A * head,int aa){
    struct A * p1,*p2;
    p1=head;
    p2=(struct A *)malloc(sizeof(struct A));

    while(p1->next!=NULL&&aa!=p1->a){
        p2=p1;
        p1=p1->next;
    }

    if(p1==head){
        head=p1->next;
    }else{
        p2->next=p1->next;
    }
    return head;
}


void show(struct A *head) {
	struct A* p = head;

	do {
		printf("%d ",p->a);
		p = p->next;
	} while (p!=NULL);
	printf("\n");

}


int main() {

	struct A *head= (struct A*)malloc(sizeof(struct A));
	head=creat(head);
    	show(head);
        struct A *p= (struct A*)malloc(sizeof(struct A));
        p->a=33;
    head=insert(head,p);
	show(head);
    head=del(head,33);
    show(head);
}