数据结构----循环链表
2013-10-22 22:16
176 查看
#include <stdio.h> #include <stdlib.h> #include <malloc.h> typedef struct Node { int elem ; struct Node * next ; } Node ; typedef struct CycleLinkList { Node * head ; } CycleLinkList ; // 初始化循环链表 void InitCycleLinkList( CycleLinkList * C ) { C->head = (Node*)malloc(sizeof(Node)) ; if( !C->head ) { printf("申请内存失败!") ; exit(0) ; } C->head->next = NULL ; } // 插入 void Insert( CycleLinkList * C , int elem ) { Node * node = (Node*)malloc(sizeof(Node)) ; if( !node) { printf("申请内存失败!") ; exit(0) ; } node->elem = elem ; if( C->head->next == NULL ) // 循环链表为空 { C->head->next = node ; node->next = C->head->next ; } else { Node * p = C->head->next ; while( p->next != C->head->next ) { p = p->next ; } p->next = node ; node->next = C->head->next ; } } // 删除 void Delete( CycleLinkList * C , int elem ) { Node * p = C->head->next ; Node * q = p ; while( p->elem != elem ) { q = p ; p = p->next ; } if( p == C->head->next ) { q->next = p->next ; C->head->next = p->next ; free(p) ; } else { q->next = p->next ; free(p) ; } } // 输出 void Print( CycleLinkList * C ) { Node * p = C->head->next ; while( p->next != C->head->next ) { printf("%d " , p->elem ) ; p = p->next ; } printf("%d " , p->elem ) ; } //测试 int main() { CycleLinkList C ; InitCycleLinkList( &C ) ; Insert( &C , 5 ) ; Insert( &C , 3 ) ; Insert( &C , 8 ) ; Print( &C ) ; printf("\n") ; Delete( &C , 3 ) ; Print( &C ) ; printf("\n") ; return 0 ; }
相关文章推荐
- 数据结构也不是那么没意思之前序二叉树+二叉树转伪双向循环链表
- 数据结构学习笔记(5.线性表之双向循环链表)
- 数据结构(4)-线性表(循环链表)
- 数据结构/循环链表
- 数据结构--循环链表,魔术师发牌问题
- 《数据结构》只设置尾指针而不设置头指针的讯循环链表的合并
- 数据结构---线性表----循环链表和双向链表
- 头疼的算法与数据结构——循环链表详解
- 数据结构也不是那么没意思之双向循环链表
- 数据结构学习笔记 --- 线性表 (双向链表、循环链表)
- 数据结构练习题 约瑟夫环—用循环链表实现
- 数据结构---线性表----循环链表和双向链表
- 数据结构学习笔记三 循环链表及双向链表
- Tekson的数据结构程序3——循环链表
- 使用不带头结点的循环链表实现队列(数据结构)
- 一步一步学数据结构之1--1(循环链表)
- 数据结构---线性表----循环链表和双向链表
- 数据结构学习笔记(5.线性表之双向循环链表)
- 数据结构-循环链表
- 数据结构-循环链表