双向链表的基本操作

main.c
/*
*2013-11-7
*双向链表的基本操作,类似于单向链表,只不过多出了一个前节点指针
*基本操作详见代码中
*/
#include "D_List.h"


int main(void)
{
	pDLIST pHead = creat_dlist();

	traverse(pHead);

	en_dlist(pHead, 3, 8);

	traverse(pHead);

	search(pHead, 8);
	search(pHead, 65);

	out_dlist(pHead, 5);

	traverse(pHead);

	clear(pHead);

	traverse(pHead);

	return 0;
}

 

以下是D_List.h

#ifndef D_LIST_H
#define D_LIST_H
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>

typedef int ElemType;

typedef struct DList   //定义链表结构体
{
	ElemType data;
	struct DList * prior;
	struct DList * next;
}DLIST, *pDLIST;


pDLIST creat_dlist();
void en_dlist(pDLIST, int pos, ElemType val);
void out_dlist(pDLIST, int pos);
bool is_empty(pDLIST);
void traverse(pDLIST);
int len_dlist(pDLIST);
void clear(pDLIST);
void search(pDLIST, int);

#endif


D_List.cpp

//功能实现
#include "D_List.h"

pDLIST creat_dlist()
{
	pDLIST head = (pDLIST)malloc(sizeof(DLIST));//创建头节点
	if (head == NULL)
	{
		printf("分配内存失败!!!\n");
		exit(-1);
	}
	else
	{
		head->prior = head->next = NULL;

		int n, i;
		printf("请输入你要创建的节点个数 :n = ");
		scanf("%d", &n);

		srand(time(NULL));
		pDLIST pNext = NULL;
		for ( i = 0; i < n; i++)
		{
			pDLIST pNew = (pDLIST)malloc(sizeof(DLIST));
			if (NULL == pNew)
			{
				printf("内存分配失败!!!\n");
				exit(-1);
			}
			else
			{
				pNext = head->next;   //保存下一个节点
				pNew->data = rand()%100;     //放入数据
				head->next = pNew;    
				pNew->prior = head;
				pNew->next = pNext;
				if(pNext != NULL)
				{
					pNext->prior = pNew;
				}
			}
		}
		
		return head;	
	}
	
}
//插入第pos个节点,其中p是移动到了pos-1的位置,这种用法比较好!!
void en_dlist(pDLIST pL, int pos, ElemType val)
{
	int i = 0;
	pDLIST p = pL;
	while (NULL != p && i < pos-1)  //将p指到pos-1所在的位置
	{
		i++;
		p = p->next;
	}
	if (i > pos-1 || NULL == p)
	{
		printf("你插入的节点位置有问题!位置为:pos = %d  !!\n", pos);
		return;
	}
	else
	{
		pDLIST q = p->next;
		pDLIST pNew = (pDLIST)malloc(sizeof(DLIST));

		pNew->data = val;

		p->next = pNew;
		pNew->prior = p;
		pNew->next = q;
		if (q != NULL)
		{
			q->prior = pNew;
		}
		printf("第 %d 个节点 %d 插入成功!!\n", pos, val);
		return;
	}
}

void out_dlist(pDLIST pL, int pos)
{
	int i = 0;
	pDLIST p = pL;
	while (NULL != p->next && i < pos-1)
	{
		i++;
		p = p->next;
	}
	if (i > pos-1 || NULL == p->next)
	{
		printf("你删除的节点不存在!!你要删除的位置为pos = %d \n", pos);
		return;
	}
	else
	{
		pDLIST temp = p->next;
		pDLIST q = temp->next;
		p->next = q;
		printf("第 %d 个节点 %d 删除成功!!\n", pos, temp->data);
		if (q != NULL)
		{
			q->prior = q;
		}
		free(temp);
		temp = NULL;


	}
}

void traverse(pDLIST pL)
{
	if (is_empty(pL))
	{
		printf("链表为空,不能遍历!!!!!\n");
		return;
	}
	pDLIST p = pL->next;
	while (p != NULL)
	{
		printf("%d  ",p->data);
		p = p->next;
	}
	printf("\n");
}


void search(pDLIST pL, int val)
{
	int i = 0 ;
	int j = 0;
	pDLIST p = pL->next;
	while (p != NULL)
	{
		i++;
		if (p->data == val)
		{
			j++;
			printf("%d 是第 %d 个节点!! \n",val , i);
		}
		p = p->next;
	}
	if (j == 0)
	{
		printf("%d不在这个链表中!!\n", val);
	}
}

void clear(pDLIST pL)
{
	pDLIST p = pL->next;
	pL->next = NULL;
	printf("清空链表………\n");
	while (p != NULL)
	{
		pDLIST q = p->next;
		free(p);
		p = q;
	}
}
int len_dlist(pDLIST pL)
{
	int len=0;
	pDLIST p = pL->next;
	while (NULL != p)
	{
		len++;
		p = p->next;
	}
	return len;
}

bool is_empty(pDLIST pL)
{
	if (pL->next == NULL && pL->prior == NULL)
	{
		return true;
	}
	else return false;
}


测试结果为:


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值