⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ensure.txt

📁 Ensure the integrity/ Ensure the integrity
💻 TXT
字号:
/*诚信保证:"我保证没有抄袭他人的作业!"*/
#include"malloc.h"
#include"stdio.h"
#define LEN sizeof(struct node)
struct node
{
 int data;
 struct node*next;
};
 int n;                                    /*n为结点个数*/
 struct node*setup()					   /*建立链表函数*/
{struct node *head,*p,*q;
        n=0;
        p=(struct node*)malloc(LEN);	   /*建立新结点*/
        scanf("%d",&(p->data));			   /*输入一结点数据赋给p所指结点*/
        head=NULL;
 while(p->data!=0)						   /*输入一结点数据不为零时*/
{n++;
 if(n==1)head=p;						   /*把p所指结点作第一个结点*/
 else q->next=p;						   /*把p所指结点连结到表尾*/
        q=p;							   /*q移至表尾*/
        p=(struct node*)malloc(LEN);
        scanf("%d",&(p->data));
}
        q->next=NULL;					   /*表尾结点的指针变量置空*/
return(head);
}
struct node*del(struct node*head,int data) /*删除链表的结点函数*/
{struct node *p,*q;
 if(head==NULL)
 {printf("\nlist null!\n");}
else
{p=head;
 while(data!=p->data&&p->next!=NULL)
 {q=p;p=p->next;}
if(data==p->data)
{
	if(p==head)
		head=p->next;
	else
		q->next=p->next;
	    printf("delete:%d\n",data);
	    n=n-1;
}
else 
        printf("%d not been found!\n",data);
}
return(head);
}
void print(struct node*head)				/*输出链表函数*/
{
	struct node*m;
	    printf("new,there %d records are:\n",n);
	    m=head;
	while(m!=NULL)
	{printf("%d\n",m->data);
	    m=m->next;
	}
}
struct node*insert(struct node*head,struct node*stud)/*插入结点数据*/
{
	struct node *p0,*p,*q;
	    p=head;
	    p0=stud;
	if(head==NULL)
	{head=p0;
	    p0->next=NULL;}
	else while((p0->data>p->data)&&(p->next!=NULL))
	{q=p;p=p->next;}
	if(p0->data<p->data)
	{
		if(head==p)
			head=p0;
		else q->next=p0;
		p0->next=p;
	}
	else
	{p->next=p0;p0->next=NULL;}
	    n=n+1;
	return(head);
}
void main()
{
	struct node*head,stud;
	int x;
	    printf("input records:\n");
	    head=setup();				         /*建立链表,返回头指针*/
	    printf("\ninput the delete number:");
	    scanf("%d",&x);			             /*输入要删除的结点数据*/
	    head=del(head,x);		             /*删除后的头地址*/
 	    print(head);			             /*输出全部结点*/
	    printf("\ninput the inserted record:");
	    scanf("%d",&stud.data);				 /*输入要插入的记录*/
	    head=insert(head,&stud);			 /*插入如记录后的头地址*/
	    print(head);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -