数据结构day5作业

头文件:

#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 creat();
Doublelink insert_head(Doublelink head,datatype element);
void output(Doublelink head);
Doublelink insert_rear(Doublelink head,datatype element);
Doublelink delete_head(Doublelink head);
Doublelink delete_rear(Doublelink head);
int length(Doublelink head);
Doublelink insert_pos(Doublelink head,int pos,datatype element1);
Doublelink delete_pos(Doublelink head,int pos);


#endif

主函数 :

#include"head.h"

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

自定义函数: 

#include"head.h"

//创建新节点
Doublelink creat()
{
	Doublelink s=(Doublelink)malloc(sizeof(struct Node));
	if(s==NULL)
		return NULL;
	s->data='\0';
	s->next=s->prev=NULL;
	return s;
}

//头插
Doublelink insert_head(Doublelink head,datatype element)
{
	//创建新节点
	Doublelink s=creat();
	s->data=element;
	//判空
	if(NULL==head)
		head=s;
	else
	{
		//头插
		s->next=head;
		head->prev=s;
		head=s;
	}
	return head;
}

//输出
void output(Doublelink head)
{
	if(head==NULL)
	{
		puts("empty");
		return;
	}
	Doublelink p=head;
	//正向输出
	puts("forward");
	while(p->next)
	{
		printf("%c  ",p->data);
		p=p->next;
	}
	printf("%c  ",p->data);
	puts("");
	//反向输出
/*
	puts("reverse");
	while(p)
	{
		printf("%c  ",p->data);
		p=p->prev;
	}
	puts("");
*/
}



//尾插

Doublelink insert_rear(Doublelink head,datatype element)
{
	Doublelink s=creat();
	s->data=element;

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

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

//尾删

Doublelink delete_rear(Doublelink head)
{
	if(NULL==head)
		return head;
	Doublelink p=head;
	if(p->next==NULL)
	{
		free(head);
		head=NULL;
		return head;
	}
	while(p->next)
	{
		p=p->next;
	}
	p->prev->next=NULL;
	free(p);
	p=NULL;
	return head;

}

//计算长度
int length(Doublelink head)
{
	int len=0;
	Doublelink p=head;
	while(p)
	{
		len++;
		p=p->next;
	}
	return len;
}




//任意位置插入

Doublelink insert_pos(Doublelink head,int pos,datatype element1)
{
		//头位置插入
	if(pos==1)
	{
		head=insert_head(head,element1);
		return head;
	}
	else if(pos>1 && pos <=length(head))
	{
	
		//创建新节点
		Doublelink s=creat();
		s->data=element1;
		//中间位置插入
		Doublelink p=head;
		for(int i=1;inext;
		}
		s->next=p->next;
		s->prev=p;
		p->next->prev=s;
		p->next=s;
		return head;
	}
	//尾位置插入
	else if(pos==length(head)+1)
	{
		head=insert_rear(head,element1);
		return head;
	}
	//位置不合法
	else
	{
		puts("pos error");
		return head;
	}
}
//任意位置删除

Doublelink delete_pos(Doublelink head,int pos)
{
	if(head==NULL || pos<1 || pos>length(head))
	{
		puts("error");
		return head;
	}
	if(pos==1)
	{
		head=delete_head(head);
		return head;
	}
	else if(pos>1 && posnext;
		}
		del=p->next;
		p->next=del->next;
		del->next->prev=p;
		free(del);
		del=NULL;
		return head;

	}
	else if(pos==length(head))
	{
		head=delete_rear(head);
		return head;
	}
/*
	else
	{
		puts("error");
		return head;
	}
*/
}

1、双链表按任意位置插入

数据结构day5作业_第1张图片

2、双链表按任意位置删除 

数据结构day5作业_第2张图片

思维导图: 

数据结构day5作业_第3张图片

你可能感兴趣的:(数据结构)