数据结构(c语言实现)——双链表

//有头结点双链表
#include <malloc.h>
#include <string.h>
#include <stdio.h>
#define max 51

struct People
{
	char name[max];
	char ID[max];
};
typedef struct LNode {
	People data; //结点的数据域
	struct LNode* next; //结点的指针域(指向下一个元素)
	struct LNode* previous; //结点的指针域(指向前一个元素)
} LNode, * LinkList; //LinkList为指向结构体LNode的指针类型,这里Linklist 与LNote*等价

void printPeo(People peo);//输出people
int listLength(LinkList list);//链表的长度
void intiList(LinkList& list);//双链表初始化
LNode* GetElem(LinkList& list, int i);//按位查找元素
bool InsertLNode(LinkList& list, People peo , int i);//按位插入节点
bool DelLNode(LinkList& list, int i);//按位删除节点


void main()
{
	LinkList list;
	intiList(list);
	
	
	People peo[4] = { {"李参政" , "0418"} , { "参政" ,"0418"} , {"政" ,"0418"} ,{"李" ,"0418"} };

	for (int i = 0; i < 4; i++)
		InsertLNode(list, peo[i], 1);//依次插入peo[i]
	LNode* l;
	l = list;
	for (int i = 0; i < 4; i++)
	{
		l = l->next;
		printPeo(l->data);
	}
	for (int i = 0; i < 4; i++)
		DelLNode(list, 1);
	printf("%d\n", listLength(list));
	free(list);

}

void intiList(LinkList& list)//双链表初始化
{
	//申请头指针空间
	list = (LNode*)malloc(sizeof(LNode));
	
	//初始化表头
	list->previous = NULL;
	list->next = NULL;
}


LNode* GetElem(LinkList& list, int i)//按位查找元素
{
	if (i > listLength(list))//判断i 的有效性
		return NULL;
	if (i == 0)//输出头节点
		return list;
	
	LNode* p = list->next;
	for (int j = 0 ; j < listLength(list);i++)
	{
		p = p->next;
		++j;
	}
	return p;
}

bool InsertLNode(LinkList& list, People peo, int i)//按位插入节点
{
	int len = listLength(list);
	if (i > len+1 || i == 0)//判断i的有效性
		return false;

	//申请新的节点空间并初始化
	LNode* s = (LNode*)malloc(sizeof(LNode));
	s->data = peo;
	s->next = NULL;
	s->previous = NULL;

	//链表为空表,新节点插入到表头
	if (i == 1 && len == 0)
	{
		s->previous = list;
		list->next = s;
		return true;
	}
	//链表为非空表,新节点插入到表头
	if (i == 1 && len != 0)
	{
		s->next = list->next;
		list->next->previous = s;
		s->previous = list;
		list->next = s;
		return true;
	}
	//链表不空,新节点查到表尾
	if (i == len +1 && len != 0)
	{
		LNode* temp = GetElem(list ,len);
		temp->next = s;
		s->previous = temp;
		return true;
	}

	//链表非空,且不是插入到表头、表尾
	LNode* temp =list->next ;
	temp= GetElem(list, i-1);
	s->next = temp->next;
	temp->next->previous = s;
	s->previous = temp;
	temp->next = s;
	return true;
}

bool DelLNode(LinkList& list, int i)//按位删除节点
{
	int len = listLength(list);
	if (i > len || len == 0) //判断i的有效行
		return false;
	LNode* temp = NULL;
	if (len == 1)
	{
		temp = GetElem(list, 1);
		free(temp);
		list->next = NULL;
		return true;
	}
	temp = GetElem(list, i - 1);
	LNode* del;
	del = temp->next;

	temp->next = del->next;
	del->next->previous = temp;
	free(del);
	return true;

}
//链表长度
int listLength(LinkList list)
{
	int i = 0;
	if (list->next == NULL)
		return i;

	while (list->next != NULL)
	{
		i++;
		list = list->next; 
	}
	return i;
}

//判断两个people 是否相等
bool JudgeEq(People peo1, People peo2)
{
	if (strcmp(peo1.name, peo2.name) == 0 && strcmp(peo1.ID, peo2.ID) == 0)
		return true;
}
void printPeo(People peo)
{
	printf("name = %s\n", peo.name);
	printf("ID = %s\n", peo.ID);
	printf("-------------------------------\n");
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅隐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值