C双向链表

无人久伴 提交于 2019-11-29 06:17:28
#include <stdio.h>

typedef int ElemType;
typedef struct Node
{
  ElemType data;
  struct Node* next;
  struct Node* prior;
}Node;
typedef struct DoubleList
{
  int length;
  Node header;
}DoubleList;

void InitList(DoubleList*); 
int ListEmpty(DoubleList*);
int ListLength(DoubleList*);
void ListInsert(DoubleList*, int, ElemType );
void PrintList(DoubleList*);
void PrintList_Reverse(DoubleList*);
void CreateList(DoubleList*, int);
void ListDelete(DoubleList*, int, int*);
void GetElem(DoubleList*, int, int*);
void ClearList(DoubleList*);
void DestroyList(DoubleList*);

#include <stdio.h>
#include <stdlib.h>
#include "doublelist.h"

void InitList(DoubleList* L)
{
	L->header.next = NULL;
	L->length = 0;
}

int ListEmpty(DoubleList* L)
{
	if(L->length == 0)
		return 1;
	else
		return 0;
}

int ListLength(DoubleList* L)
{
	return L->length;
}

void ListInsert(DoubleList* L, int pos, ElemType e)
{
	if(pos < 0 || pos > L->length)
	{	
		printf("插入位置非法.\n");
		return;
	}

	Node* pCur=&L->header;

	int i = 0;
	for(i = 0; i < pos; i++)
		pCur = pCur->next;

	Node* pNew = (Node*)malloc(sizeof(Node));
	if(pos == L->length)
	{
		pNew->data = e;
		pNew->prior = pCur;
		pCur->next = pNew;
		pNew->next = NULL;
		L->length++;
		return;
	}

	pNew->data = e;
	pNew->next = pCur->next;
	pCur->next->prior = pNew;
	pCur->next = pNew;
	pNew->prior = pCur;

	L->length++;
}

void PrintList(DoubleList* L)
{
	if(L->length == 0)
	{
		printf("此链表为空链表.\n");
		return;
	}
	Node* pCur = &L->header;
	int i = 0;
	for(i = 0; i < L->length; i++)
	{
		pCur = pCur->next;
		printf("链表中位置%d的值为: %d\n", i, pCur->data);
	}
}

void PrintList_Reverse(DoubleList* L)
{
	if(L->length == 0)
	{
		printf("链表为空,无法输出.\n");
		return;
	}

	Node* pCur=&L->header;
	int i=0;
	
	for(i = 0; i < L->length; i++)
		pCur = pCur->next;

	for(i = L->length - 1; i >= 0; i--)
	{
		printf("链表中位置为%d的值为: %d\n",i, pCur->data);
		pCur = pCur->prior;
	}
}

void CreateList(DoubleList* L, int n)
{
	if(n<=0)
	{
		printf("输入的链表长度不合法.\n");
		return;
	}

	int i = 0;
	for(i = 0; i < n; i++)
	{
		printf("请输入链表位置%d处的值: \n", i);
		int num = 0;
		scanf("%d", &num);
		ListInsert(L, i, num);
	}
}

void ListDelete(DoubleList* L, int pos, int* e)
{
	if(pos < 0 || pos >= L->length)
	{
		printf("删除位置非法.\n");
		return;
	}
	
	if(L->length == 0)
	{
		printf("此链表为空.\n");
		return;
	}

	Node* pCur = &L->header;
	int i = 0;

	for(i = 0; i < pos; i++)
		pCur = pCur->next;

	Node* pDel = (Node *)malloc(sizeof(Node));
	if(!pDel) return;
    
	if(pos == L->length - 1)
	{
		pDel = pCur->next;
		pCur->next = NULL;
		free(pDel);
		L->length--;
		return;
	}

	pDel = pCur->next;
	*e = pDel->data;

	pCur->next = pDel->next;
	pDel->next->prior = pCur;
	free(pDel);
	L->length--;
}


void GetElem(DoubleList* L, int pos, int* e)
{
	if(pos < 0 || pos >= L->length)
	{
		printf("查询位置非法.\n");
		return;
	}

	if(L->length == 0)
	{
		printf("链表为空.\n");
		return;
	}

	Node* pCur = &L->header;
	int i = 0;
	for(i = 0; i < pos + 1; i++)
		pCur = pCur->next;
	*e = pCur->data;
}

void ClearList(DoubleList* L)
{
	while(L->length)
	{
		int tmp;
		ListDelete(L, 0, &tmp);
	}
}


void DestroyList(DoubleList* L)
{
	ClearList(L);
	free(L);
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!