当前位置:   article > 正文

双向循环链表的C语言实现_双向循环链表实现的队列c语言实现

双向循环链表实现的队列c语言实现

在工作中的项目有用到双链表,尤其是跟着别人写双链表代码的思路,自己去看总觉得没那么顺,感觉以后也会经常用到,所以索性自己写一个出来,细节由自己去把握,终于是理解了这一块,以下是实现双链表的所有源码:

#include <stdio.h>
#include <malloc.h> 

typedef struct List
{
	struct List *pre; 
	struct List *next;
	int data;
}List_t;

typedef struct HeadNode
{
	int nodeLenth;   //用于存储节点的长度
	List_t *headNode;  //头结点
}HeadNode_t;

//初始化头结点 
HeadNode_t g_headNode = {
    .nodeLenth = 0,
    .headNode = NULL,
};

//双向循环链表 
//插入结点 
void InsertNode(List_t *ListMember)
{
	unsigned char i = 0;
	if(g_headNode.nodeLenth == 0) {   //插入的第一个结点当做头结点 
	    g_headNode.nodeLenth++;
		g_headNode.headNode = ListMember;
		g_headNode.headNode->next = ListMember;    //第一个结点的next指向自己,主要是为了构成双向循环链表 
		g_headNode.headNode->pre = ListMember;     //第一个结点的pre指向自己 
		return;
	}
	
	g_headNode.nodeLenth++;
	List_t *tempNode = g_headNode.headNode->pre;  //tempNode永远表示链表的最后一个节点       
	
	tempNode->next = ListMember;                 //插入的结点放到最后 
	g_headNode.headNode->pre = ListMember;
	ListMember->next = g_headNode.headNode;
	ListMember->pre = tempNode;
	
	tempNode = g_headNode.headNode;              //打印结点 
	for(i = 1; i <= g_headNode.nodeLenth; i++) {
		printf("The data%d is %d\n", i, tempNode->data);
		tempNode = tempNode->next;
	}
}

//根据数据找到结点 
List_t* FindDataNode(int data)
{
	unsigned char i;
	List_t *tempNode = g_headNode.headNode;
	for(i = 0; i < g_headNode.nodeLenth; i++) {
		if(tempNode->data == data) {
			return tempNode;
		}
		tempNode = tempNode->next;
	}
	return NULL;
}

//删除结点 
void DelDataNode(int data)
{
	unsigned char i = 0;
	List_t *DelNode = FindDataNode(data);
	g_headNode.nodeLenth--;
	if(DelNode == g_headNode.headNode) {     //如果删除的是头结点 
		g_headNode.headNode->pre->next = DelNode->next;
		g_headNode.headNode->next->pre = DelNode->pre;
		g_headNode.headNode = DelNode->next;
		
		for(i = 1; i <= g_headNode.nodeLenth; i++) {
			printf("After del HeadNode, the data%d is %d\n", i, DelNode->next->data);
			DelNode = DelNode->next;
		}
		return;
	}
	DelNode->next->pre = DelNode->pre;       //删除结点的具体操作 
	DelNode->pre->next = DelNode->next;
	
	DelNode = g_headNode.headNode;
	for(i = 1; i <= g_headNode.nodeLenth; i++) {
		printf("The data%d is %d\n", i, DelNode->data);
		DelNode = DelNode->next;
	}
}

//设置结点为头结点 
void SetHeadNode(int data)
{
	List_t *TempNode = FindDataNode(data);
	g_headNode.headNode = TempNode;
}

int main(void) 
{
	List_t *listMember1 = NULL;
	listMember1 = (List_t *)malloc(sizeof(List_t));
	listMember1->data = 18;
	listMember1->next = NULL;
	listMember1->pre = NULL;
	
	List_t *listMember2 = NULL;
	listMember2 = (List_t *)malloc(sizeof(List_t));
	listMember2->data = 19;
	listMember2->next = NULL;
	listMember2->pre = NULL;
	
	InsertNode(listMember1);
	InsertNode(listMember2);

    DelDataNode(18);
    
	printf("The lenth is %d\n", g_headNode.nodeLenth);
	
	free(listMember1);
	free(listMember2);
	return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124

若是大家有其他想法或者有发现错误,欢迎指正哈~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/660735
推荐阅读
相关标签
  

闽ICP备14008679号