2024.2.4作业

1、双向链表的头插、头删、尾插、尾删

#include
#include
#include
typedef int datatype;
typedef struct node
{
	//数据域
	datatype data;
	//指针域
	struct node *next;
	struct node *pre;
}*Doublelist;
Doublelist create()
{
	Doublelist s=(Doublelist)malloc(sizeof(struct node));
	if(s==NULL)
		return NULL;
	s->data=0;
	s->next=s->pre=NULL;
}
Doublelist head_insert(Doublelist head,datatype element)
{
	Doublelist s=create();
	s->data=element;
	if(head==NULL)
		head=s;
	else
	{
		s->next=head;
		head->pre=s;
		head=s;
	}
	return head;

}
Doublelist rear_insert(Doublelist head,datatype element)
{
	//创建节点
	Doublelist s=create();
	s->data=element;
	Doublelist p=head;
	if(head==NULL)
	{
		head=s;
		return head;
	}
	while(p->next)
	{
		p=p->next;
	}
	p->next=s;
	s->pre=p;
	return head;
}
Doublelist head_delete(Doublelist head)
{
	Doublelist del=head;
	if(head==NULL)
		return head;
	if(del->next==NULL)
	{
		free(del);
		del=NULL;
		return head;
	}
	head=head->next;
	head->pre=NULL;
	free(del);
	del=NULL;
	return head;
}
Doublelist rear_delete(Doublelist head)
{

	if(head==NULL)
		return head;
	if(head->next==NULL)
	{
		free(head);
		head=NULL;
		return head;
	}	
	Doublelist p=head;
	while(p->next)
	{
		p=p->next;
	}
	p->pre->next=NULL;
	free(p);
	p=NULL;
	return head;

}
void output(Doublelist head)
{
	Doublelist p=head;
	if(head==NULL)
	{
		puts("empty!");
		return;
	}
	while(p->next)
	{
		printf("%-5d",p->data);
		p=p->next;
	}
	printf("%-5d",p->data);
	puts("");
	return;
}
int main(int argc, const char *argv[])
{
	int n;
	datatype element;
	Doublelist head=NULL;
	printf("please enter n:");
	scanf("%d",&n);
	for(int i=0;i

2024.2.4作业_第1张图片

2、双向链表任意位置的插入、删除、修改、查找

#include
#include
#include
enum{FALSE=-1,SUCCESS};
typedef int datatype;
typedef struct node
{
	//数据域
	datatype data;
	//指针域
	struct node *next;
	struct node *pre;
}*Doublelist;
Doublelist create()
{
	Doublelist s=(Doublelist)malloc(sizeof(struct node));
	if(s==NULL)
		return NULL;
	s->data=0;
	s->next=s->pre=NULL;
}
Doublelist head_insert(Doublelist head,datatype element)
{
	Doublelist s=create();
	s->data=element;
	if(head==NULL)
		head=s;
	else
	{
		s->next=head;
		head->pre=s;
		head=s;
	}
	return head;

}
Doublelist rear_insert(Doublelist head,datatype element)
{
	//创建节点
	Doublelist s=create();
	s->data=element;
	Doublelist p=head;
	if(head==NULL)
	{
		head=s;
		return head;
	}
	while(p->next)
	{
		p=p->next;
	}
	p->next=s;
	s->pre=p;
	return head;
}
Doublelist head_delete(Doublelist head)
{
	Doublelist del=head;
	if(head==NULL)
		return head;
	if(del->next==NULL)
	{
		free(del);
		del=NULL;
		return head;
	}
	head=head->next;
	head->pre=NULL;
	free(del);
	del=NULL;
	return head;
}
Doublelist rear_delete(Doublelist head)
{

	if(head==NULL)
		return head;
	if(head->next==NULL)
	{
		free(head);
		head=NULL;
		return head;
	}	
	Doublelist p=head;
	while(p->next)
	{
		p=p->next;
	}
	p->pre->next=NULL;
	free(p);
	p=NULL;
	return head;

}
void output(Doublelist head)
{
	Doublelist p=head;
	if(head==NULL)
	{
		puts("empty!");
		return;
	}
	while(p->next)
	{
		printf("%-5d",p->data);
		p=p->next;
	}
	printf("%-5d",p->data);
	puts("");
	return;
}
int length(Doublelist head)
{
	int len=0;
	Doublelist p=head;
	while(p)
	{
		p=p->next;
		len++;
	}
	return len;
}
Doublelist any_insert(Doublelist head,int pos,datatype element)
{
	int len=length(head);
	if(pos<1||pos>len+1)
	{
		puts("pos error");
		return head;
	}
	Doublelist s=create();
	s->data=element;
	if(pos==1)
	{
		head=head_insert(head,element);
		return head;
	}
	Doublelist p=head;
	for(int i=1;inext;
	}
	s->next=p->next;
	s->pre=p;
	p->next->pre=s;
	s->pre=p;
	return head;

}
Doublelist any_delete(Doublelist head,int pos)
{
	datatype len=length(head);
	if(pos<1||pos>len||head==NULL)
	{
		puts("pos error");
		return head;
	}
	if(pos==1)
	{
		head=head_delete(head);
		return head;
	}
	Doublelist p=head;
	for(int i=1;inext;
	}
	Doublelist del=p->next;
	p->next=del->next;
	del->next->pre=p;
	free(del);
	del=NULL;
	return head;
	
}
void any_change(Doublelist head,int pos,datatype element)
{
	int len=length(head);
	if(pos<1||pos>len||head==NULL)
	{
		puts("error");
		return ;
	}
	Doublelist p=head;
	for(int i=1;inext;
	p->data=element;
	return;

}
int pos_search(Doublelist head,int pos)
{
	int len=length(head);
	Doublelist p=head;
	if(pos<1||pos>len||head==NULL)
	{
		puts("pos error");
		return FALSE;
	}
	for(int i=1;inext;
	return p->data;

}
int main(int argc, const char *argv[])
{
	int n;
	datatype element;
	Doublelist head=NULL;
	printf("please enter n:");
	scanf("%d",&n);
	for(int i=0;i

2024.2.4作业_第2张图片

3、说明栈和队列的区别

栈和队列的区别:栈是先进后出,可以在一端进行操作,逻辑连续物理不一定连续,队列是先进先出,在两端操作,内存连续

4、说明什么是内存泄露

内存泄漏:在释放的时候,指针没有指向首地址而是指向了中间的某一块地址

你可能感兴趣的:(算法)