链表的ADT

Status InitList(LinkedList L) {
	if(!L) return OVERFLOW;
	L = (LinkedList) malloc (sizeof(LNode));
	L->next = NULL;
	return SUCCESS;
}

/**
 *  @name        : void DestroyList(LinkedList *L)
 *	@description : destroy a linked list, free all the nodes
 *	@param		 : L(the head node)
 *	@return		 : None
 *  @notice      : None
 */
void DestroyList(LinkedList *L) {
	LNode*p = L ;
	while(p){
		p = p->next;
		free(L);
		L = p;
	}
	
}

/**
 *  @name        : Status InsertList(LNode *p, LNode *q)
 *	@description : insert node q after node p
 *	@param		 : p, q
 *	@return		 : Status
 *  @notice      : None
 */
Status InsertList(LNode *p, LNode *q) {
	if(p == NULL||q == NULL) return ERROR;
	q->next = p->next;
	p->next = q;
	return SUCCESS;	
}
/**
 *  @name        : Status DeleteList(LNode *p, ElemType *e)
 *	@description : delete the first node after the node p and assign its value to e
 *	@param		 : p, e
 *	@return		 : Status
 *  @notice      : None
 */
Status DeleteList(LNode *p, ElemType *e) {
	if(p == NULL||p->next == NULL)return ERROR;
	LNode*q = p->next;
	e = q->data;
	p->next = q->next;
	free(q);
	return SUCCESS;
}

/**
 *  @name        : void TraverseList(LinkedList L, void (*visit)(ElemType e))
 *	@description : traverse the linked list and call the funtion visit
 *	@param		 : L(the head node), visit 
 *	@return		 : None
 *  @notice      : None
 */
void TraverseList(LinkedList L, void (*visit)(ElemType e)) {
	LNode*p = L->next;
	while(p){
		visit(p->data);
		p=p->next;
	}
}
void visit(ElemType e){
	printf("%d",e);
} 

/**
 *  @name        : Status SearchList(LinkedList L, ElemType e)
 *	@description : find the first node in the linked list according to e 
 *	@param		 : L(the head node), e
 *	@return		 : Status
 *  @notice      : None
 */
Status SearchList(LinkedList L, ElemType e) {
	if(L->next == NULL) return ERROR;
	LNode*p = L->next;
	while(p->data != e&&p != NULL){
		p = p->next;
	}
	return SUCCESS;
} 

/**
 *  @name        : Status ReverseList(LinkedList *L)
 *	@description : reverse the linked list 
 *	@param		 : L(the head node)
 *	@return		 : Status
 *  @notice      : None
 */
Status ReverseList(LinkedList L) {
	if(L == NULL)return ERROR;
	LNode*p,*q,*r;
	p = L->next; L->next = NULL;
	q = r = NULL;
	while(p){
		q = p->next;
		p->next = r;
		r = p;
		p = q;
	}
	L->next = r;
	return SUCCESS;
}

/**
 *  @name        : Status IsLoopList(LinkedList L)
 *	@description : judge whether the linked list is looped
 *	@param		 : L(the head node)
 *	@return		 : Status
 *  @notice      : None
 */
Status IsLoopList(LinkedList L) {
	if(L == NULL||L->next == NULL)return ERROR;
	LNode*fast,*slow;
	fast = slow = L;
	fast = fast->next->next;
	slow = slow->next;
	while(1){
		if(!fast||!slow)return ERROR;
		if(fast == slow)return SUCCESS;
		else{
			fast = fast->next->next;
			slow = slow->next;	
		}
	}
}

/**
 *  @name        : LNode* ReverseEvenList(LinkedList *L)
 *	@description : reverse the nodes which value is an even number in the linked list, input: 1 -> 2 -> 3 -> 4  output: 2 -> 1 -> 4 -> 3
 *	@param		 : L(the head node)
 *	@return		 : LNode(the new head node)
 *  @notice      : choose to finish 
 */
LNode* ReverseEvenList(LinkedList L) {
	if(L == NULL||L->next == NULL)return L;
	LNode*cur,*prior,*dummy;
	dummy = L;
	prior = dummy->next;
	cur = prior->next;
	while(cur!=NULL){
		if(!(cur->data%2)){
			prior->next = cur->next;
			cur->next = prior;
			dummy = cur;
			cur = prior;
			prior = dummy;
		}
		dummy = dummy->next;
		prior = prior->next;
		cur = cur->next;	
	}
	return L;
	}

/**
 *  @name        : LNode* FindMidNode(LinkedList *L)
 *	@description : find the middle node in the linked list
 *	@param		 : L(the head node)
 *	@return		 : LNode
 *  @notice      : choose to finish 
 */
LNode* FindMidNode(LinkedList L) {
	if(L==NULL || L->next==NULL) return L;
	LNode *mid , *fast;
	mid = fast = L;
	while(fast!=NULL){
		if(fast->next==NULL)fast = fast->next;
		else fast = fast->next->next;
		mid=mid->next;
	}
	return mid;
	
}
/**
 *  @name        : Status InitList_DuL(DuLinkedList *L)
 *	@description : initialize an empty linked list with only the head node
 *	@param		 : L(the head node)
 *	@return		 : Status
 *  @notice      : None
 */
Status InitList_DuL(DuLinkedList L) {
	L = (DuLinkedList)malloc(sizeof(DuLNode));
	if(!L) return OVERFLOW;
	L->prior = L->next = L;
	return SUCCESS;	
}

/**
 *  @name        : void DestroyList_DuL(DuLinkedList *L)
 *	@description : destroy a linked list
 *	@param		 : L(the head node)
 *	@return		 : status
 *  @notice      : None
 */
void DestroyList_DuL(DuLinkedList *L) {
	DuLNode *p=L;
	while(p){
		p=p->next;
		free(L);
		L=p;
	}
}

/**
 *  @name        : Status InsertBeforeList_DuL(DuLNode *p, LNode *q)
 *	@description : insert node q before node p
 *	@param		 : p, q
 *	@return		 : status
 *  @notice      : None
 */
Status InsertBeforeList_DuL(DuLNode *p, DuLNode *q) {
		if(p == NULL||q == NULL||p->prior == NULL) return ERROR;
		q->next = p;
		q->prior = p->prior;
		p->prior->next = q;
		p->prior = q;
		return SUCCESS;
}

/**
 *  @name        : Status InsertAfterList_DuL(DuLNode *p, DuLNode *q)
 *	@description : insert node q after node p
 *	@param		 : p, q
 *	@return		 : status
 *  @notice      : None
 */
Status InsertAfterList_DuL(DuLNode *p, DuLNode *q) {
	if(p == NULL||q == NULL||p->prior == NULL) return ERROR;
	q->next = p->next;
	q->prior=p;
	p->next->prior = q;
	p->next=q; 	
	return SUCCESS;
}

/**
 *  @name        : Status DeleteList_DuL(DuLNode *p, ElemType *e)
 *	@description : delete the first node after the node p and assign its value to e
 *	@param		 : p, e
 *	@return		 : status
 *  @notice      : None
 */
Status DeleteList_DuL(DuLNode *p, ElemType *e) {
	if(p == NULL|| p->next == NULL) return ERROR;
	e = p->next->data;
	DuLNode *q = p->next;
	p->next = p->next->next;
	q->next->prior = p;
	free(q);
	return SUCCESS;	
}
 
/**
 *  @name        : void TraverseList_DuL(DuLinkedList L, void (*visit)(ElemType e))
 *	@description : traverse the linked list and call the funtion visit
 *	@param		 : L(the head node), visit 
 *	@return		 : Status
 *  @notice      : None
 */
void TraverseList_DuL(DuLinkedList L, void (*visit)(ElemType e)) {
	DuLNode *p = L->next;
	while(p){
		visit(p->data);
		p = p->next;
	}
}
void visit(ElemType e){
	printf("%d",e);
} 
复制代码

转载于:https://juejin.im/post/5c9777995188256aa356e135

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值