데이터 구조 - 양 방향 순환 링크 c 언어 구현

XList. h 는 다음 과 같 습 니 다.
#ifndef __LIST_H
#define __LIST_H

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

void InitList(List list);//       

void Insert_Head(List list, int var);//    

void Insert_Tail(List list, int var);//    

LNode *Search(List list, int key);//  key

LNode *SearchPre(List list, int key);//  key   

bool Delete(List list, int key);//  key

bool isEmpty(List list);//    

int Length(List list);//           

void Show(List list);//  

void Destroy(List list);//  list      


#endif

XList. c 는 다음 과 같 습 니 다.
#include"XList.h"
#include
#include
#include

static LNode* BuyNode(int val)
{
	LNode *p = (LNode*)malloc(sizeof(LNode));
	assert(p != NULL);
	p->data = val;
	p->next = NULL;
	p->pre = NULL;
	return p;
}

void InitList(List list)//       
{
	assert(list);
	list->next = list;
	list->pre = list;
}

void Insert_Head(List list, int var)//    
{
	LNode *p = BuyNode(var);
	LNode *tmp = list;
	p->next = list->next;
	//list->next->pre = p;//   
	p->pre = list;
	list->next = p;
	while (tmp->next != list)
		tmp = tmp->next;
	list->pre = tmp;
}

void Insert_Tail(List list, int var)//    
{
	LNode *last = list;
	LNode *p = BuyNode(var);
	while (last->next != list)
		last = last->next;
	last->next = p;
	p->pre = last;
	p->next = list;
	list->pre = p;
}

LNode *Search(List list, int key)//  key
{
	LNode* p = list->next;
	while (p!= list)
	{
		if (p->data == key)
			return p;
		p = p->next;
	}
	return NULL;
}

LNode *SearchPre(List list, int key)//  key   
{
	LNode* p = list->next;
	while (p!= list)
	{
		if (p->data == key)
			return p->pre;
		p = p->next;
	}
	return NULL;
}

bool Delete(List list, int key)//  key
{
	LNode* p = Search(list, key);
	if (p == NULL)
		return false;
	p->next->pre = p->pre;
	p->pre->next = p->next;
	free(p);
	return true;
}

bool isEmpty(List list)//    
{
	if (list->next = list)
		return true;
	return false;
}

int Length(List list)//           
{
	int count = 0;
	LNode* p = list;
	while (p->next != list)
	{
		count++;
		p = p->next;
	}
	return count;
}

void Show(List list)//  
{
	LNode* p = list;
	while (p->next != list)
	{
		printf("%d ", p->next->data);
		p = p->next;
	}
	printf("
"); } void Destroy(List list)// list { LNode* p = list->next; LNode* t = p; while (p!= list) { p->next->pre = p->pre; p->pre->next = p->next; t = p; free(t); } free(list); }


좋은 웹페이지 즐겨찾기