双向链表的基本实现

list.h

#pragma once 
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef int LTDataType;

typedef struct ListNode
{
	struct ListNode* _next;
	struct ListNode* _prev;
	LTDataType _data;
}ListNode;

typedef struct List
{
	struct ListNode* _head;
	struct ListNode* _tail;
	int _size;
}List;

void ListInit(List* lt);
void ListDestory(List* lt);
void ListPushBack(List* lt, LTDataType x);
void ListPushFront(List* lt, LTDataType x);
void ListPopBack(List* lt);
void ListPopFront(List* lt);

ListNode* BuyListNode(LTDataType x);
ListNode* ListFind(List* lt, LTDataType x);
void ListInsert(ListNode* pos, LTDataType x);
void ListErase(List* lt, ListNode* pos, LTDataType x);
int ListSize(List* lt);
int ListEmpty(List* lt);
void ListPrint(List* lt);

list.c

#include"list.h"
ListNode* BuyListNode(LTDataType x)
{
	ListNode *newnode = NULL;
	newnode = (ListNode*)malloc(sizeof(ListNode));
	if (newnode != NULL)
	{
		newnode->_data = x;
		newnode->_prev = NULL;
		newnode->_next = NULL;
	}
	return newnode;
}
void ListInit(List* lt)
{
	assert(lt != NULL);
	lt->_head = BuyListNode(0);
	lt->_head->_next = lt->_head;
	lt->_head->_prev = lt->_head;
	int size = 0;
}
void ListDestory(List* lt)
{
	ListNode* temp;
	ListNode* p;

	p = lt->_head->_prev;

	while (!ListEmpty(lt))

	{

		temp = p->_prev;

		free(p);

		p = temp;

		lt->_head->_prev = temp;

		lt->_size--;

	}
}
void ListPushBack(List* lt, LTDataType x)
{
	ListInsert(lt->_head, x);
}
void ListPushFront(List* lt, LTDataType x)
{
	ListInsert(lt->_head->_next, x);
}
void ListErase(List* lt, ListNode* pos, LTDataType x)
{
	if (lt == NULL&&pos < 1)
	{
		return;
	}
	if (pos>ListSize(lt))
	{
		return;
	}
	ListNode *pNode = lt;
	int count = 0;
	while (count < ListSize(lt))
	{
		pNode = pNode->_next;
		count++;
	}
	if (count != pos)
		return;
	x = pNode->_data;
	pNode->_prev->_next = pNode->_next;
	pNode->_next->_prev = pNode->_prev;
	free(pNode);
	pNode = NULL;

}
void ListPopBack(List* lt,ListNode* pos,LTDataType x)
{
	assert(lt != NULL);
	ListErase(lt->_head, pos, x);
}
void ListPopFront(List* lt, ListNode* pos, LTDataType x)
{
	assert(lt != NULL);
	ListErase(lt->_head->_next, pos, x);
}


ListNode* ListFind(List* lt, LTDataType x)
{
	int i = 1;
	while (lt != NULL && lt->_head->_data != x)//寻找值为x的元素**注意这里循环的条件不能写反。原因,当L == NULL 时候 L->data会出错
	{
		i++;
		lt = lt->_head->_next;
	}
	if (lt == NULL)               //如果没找到返回-1
		return -1;
	else
		return i;
}
void ListInsert(ListNode* pos, LTDataType x)
{
	assert(pos != NULL);
	ListNode * newnode = BuyListNode(3);
	newnode->_data = x;
	ListNode* prev = pos->_prev;
	prev->_next = newnode;
	newnode->_next = pos;
	newnode->_prev = prev;
	pos->_prev = newnode;
}

int ListSize(List* lt)
{
	assert(lt != NULL);
	return (lt->_head->_prev) - (lt->_head->_next);
}
int ListEmpty(List* lt)
{
	if (ListSize(lt) == 0 && lt->_head->_next == lt->_head->_prev)
		return 1;
	else
		return 0;
	
}
void ListPrint(List* lt)
{
	ListNode *cur = lt->_head->_next;
	while (cur != lt->_head)
	{
		printf("%d->", cur->_data);
		cur = cur->_next;
	}
	printf("\n");
}

test.c

#include"list.h"
void Testlist()
{
	ListNode * lt;
	ListInit(&lt);
    //ListDestory(&lt);
	//ListInsert(&pos, x);
	ListPushBack(&lt, 1);
	ListPushBack(&lt, 2);
	ListPushBack(&lt, 3);
	ListPushBack(&lt, 4);
	ListPrint(&lt);
	////ListPushFront(&lt, 1);
	//ListPopBack(&lt);
	//ListPopBack(&lt);
	//ListPopBack(&lt);
	//ListPrint(&lt);
	////ListPopFront(&lt);
	int x = ListFind(&lt,2);
	if (x == -1)
	{
		printf("没找到\n");
	}
	else
	{
		printf("%d\n", x);
	}
	
	//ListErase(&lt, 2,3);
	int size=ListSize(&lt);
	printf("%d\n", size);
	int ret = ListEmpty(&lt);
	if (ret == 1)
	{
		printf("链表为空\n");
	}
	else
	{
		printf("链表不为空\n");
	}
	
}

int main()
{
	Testlist();
	system("pause");
	return;
}

猜你喜欢

转载自blog.csdn.net/qq_41268108/article/details/81946252
今日推荐