作业/2024/1/25

1.双向链表按任意位置插入

作业/2024/1/25_第1张图片

2.双向链表按照任意位置删除

作业/2024/1/25_第2张图片

主函数代码:

#include "head.h"

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

head.h

#ifndef _HEAD_H_
#define _HEAD_H_

#include 
#include 
#include 
//
//定义双向链表
typedef char datatype;

typedef struct Node
{
	datatype data;//定义存储元素
	struct Node *next;//定义下一个节点地址
	struct Node *prev;//定义上一个节点地址
}*Doublelink;



Doublelink insert_head(Doublelink head,datatype element);

Doublelink output(Doublelink head);

Doublelink insert_rear(Doublelink head,datatype element);

Doublelink delete_head(Doublelink head);

Doublelink delete_rear(Doublelink head);

int  length_Doublelink(Doublelink head);//计算双向链表长度的函数

int find_key(Doublelink head,int key);//输入位元素查找到相同元素的位置

Doublelink insert_pos_element(Doublelink head,int pos,datatype element);

Doublelink delete_pos(Doublelink head,int pos);
#endif

test.c

#include "head.h"

//创建
Doublelink create()
{
	Doublelink s=(Doublelink)malloc(sizeof(struct Node));
	if(s==NULL)
		return NULL;

	s->data=0;
	s->next=s->prev=s;
	return s;	

	

}

//头插
Doublelink insert_head(Doublelink head,datatype element)
{
	Doublelink s=create();
	s->data=element;

	if(NULL==head)
		head=s;
	
	else 
	{
	Doublelink rear=head->prev;
	s->next=head;
	head->prev=s;
	head=s;
	head->prev=rear;
	rear->next=head;
	}
	return head;
}

//循环输出
Doublelink output(Doublelink head)
{
	if(NULL == head)
	{
		puts("empty");
		return head;
	}

	else
	{
	Doublelink p=head;
	while(p->next!=head)
	{
	printf("%-5c",p->data);
	p=p->next;
	}
	printf("%-5c\n",p->data);



	do
	{
	printf("%-5c",p->data);
	p=p->prev;
	}while(p!=head->prev);
	puts("");
	}

}
//尾部插入
Doublelink insert_rear(Doublelink head,datatype element)
{
	Doublelink s=create();
	s->data=element;

	if(NULL==head)
		head=s;
	

	else
	{

	Doublelink p=head->prev;
	p->next=s;
	s->prev=p;
	s->next=head;
	head->prev=s;
	}
	return head;	
}

//头删
Doublelink delete_head(Doublelink head)
{
	if(head==NULL)
		return head;
	if(head->next==head)
	{
	free(head);
	head=NULL;
	return head;
	}
	
	
	Doublelink rear=head->prev;
	Doublelink del=head;
	head=head->next;
	head->prev=rear;
	rear->next=head;
	free(del);
	del=NULL;
	return head;
	

}

//尾删
Doublelink delete_rear(Doublelink head)
{
	if(head==NULL)
		return head;
	 if(head->next==head)
	{
	free(head);
	head=NULL;
	return head;
	}
	else
	{
		Doublelink p=head->prev;
						
		p->prev->next=head;
		head->prev=p->prev;
		free(p);
			p=NULL;
	return head;
	}

}
//定义双向链表长度位置的函数
int length_Doublelink(Doublelink  head)
{
	int len=0;
	Doublelink p=head;
	while(p->next!=head)
	{
	len++;
	p=p->next;
	}
	return len+1;
}
//定义按照所给的元素查找位置
int find_key(Doublelink head,int key)
{
	if(head==NULL)
		return 0;

	int len=length_Doublelink(head);
	Doublelink p=head;
	for(int i=1;i<=len;i++)
	{
		if(p->data==key)
		{
			printf("position(%d)=key\n",i);
			//return i;
		}
		p=p->next;
	}

}


//双向链表按任意位置插入
Doublelink insert_pos_element(Doublelink head,int pos,datatype element)
{
	if(pos<1||pos>length_Doublelink(head)+1)
	{
		puts("position ERROR");
		return head;
	}


	Doublelink s=create();
	s->data=element;



	if(pos==1)
	{
	head=insert_head(head,element);
	return head;
	}

	Doublelink p=head;
	for(int i=1;inext;
	}
	s->prev=p;
	s->next=p->next;

	p->next->prev=s;
	p->next=s;

	return head;
}




//双向链表按照任意位置删除
Doublelink delete_pos(Doublelink head,int pos)
{
	if(pos<1||pos>length_Doublelink(head))
	{
		puts("ERROR");
	}
	if(head==NULL)//链表为空返回null
		{
		 return head;
		}
	if(pos==1)
	{
		delete_head;
		return head;
	}

	Doublelink p=head;
	for(int i=1;inext;
	}

	Doublelink del=p->next;
	p->next=del->next;
	del->next->prev=p;
	free(del);
	del=NULL;
	return head;

}

作业/2024/1/25_第3张图片

你可能感兴趣的:(c语言)