线性表之双向循环链表

//exp2-5.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define OK 1
#define ERROR 0 

//========================循环双向链表     结构说明=============
typedef int ElemType;
typedef struct LNode{
	ElemType data;
	struct LNode *next;
	struct LNode *prior;
}CDListNode;
typedef CDListNode *CDLinkList;

//内置申请节点函数
inline CDLinkList newNode(){
	CDLinkList p = (CDLinkList)malloc(sizeof(CDListNode));
	if(p != NULL){
		p->next = p->prior = NULL; 
	} 
	return p;
} 
//初始化 
void InitList(CDLinkList *L){
	(*L) = (CDLinkList)malloc(sizeof(CDListNode));
	if(*L == NULL){
		fprintf(stderr, "初始化失败!\n");
		exit(EXIT_FAILURE);
	}
	(*L)->next = (*L)->prior = *L;
}

//销毁表
void DestroyList(CDLinkList L){
	CDLinkList p = L, q = p->next;
	while(q != L){
		free(p);
		p = q;
		q = p->next;
	}
	free(p);
} 

//判断表是否为空
int ListEmpty(CDLinkList L){
	return L->next == L;
} 

//求表长
int ListLength(CDLinkList L){
	int i = 0;
	CDLinkList p = L;
	while(p->next != L){
		i++;
		p = p->next;
	}
	return i;
} 

//输出表
void DispList(CDLinkList L){
	CDLinkList p = L->next;
	while(p != L){
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
} 
//求i位置的元素
int GetElem(CDLinkList L, int i, ElemType *e){
	int j;
	CDLinkList p = L;
	while(j < i && p->next != L){
		i++;
		p = p->next;
	}
	if(p == L){
		return ERROR;
	}
	else{
		*e = p->data;
		return OK;
	}
} 

//求元素的位置
int LocateElem(CDLinkList L, ElemType e){
	int i = 1;
	CDLinkList p = L->next;
	while(p != L && p->data != e){
		i++;
		p = p->next;
	}
	if(p == L){
		return ERROR;
	}
	else{
		return i;
	}
} 

//插入元素
int InsertList(CDLinkList L, int i, ElemType e){
	int j = 0;
	CDLinkList p = L;
	CDLinkList pNew = NULL;
	
	//查找位置
	if(p->next == L){                      //若为空表 
		pNew = newNode();
		pNew->data = e;
		p->next = pNew;
		pNew->next = p;
		p->prior = pNew;
		pNew->prior = p; 
		return OK;
	} 
	else if(j == 1){                     //表不为空,但插入第一个位置
		pNew = newNode();
		pNew->data = e;
		pNew->next = p->next;
		pNew->next->prior = pNew;
		p->next = pNew;
		pNew->prior = p; 
		return OK;
	}
	else{
		p = L->next;
		while(j < i - 2 && p != L){            //查找前驱
			j++;
			p = p->next; 
		}
		if(p == L)
			return ERROR;
		else{
			pNew = newNode();
			pNew->data = e;
			pNew->next = p->next;
			if(pNew->next != NULL)              //非尾节点
				pNew->next->prior = pNew;
			p->next = pNew;
			pNew->prior = p;
			return OK; 
		}
	}
} 

//删除 
int DeleteList(CDLinkList L, int i, ElemType *e){
	int j = 0;
	CDLinkList p = L;
	CDLinkList q = NULL;
	 
	if(p->next != L){
		if(i == 1){
			q = p->next;
			*e = q->data;
			p->next = q->next;
			q->next->prior = p;
			return OK; 
		}
		else{
			p = L->next;
			while(j < i - 2 && p != L){
				j++;
				p = p->next;
			}
			if(p == L)
				return ERROR;
			else{
				q = p->next;           //要被删除的节点
				if(q == L)            //不存在q 
					return ERROR;
				*e = q->data;
				p->next = q->next;
				if(q->next != NULL)       //非尾节点
					q->next->prior = p;
				free(q);
				return OK; 
			}//else
		}//else
	}//if
	else                 //空表 
		return ERROR;
}


阅读更多
换一批

没有更多推荐了,返回首页