//双链表
#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
双链表的基本功能实现
最新推荐文章于 2022-06-14 08:00:00 发布