双链表的基本功能实现

//双链表 
#include<iostream>
#include<stdio.h>
#include<cstdlib>

using namespace std;

#define TRUE 1
#define FALSE 0

typedef int ElemType;
typedef bool Status;

typedef struct LNode{
   
	struct LNode *next;
	struct LNode *pre;
	int data; 
}LNode, *Linklist; 

Status InitList(Linklist &L){
   
	L = (Linklist)malloc(sizeof(LNode));
	if(!L)
		return 0;
		L->next = NULL;
	return 1;
}

void DestroyList(Linklist &L){
   
	Linklist q;
	while(L != NULL){
   
		q = L->next;
		free(q);
		L = q;
	}
}

void ClearList(Linklist L){
   
	Linklist p, q;
	q = L->next;
	while(q != NULL){
   
		p = q;
		q = q->next;
		free(p);
	}
	L->next = NULL;
}

int LengthList(Linklist &L){
   
	Linklist q = L->next;
	int ans = 0;
	while(q != NULL){
   
		ans ++ ;
		q = q->next;
	}
	return ans;
}

Status GetElem(Linklist &L, ElemType i, ElemType &e){
   
	Linklist q = L->next;
	int j = 1;
	while(j < i && q != NULL){
   
		q = q->next;
		j ++ ;
	}
	if(!q || j < i) return 0;
	e = q->data;
	return 1;
}

int Locat(Linklist L, ElemType e){
   
	Linklist q = L->next;
	int ans = 1;
	while(q != NULL){
   
		if(q->data == e){
   
			return ans;
		}
		ans ++ ;
		q = q->next;
	}
	return 0;
}

Status Getpre(Linklist &L, ElemType e, ElemType &u){
   
	Linklist q = L->next;
	while(q != NULL){
   
		if(q->data == e){
   
			u = q->pre->data;
			return true;
		}
		q = q->next;
	}
	return false;
}

Status Getnext(Linklist &L, ElemType e, ElemType &u){
   
	Linklist q = L->next;
	while(q->next != NULL){
   
		if(q->data == e){
   
			u = q->next->data;
			return 1;	
		}
		q = q->next;
	}
	return false;
}

Status Insert(Linklist &L, ElemType i, ElemType e){
   
	Linklist q = (Linklist)malloc(sizeof(LNode));
	if(!q){
   
		return 0;
	}
	if(i <= 0) return 0; 
	Linklist p = L;
	Linklist z = L->next;
	int y = 0;
	while(y < i - 1 && p != NULL){
   
		y ++ ;
		z = z->next;
		p = p->next;
	}
	if(p == NULL){
   
		return 0;
	}
	q->data = e;
	q->pre = p;
	if(p->next != NULL) p->next->pre = q;
	q->next = p->next;
	p->next = q;
	return 1;
}

Status DeElemType(Linklist &L, ElemType i, ElemType &e){
   
	Linklist q;
	int j = 0;
	if(i == 1){
   
		q = L->next
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值