[数据结构] 双向链表的实现

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

typedef int Elementtype;


struct celltype
{
	Elementtype element;
	struct celltype *next;
	struct celltype *previous;
} ;

typedef celltype *position;

position CreateNode(position head)
{
	struct celltype *p = NULL;
	struct celltype *q = head;
	p = (struct celltype*)malloc(sizeof(struct celltype));
	if(p == NULL)//若为新建节点申请内存失败,则退出程序 
	{
		printf("No enough memory to allocate! \n");
		exit(0);
	} 
	if(head == NULL)//若原链表为空表 
	{
		head = p;//将新建节点置为头节点 
	}
	else
	{
		while(q->next!=NULL)
		{
		    q = q->next;
		}
		q->next = p;
		p->previous = q;		
	}
	scanf("%d",&p->element);
	p->next = NULL;
	return head;	
}

void DisplsyNode1(struct celltype* head)//使用next遍历 
{
	struct celltype *p = head;
	int j = 1;
	while (p != NULL)
	{
		printf("%5d%10d\n",j,p->element);//打印第J个点的数据
		p = p->next;
		j++; 
	}
}

void DisplsyNode2(struct celltype* head)//使用previous遍历 
{
	struct celltype *p = head;
	struct celltype *q = head;
	int j = 1;
	while(p->next!=NULL)
	{
		p = p->next;
	}
	
	while (p != q)
	{
		printf("%5d%10d\n",j,p->element);//打印第J个点的数据
		p = p->previous;
		j++; 
	}
	printf("%5d%10d\n",j,p->element);//打印第J个点的数据
	//p = p->previous;
}

position DeleteNode(struct celltype *head, Elementtype nodeData)//删除某个节点 
{
	struct celltype *p = head, *pr = head;
	if(head == NULL)//若链表为空表,则退出程序 
	{
		printf("Linked Table is empty!\n");
		return(head);
	}
	while(nodeData != p->element && p->next != NULL)//Not been found and no getting to the end;
	{
		pr = p;
		p=p->next;
	}
	
	if(nodeData == p->element) //若当前节点的节点值为nodeData,找到待删除节点
	{
	    if(p == head)//若待删除节点为头节点 
		{
		    head = p->next;	
		} 
		else
		{
			pr->next = p->next;//important:让前一节点的指针域指向待删节点的下一节点
			p->next->previous = p->previous; //让p的下一个前驱结点指向p的前驱结点,(跳过p) 
		}
		free(p); 
	}
	else
	{
		printf("This Node has not been found!\n");
	}
	return head;
}

position InsertNode(struct celltype *head, Elementtype nodeData)//在某个位置插入新节点 
{
	struct celltype *pr = head, *p = head, *temp = NULL;
	p = (struct celltype*)malloc(sizeof(struct celltype));//让p指向待插入节点
	if(p == NULL)
	{
		printf("No enough memory!\n");
		exit(0);//退出程序 
	} 
	
	p->next=NULL;//为待插入节点的指针域赋值为空指针
	p->element=nodeData;//为待插入节点数据域赋值为nodeData 
	if(head == NULL)//若原链表为空表
	{
		head = p;
	} 
	else
	{
		while(pr->element < nodeData && pr->next != NULL)
		{
			temp = pr;
			pr = pr->next;
		}
		if(pr->element >= nodeData)
		{
			if(pr == head)//若在头节点前加入新节点
			{
			     p->next = head;
			     head = p;
			     p->next->previous = p;
			}
			else
			{
				pr->previous = p;
				p->previous = temp;
				pr = temp;
				p->next = pr->next;
				pr->next = p;//让前一节点的指针域指向新节点 
			}
		}
		else//表尾 
		{
			pr->next = p;//让末节点的指针域指向新节点 
			p->previous = pr;
		} 
	}
	return head;//return head pointer after inserting the new node of linked table. 
}

int main()
{
	struct celltype *head = NULL;
	//head->previous = NULL;
	
	Elementtype del,ins; 
	printf("请按升序输入链表数据(5个):\n");
	
	for(int i=0;i<5;i++)
	  head = CreateNode(head);
	
	DisplsyNode1(head);
	printf("\n");
	
	DisplsyNode2(head);
	printf("\n");
	
	
	printf("Delete nodeData:\n");
	scanf("%d",&del);
	
	head = DeleteNode(head,del);
	
	DisplsyNode1(head);
	printf("\n");
	
	DisplsyNode2(head);
	printf("\n");
	
	printf("Insert nodeData:\n");
	scanf("%d",&ins);
	
	head = InsertNode(head,ins);
	
	DisplsyNode1(head);
	printf("\n");
	
	DisplsyNode2(head);
	printf("\n");
		
	return 0;
}

/*程序已通过测试,输入只支持5个,具体输入方法,数量改变读者可自行改变,为体现双向性,每次操作进行正反遍历显示,以证明双向链表创建成功*/ 

//具体可改变为循环链表,此处不予展示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值