#include<stdio.h>
#include<stdlib.h>
struct Node{
int num;
struct Node *next;
};
int main(){
struct Node *create();
struct Node *insert(struct Node *head,struct Node *p0);
struct Node *del(struct Node *head);
void print(struct Node *head);
struct Node *head;
struct Node date;
printf("请输入插入的结点:");
scanf("%d",&date.num);
head=create();
head=insert(head,&date);
head=del(head);
print(head);
return 0;
}
struct Node *create(){
struct Node *head,*p,*q;
int n=0;
p=(struct Node *)malloc(sizeof(struct Node));
scanf("%d",&p->num);
while(p->num!=0){
n++;
if(n==1){
head=p;
}else{
q->next=p;
}
q=p;
p=(struct Node *)malloc(sizeof(struct Node));
scanf("%d",&p->num);
}
q->next=NULL;
return head;
}
struct Node *insert(struct Node *head,struct Node *p0){
struct Node *p,*q;
p=head;
while(p->next!=NULL&&p->num<p0->num){
q=p;
p=p->next;
}
if(p->num>=p0->num){
if(head==p){
head=p0;
p0->next=p;
}else{
q->next=p0;
p0->next=p;
}
}else{
p->next=p0;
p0->next=NULL;
}
return head;
}
struct Node *del(struct Node *head){
int num;
struct Node *p,*q;
p=head;
printf("输入删除的结点");
scanf("%d",&num);
while(p!=NULL&&p->num!=num){
q=p;
p=p->next;
}
if(p==head){
head=p->next;
}else{
q->next=p->next;
}
return head;
}
void print(struct Node *head){
struct Node *p=head;
do{
printf("%d",p->num);
p=p->next;
}while(p!=NULL);
}
输入示例:
运行结果:
只能实现插入一个结点,删除一个结点,所以输入第一个插入数据后,输入的是你的节点数据,并且以0为结束标志。
注意点:因为本人在编写时,将要插入的数据直接定义在函数里了,这样做是不对的,因为函数里定义的都是形式参数,所以带不回原来的链表中,因此很容易发生差错。
感谢您的阅读!编写不易,欢迎批评指正!