421作业

1.双向链表按位置删除、修改、查找

//head.h头文件
#ifndef __HEAD_H__
#define __HEAD_H__

#include <stdio.h>
#include <stdlib.h>

typedef char datatype;
//定义双向链表结构体
typedef struct Node
{
	union{
		int len;//头节点的数据域:链表长度
		datatype data;//普通节点的数据域:数据元素
	};
	//指针域:下一个节点的地址
	struct Node *next;
	//指针域:上一个节点的地址
	struct Node *prev;
}*doubleLink;

doubleLink create(int flag);

int insert_pos(doubleLink L,int pos,datatype e);
int delete_pos(doubleLink L,int pos);
int update_pos(doubleLink L,int pos,datatype e);
char search_pos(doubleLink L,int pos);
int delete_rear(doubleLink L);
int delete_head(doubleLink L);
int insert_rear(doubleLink L,datatype e);
void output(doubleLink L);
int insert_head(doubleLink L,datatype e);
#endif

//功能函数
#include "head.h"
/*
 * function:    创建双向链表节点
 * @param [ in] flag=1:创建头节点
 * @param [out] flag=0:创建普通节点
 * @return      成功返回地址  失败返回NULL
 */
doubleLink create(int flag)
{
	doubleLink L=(doubleLink)malloc(sizeof(struct Node));
	if(L==NULL)
	{
		return NULL;
	}
	if(flag==1)
	{
	L->len=0;
	}
	else if(flag==0)
	{
		L->data=0;
	}
	L->next=NULL;
	L->prev=NULL;
	return L;
}
/*
 * function:    头插入
 * @param [ in] 
 * @param [out] 链表  插入的值
 * @return      成功返回0 失败返回-1
 */
int insert_head(doubleLink L,datatype e)
{
	//1,判断链表是否创建成功
	if(L==NULL)
	{
		return -1;
	}
	//2,创建新节点s
	doubleLink s=create(0);
	if(s==NULL)
	{
		return -1;
	}
	//s的数据域
	s->data=e;
	//s的指针域
	s->next=L->next;
	s->prev=L;

	if(L->next!=NULL)
	{
		L->next->prev=s;
	}
	L->next=s;
	L->len++;
	return 0;
	
}
/*
 * function:    输出
 * @param [ in] 
 * @param [out] 链表
 * @return      无
 */
void output(doubleLink L)
{
	//1,判断链表是否创建成功
	//2,判断链表是否为空
	if(L==NULL || L->next==NULL)
	{
		return ;
	}
	//3,输出
	printf("正向遍历\n");
	doubleLink p=L;
	while(p->next!=NULL)
	{
		p=p->next;
		printf("%c\t",p->data);
	}
	printf("\n逆向遍历\n");
	while(p->prev!=NULL)
	{
		printf("%c\t",p->data);
		p=p->prev;
	}
	puts("");
}
/*
 * function:    尾部插入
 * @param [ in] 
 * @param [out] 链表  插入的值
 * @return      成功返回0 失败返回-1
 */
int insert_rear(doubleLink L,datatype e)
{
	//1,判断链表是否创建成功
	if(L==NULL)
	{
		return -1;
	}
	//2,循环最后一个节点
	doubleLink p=L;
	while(p->next!=NULL)
	{
		p=p->next;
	}
	//3,插入新节点s
	doubleLink s=create(0);
	if(s==NULL)
	{
		return -1;
	}
	//s的数据域
	s->data=e;
	//s的指针域
	s->next=p->next;
	s->prev=p;
	p->next=s;
	L->len++;
	return 0;
}
/*
 * function:    头删除
 * @param [ in] 
 * @param [out] 链表
 * @return      成功返回0 失败返回-1
 */
int delete_head(doubleLink L)
{
	//1,判断链表是否创建成功
	//2,判断链表是否为空
	if(L==NULL ||L->next==NULL)
	{
		return -1;
	}
	//3,删除L->next
	doubleLink q=L->next;
	L->next=q->next;
	if(q->next!=NULL)
	{
		q->next->prev=L;
	}
	free(q);
	q=NULL;
	L->len--;
}
/*
 * function:    尾部删除
 * @param [ in] 
 * @param [out] 链表
 * @return      成功返回0 失败返回-1
 */
int delete_rear(doubleLink L)
{
	//1,判断链表是否创建成功
	//2,判断链表是否为空
	if(L==NULL ||L->next==NULL)
	{
		return -1;
	}
	//3,循环到最后一个节点
	doubleLink p=L;
	while(p->next!=NULL)
	{
		p=p->next;
	}
	p->prev->next=NULL;
	free(p);
	p=NULL;
	L->len--;
}
/*
 * function:    按位置插入
 * @param [ in] 链表 插入的位置 
 * @param [out] 插入的值
 * @return      成功返回0 失败返回-1
 */
int insert_pos(doubleLink L,int pos,datatype e)
{
	//1,判断链表是否创建成功
	//2,判断位置是否合法
	if(L==NULL || pos<1 || pos>L->len+1)
	{
		return -1;
	}
	//3,循环pos-1,起名字p
	doubleLink p=L;
	for(int i=0;i<pos-1;i++)
	{
		p=p->next;
	}
	//4,创建新节点s
	doubleLink s=create(0);
	if(s==NULL)
	{
		return -1;
	}
	//s的数据域
	s->data=e;
	//s的指针域
	s->next=p->next;
	s->prev=p;
	if(p->next!=NULL)
	{
		p->next->prev=s;
	}
	p->next=s;
	L->len++;
	return 0;

}
/*
 * function:    按位置删除
 * @param [ in] 链表 删除的位置
 * @param [out] 
 * @return      成功返回0,失败返回-1
 */
int delete_pos(doubleLink L,int pos)
{
	//1,判断链表是否创建成功
	//2,判断位置是否合法
	if(L==NULL || pos<1 || pos>L->len)
	{
		return -1;
	}
	//3,循环pos-1,起名字p
	doubleLink p=L;
	for(int i=0;i<pos-1;i++)
	{
		p=p->next;
	}
	//4,删除p->next
	doubleLink q=p->next;
	if(q->next!=NULL)
	{
		p->next=q->next;
		q->next->prev=p;
	}
	else
	{
		q->prev->next=NULL;
	}
	free(q);
	q=NULL;
	L->len--;
	return 0;
}
/*
 * function:    按位置修改
 * @param [ in] 链表 修改的位置 修改的值
 * @param [out] 
 * @return      成功返回0,失败返回-1
 */
int update_pos(doubleLink L,int pos,datatype e)
{
	int a=delete_pos(L,pos);
	int b=insert_pos(L,pos,e);
	if(a&&b==0)
	{
		return 0;
	}
	else
	{
		return -1;
	}
}
/*
 * function:    按位置查找
 * @param [ in] 链表 查找的位置
 * @param [out] 
 * @return      成功返回查找的值,失败返回NULL
 */
char search_pos(doubleLink L,int pos)
{
	//1,判断链表是否创建成功/是否为空
	//2,判断位置是否合法
	if(L==NULL ||L->next==NULL|| pos<1 || pos>L->len)
	{
		return 0;
	}
	//3,循环pos,起名字p
	doubleLink p=L;
	for(int i=0;i<pos;i++)
	{
		p=p->next;
	}
	return p->data;
}

//main.c主函数
#include "head.h"

int main(int argc, const char *argv[])
{
	doubleLink L=create(1);
	
	//头插入
	int n;
	datatype e;
	printf("请输入数据元素的个数:");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		printf("请输入%d个元素:",i+1);
		scanf(" %c",&e);
		//insert_head(L,e);
		insert_rear(L,e);
	}
	
	output(L);

	//头删除
	//delete_head(L);
	//delete_head(L);
	//output(L);
	 
	//尾部删除
//	delete_rear(L);
//	delete_rear(L);
//	output(L);
#if 0
	int pos;
	printf("请输入插入的位置:");
	scanf("%d",&pos);
	printf("请输入插入的值:");
	scanf(" %c",&e);
	insert_pos(L,pos,e);
	output(L);

	int pos;
	//按位置删除
	printf("请输入删除的位置:");
	scanf("%d",&pos);
	delete_pos(L,pos);
	output(L);
	
	int pos;
	//按位置修改
	printf("请输入修改的位置:");
	scanf(" %d",&pos);
	printf("请输入修改的值:");
	scanf(" %c",&e);
	puts("");
	update_pos(L,pos,e);
	output(L);
#endif
	//按位置查找
	int pos;
	printf("请输入查找的位置:");
	scanf(" %d",&pos);
//	puts("");
	char c=search_pos(L,pos);
	printf("查找的值为%c\n",c);
		
	return 0;
}

2.双向循环链表头插,头删,尾删

//head.h 头文件
#ifndef __HEAD_H__
#define __HEAD_H__

#include <stdio.h>
#include <stdlib.h>
typedef float datatype;
typedef struct Node
{
	union{
		int len;
		datatype data;
	};
	struct Node *next;
	struct Node *prev;
}*loopDobleLink;


loopDobleLink create(int flag);
void output(loopDobleLink L);

int insert_rear(loopDobleLink L,datatype e);
int insert_head(loopDobleLink L,datatype e);
int delete_head(loopDobleLink L);
int delete_rear(loopDobleLink L);

#endif

//功能函数
#include "head.h"
/*
 * function:    创建节点
 * @param [ in] 0:普通
 * @param [out] 1:头
 * @return      成功返回地址  失败返回NULL
 */
loopDobleLink create(int flag)
{
	loopDobleLink L=(loopDobleLink)malloc(sizeof(struct Node));
	if(L==NULL)
	{
		return NULL;
	}
	if(flag==1)
	{
	L->len=0;
	L->next=L;
	L->prev=L;
	}
	else if(flag==0)
	{
		L->data=0;
		L->next=NULL;
		L->prev=NULL;
	}
	return L;
}
/*
 * function:    尾部插入
 * @param [ in] 链表
 * @param [out] 插入的值
 * @return      成功返回0 失败返回-1
 */
int insert_rear(loopDobleLink L,datatype e)
{
	//1,判断链表是否创建成功
	if(L==NULL)
	{
		return -1;

	}
	//2,找到最后一个节点
	loopDobleLink p=L->prev;
	//3,插入新节点s
	loopDobleLink s=create(0);
	if(s==NULL)
	{
		return -1;
	}
	s->data=e;
	s->next=p->next;
	s->prev=p;
	p->next->prev=s;
	p->next=s;
	L->len++;
	return 0;
}
/*
 * function:    头部插入
 * @param [ in] 链表
 * @param [out] 插入的值
 * @return      成功返回0 失败返回-1
 */
int insert_head(loopDobleLink L,datatype e)
{
	//1,判断链表是否创建成功
	if(L==NULL)
	{
		return -1;

	}
	//2,找到第一个节点
	loopDobleLink p=L->next;
	//3,插入新节点s
	loopDobleLink s=create(0);
	if(s==NULL)
	{
		return -1;
	}
	s->data=e;
	s->next=p;
	s->prev=L;
	p->prev=s;
	L->next=s;
	L->len++;
	return 0;
}
/*
 * function:    头删
 * @param [ in] 链表
 * @param [out] 
 * @return      成功返回0,失败返回-1
 */
int delete_head(loopDobleLink L)
{
	//1,判断链表是否创建成功
	//2,判断链表是否为空
	//L->next=L L->prev==L L->len==0
	if(L==NULL ||L->next==L)
	{
		return -1;
	}
	//3,找到第一个节点
	loopDobleLink p=L->next;
	L->next=p->next;
	p->next->prev=L;
	free(p);
	p=NULL;
	L->len--;
	return 0;
}
int delete_rear(loopDobleLink L)
{
	//1,判断链表是否创建成功
	//2,判断链表是否为空
	//L->next=L L->prev==L L->len==0
	if(L==NULL ||L->next==L)
	{
		return -1;
	}
	//3,找到最后一个节点
	loopDobleLink p=L->prev;
	L->prev=p->prev;
	p->prev->next=L;
	free(p);
	p=NULL;
	L->len--;
	return 0;
}
/*
 * function:    遍历
 * @param [ in] 链表
 * @param [out] 
 * @return      无
 */
void output(loopDobleLink L)
{
	//1,判断链表是否创建成功
	//2,判断链表是否为空
	//L->next=L L->prev==L L->len==0
	if(L==NULL ||L->next==L)
	{
		return ;
	}
	printf("正向遍历\n");
	loopDobleLink p=L;
	while(p->next!=L)
	{
		p=p->next;
		printf("%.1f\t",p->data);
	}
	printf("\n逆向遍历\n");
	while(p->prev!=L->prev)
	{
		printf("%.1f\t",p->data);
		p=p->prev;
	}
	puts("");
}

//main.c主函数
#include "head.h"
int main(int argc, const char *argv[])
{
	loopDobleLink L=create(1);
	
	int n;
	datatype e;
	printf("请输入元素的个数:");
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		printf("请输入插入的元素:");
		scanf("%f",&e);
	//尾插
	//insert_rear(L,e);
	//头插
		insert_head(L,e);
	}
	output(L);
	//头删
//	printf("头删\n");
//	delete_head(L);
//	output(L);
	//尾删
	printf("尾删\n");
	delete_rear(L);
	output(L);
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值