循环单链表的头插及尾插实现

typedef void CircleList;
struct CircleListNode {
	CircleListNode* next;
};
struct CircleListT {
	CircleListNode header;
	CircleListNode* Slider;
	int length;
};
CircleList* createCircleList() {
	CircleListT* list = new CircleListT;
	if (list == NULL) {
		return NULL;
	}
	list->length = 0;
	list->header.next = NULL;
	list->Slider = NULL;
	return list;
}
void destoryCircleList(CircleList* list) {
	CircleListT* listt = (CircleListT*)list;
	if (listt == NULL) {
		return;
	}
	delete listt;
	listt = NULL;
}
void clearCircleList(CircleList* list) {
	CircleListT* listt = (CircleListT*)list;
	if (listt == NULL) {
		return;
	}
	listt->length = 0;
	listt->header.next = NULL;
	listt->Slider = NULL;
}
int getCircleListLength(CircleList* list) {
	CircleListT* listt = (CircleListT*)list;
	int ret = -1;
	if (listt == NULL) {
		return ret;
	}
	ret = listt->length;
	return ret;
}
CircleListNode* getNode(CircleList* list, int pos) {
	CircleListT* listt = (CircleListT*)list;
	if (listt == NULL || pos < 0) {
		return NULL;
	}
	CircleListNode* current = (CircleListNode*)listt;
	//cout << "getNode current : " << current << endl;
	for (int i = 0; i < pos && (current->next != NULL); i++) {
		current = current->next;
		//cout << "current: " << current << endl;
	}
	CircleListNode* node = current->next;
	//cout << "getNode node: " << node << " pos: " << pos << endl;
	return node;
}
int insertNodePre(CircleList* list, CircleListNode* node) {
	CircleListT* listt = (CircleListT*)list;
	if (listt == NULL || node == NULL ) {
		return -1;
	}
	CircleListNode* current = (CircleListNode*)listt;
	node->next = current->next;
	current->next = node;
	listt->length++;
	if (listt->length == 0) {
		//node->next = node;
	}
	else {
		CircleListNode* last = getNode(listt, listt->length - 1);
		listt->Slider = node;
		last->next = current->next;
	}

	return 0;
}

int insertNode(CircleList* list, CircleListNode* node, int pos) {
	CircleListT* listt = (CircleListT*)list;
	if (listt == NULL || node == NULL || pos < 0) {
		return -1;
	}
	CircleListNode* current = (CircleListNode*)listt;
	for (int i = 0; i < pos && (current->next != NULL); i++) {
		current = current->next;
	}

	node->next = current->next;
	current->next = node;

	//如果是第一次插入元素
	if (listt->length == 0) {
		listt->Slider = node;
	}
	listt->length++;

	//如果是头插法
	if (current == (CircleListNode*)listt) {
		CircleListNode* last = getNode(listt, listt->length - 1);
		last->next = current->next;
	}

	return 0;

}

CircleListNode* deleteNode(CircleList* list, int pos) {
	CircleListT* listt = (CircleListT*)list;
	if (listt == NULL || pos < 0||pos>=listt->length) {
		return NULL;
	}
	CircleListNode* current = (CircleListNode*)listt;
	for (int i = 0; i < pos; i++) {
		current = current->next;
	}
	CircleListNode* last = NULL;
	//如果删除第一个元素
	if (current == (CircleListNode*)listt) {
		 last= getNode(listt, listt->length - 1);
	}
	//删除元素
	CircleListNode* node = current->next;
	current->next = node->next;

	listt->length--;

	if (last != NULL) {
		listt->header.next = node->next;
		last->next = node->next;
	}
	//删除元素为游标所指
	if (listt->Slider == node) {
		listt->Slider = node->next;
	}
	//删除元素后长度为零
	if (listt->length == 0) {
		listt->header.next = NULL;
		listt->Slider = NULL;
	}
	return node;
}

CircleListNode* deleteNode(CircleList* list, CircleListNode* node) {
	CircleListT* listt = (CircleListT*)list;
	if (listt == NULL || node==NULL) {
		return NULL;
	}
	CircleListNode* current = (CircleListNode*)listt;
	CircleListNode* deletenode = NULL;
	int index = -1;
	for (int i = 0; i < listt->length; i++) {
		if (current->next == node) {
			index = i;
			deletenode = current->next;
			break;
		}
		current = current->next;
	}

	if (index != -1) {
		deleteNode(list, index);
	}

	return deletenode;

}
CircleListNode* resetList(CircleList* list) {
	CircleListT* listt = (CircleListT*)list;
	CircleListNode* node = NULL;
	if (listt == NULL) {
		return NULL;
	}
	listt->Slider = listt->header.next;
	node = listt->Slider;
	return node;
}
CircleListNode* currentNode(CircleList* list) {
	CircleListT* listt = (CircleListT*)list;
	CircleListNode* node = NULL;
	if (listt == NULL) {
		return NULL;
	}
	node = listt->Slider;
	return node;
}
//返回当前位置 游标下移
CircleListNode* nextNode(CircleList* list) {
	CircleListT* listt = (CircleListT*)list;
	CircleListNode* node = NULL;
	if (listt == NULL) {
		return NULL;
	}
	node = listt->Slider;
	listt->Slider = node->next;
	return node;
}



struct  NodeValue {
	CircleListNode node;
	int value;
};


int main() {

	CircleList* list = createCircleList();

	NodeValue v1, v2, v3, v4, v5, v6,v7,v8;
	v1.value = 1;
	v2.value = 2;
	v3.value = 3;
	v4.value = 4;
	v5.value = 5;
	v6.value = 6;
	v7.value = 7;
	v8.value = 8;
	//尾插法
	/*insertNode(list, (CircleListNode*)&v1, getCircleListLength(list));
	insertNode(list, (CircleListNode*)&v2, getCircleListLength(list));
	insertNode(list, (CircleListNode*)&v3, getCircleListLength(list));
	insertNode(list, (CircleListNode*)&v4, getCircleListLength(list));
	insertNode(list, (CircleListNode*)&v5, getCircleListLength(list));
	insertNode(list, (CircleListNode*)&v6, getCircleListLength(list));
	insertNode(list, (CircleListNode*)&v7, getCircleListLength(list));
	insertNode(list, (CircleListNode*)&v8, getCircleListLength(list));*/
	//头插法
	insertNodePre(list, (CircleListNode*)&v1);
	insertNodePre(list, (CircleListNode*)&v2);
	insertNodePre(list, (CircleListNode*)&v3);
	insertNodePre(list, (CircleListNode*)&v4);
	insertNodePre(list, (CircleListNode*)&v5);
	insertNodePre(list, (CircleListNode*)&v6);

	for (int i = 0; i < getCircleListLength(list)*2; i++) {
		NodeValue* v = (NodeValue*)nextNode(list);
		cout << "value: " << v->value << endl;
	}
	//约瑟夫问题
	/*while (getCircleListLength(list) > 0) {
		NodeValue* v = NULL;
		for (int i = 0; i < 3; i++) {
			v = (NodeValue*)nextNode(list);
		}
		cout << "delete v: " << v->value << endl;
		deleteNode(list, (CircleListNode*)v);
	}*/


	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构中的循环链表是一种特殊的链表,其中节点的next指针指向尾节点,而尾节点的next指针又指向节点,形成了一个环形结构。尾法(pushing at the end)是向循环链表中添加新节点的一种常见方法,通常在链表为空或已知尾节点的情况下操作。 下面是一个使用C语言实现循环链表法的简代码示例: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct Node { int data; // 数据域 struct Node* next; // 指向下一个节点的指针 } Node; // 初始化链表循环链表 Node* createCircularList() { Node* head = NULL; return head; } // 尾法添加新节点 void insertAtEnd(Node** head, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); if (!newNode) { printf("Memory allocation failed.\n"); return; } newNode->data = data; newNode->next = *head; if (*head == newNode) { newNode->next = newNode; // 如果链表为空,形成环 } else { for (Node* current = *head; current->next != *head; current = current->next) { // 遍历直到找到节点 } current->next = newNode; } // 更新节点 *head = newNode; } // 打印循环链表 void printCircularList(Node* head) { if (head == NULL) { printf("Empty list.\n"); return; } Node* current = head; do { printf("%d ", current->data); current = current->next; } while (current != head); printf("\n"); } int main() { Node* head = createCircularList(); insertAtEnd(&head, 1); insertAtEnd(&head, 2); insertAtEnd(&head, 3); printf("Circular linked list after insertion:\n"); printCircularList(head); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值