数据结构--双向链表

先上代码:双向链表的基本操作 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define overflow 1
typedef struct node_{
	int data;
	struct node_ *next;
	struct node_ *precursor;
}Node,*node;
void initList(node &l);
void insertNode(node &l,int n);
void clearList(node &l);
void desList(node &l);
bool listEmpty(node &l);
int getElement(node &l,int i);
int locateElement(node &l,int e);
int priorElement(node &l,int e);
int nextElement(node &l,int e);
void deleteNode(node &l,int e);
void test();
//建立链表
void initList(node &l){
	l=(node)malloc(sizeof(Node));
	l->next=NULL;
	l->precursor=NULL;
	if (!l){
		exit(overflow);
	}
}
//插入元素 
void insertElement(node &l,int n,int g){
     node p,q;
     p=l;
	for (int i = 0; i < g; i ++) {
		p = p->next;
		if (p == NULL) {
			printf("The position %d is beyond the scope of the list.", g);
			return;
		}
	}
	q = (node)malloc(sizeof(Node));
    q->data = g;
	q->next = p->next;
	p->next = q;
	q->precursor=p;
	q->next->precursor=q;
	}
//尾插法
void insertNode(node &l,int n){
    if(n<0){
		printf("输入的n不合法\n");
		exit(overflow);
	}
	node last=NULL;
	last=l;
	for (int i=0;i<n;i++){
		node p=(node)malloc(sizeof(Node));
		scanf("%d",&p->data);
		p->next=NULL;
		p->precursor=NULL;
		last->next=p;
		p->precursor=last;
		last=p;		
	}
}
//打印链表
void printList(node &l)
{
	if (!l)
	{
		printf("链表不存在");
		exit(overflow);
	}
	printf("打印链表:");
	for (node p=l->next;p;p=p->next)
	{
		printf("%d ",p->data);
	}
	printf("\n"); 
}
//清空链表 
void clearList(node &l)
{
	node p=l->next;
	node q=p;
	while(p){
		q=p->next;
		free(p);
		p=NULL;
		p=q;
	}
	l->next=NULL; 
}
//销毁链表
void desList(node &l){
    node p=l;
	node q=p;
	while(p->next){
		q=p->next;
		free(p);
		p=NULL;
		p->next=q->next;
}
l=NULL;
printf("链表已销毁");}
 
//检查链表是否为空
bool listEmpty(node &l)
{
	if (l->next)
	{
		return true;
	}
	else
	{
		return false;
	}
}
//返回链表第i个的值
int getElement(node &l,int i){
    if (i<=0 || l->next==NULL){
		printf("链表为空或i的值不合法");
		exit(overflow);
	}
	node p=l;
	for (int q=0;q<i;q++)
	{
		p=p->next;
	}
    return p->data;
}
//返回与e值相同的元素位置
int locateElement(node &l,int e){
	int i=0;
	for (node p=l->next;p;p=p->next)
	{
		i++;
		if (p->data==e){
			return i;
		}
	}
    return 0;
}
//返回指定元素的前驱
int priorElement(node &l,int e){
	for (node p=l->next;p;p=p->next)
	{
		if (p->data==e){
			return p->precursor->data;
		}
	}
	return 0;
	
}
//返回指定元素的后继
int nextElement(node &l,int e)
{
	for (node p=l->next;p->next;p=p->next)
	{
		if (p->data==e){
			return p->next->data;
		}
	}
	return 0;
}
//删除指定的结点
void deleteNode(node &l,int e){
	node p=l;
	while(p->next) 
	{
		if (p->next->data==e){
			node q=p->next;
			p->next=p->next->next;
			free(q);
			q=NULL; 
		}
		else 
		p=p->next;
	}
}
void test(){
	node l;
	initList(l);
	int n;
	scanf("%d",&n);
	insertNode(l,n);
	printList(l);
	bool r=listEmpty(l);
	printf("链表是否为空:%d\n",r);
	int e;
	e=locateElement(l, 2);
	printf("该元素的位置:%d\n",e);
    e=getElement(l,3);
	printf("某位置的元素:%d\n",e);
	e=priorElement(l,e);
	printf("元素的前驱:%d\n",e);
	e=nextElement(l,e);
	printf("元素后继:%d\n",e);
	deleteNode(l,e);
	printf("删除指定元素后:");
	printList(l);
	clearList(l);
	r=listEmpty(l);
	printf("清空链表后:");
	printf("链表是否为空:%d\n",r);
	desList(l);
}
int main(void)
{
	test();
	return 0;
}

运行结果: 

 

总结:

双向链表的相关操作还是比较简单。关键收获在于:这次再写双向链表的过程中依然出现了之前经常出现的问题---关于内存方面,这次看了好一些文章看完总算有种豁然开朗的感觉(以前内存方面确实学得太差了)

附上几篇好文章:C与C++关于*与&的传参解析 (baidu.com)

(26条消息) c中使用free()函数,指针仍然有地址?_小菜菜ovo的博客-CSDN博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值