数据结构之循环链表
2015-05-12 00:01
225 查看
循环链表与单链表相比,最大的特点就是其最后一个节点的指针域指向我们的头节点,其他方面与单链表并无二致。
节点结构:
链表结构:
不过和单链表不同的是,循环链表的表尾的指针域指向头节点,即:
下面上代码:
头文件List.h:
函数实现List.cpp:
节点结构:
链表结构:
不过和单链表不同的是,循环链表的表尾的指针域指向头节点,即:
下面上代码:
头文件List.h:
#ifndef _LIST_H #define _LIST_H #include<iostream> #include<assert.h> using namespace std; #define ElemType int typedef struct Node { ElemType data; struct Node *next; }Node, *PNode; typedef struct List { PNode first; PNode last; size_t size; }List; void InitList(List *list); bool push_back(List *list, ElemType x); void ShowList(List *list); bool push_front(List *list,ElemType x); #endif
函数实现List.cpp:
#include"List.h" void InitList(List *list) { Node *s = (Node *)malloc(sizeof(Node)); assert(s != NULL); list->first = list->last = s; list->last->next = list->first; list->size = 0; } bool push_back(List *list, ElemType x) { Node *s = (Node*)malloc(sizeof(Node)); if(s == NULL) return false; s->data = x; s->next = NULL; list->last->next = s; list->last = s; list->last->next = list->first; list->size++; return true; } bool push_front(List *list,ElemType x) { Node *s = (Node*)malloc(sizeof(Node)); if(s == NULL) return false; s->data = x; s->next = NULL; list->last->next = s; list->last = s; list->last->next = list->first; list->size++; return true; } void ShowList(List *list) { Node *p = list->first->next; while(p != list->first) { cout<<p->data<<"-->"; p = p->next; } cout<<"Nul"<<endl; }
相关文章推荐
- 数据结构高分笔记 算法2-18~2-19:双向循环链表
- Python写数据结构:单向循环链表
- 数据结构 循环链表、双向链表、一元多项式
- 小猪的数据结构辅助教程——2.4 线性表中的循环链表
- 【数据结构】用C++实现双循环链表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等)
- 数据结构与算法之六 双向链表和循环链表
- 简单数据结构之循环链表(C++实现)
- 数据结构 学习笔记之:线性表——顺序表、静态链表、动态链表(单链表、双链表、单循环链表、双循环链表,链式栈、链式队列)——基础概念大扫盲!
- 数据结构--单表非循环链表
- 数据结构:双向链表实现队列与循环链表
- 数据结构_双向循环链表
- 数据结构-----双向循环链表
- 数据结构之线性结构--双向循环链表
- 线性表12|循环链表 – 数据结构和算法17
- 数据结构之循环链表
- 数据结构--循环链表
- java数据结构之循环双链表
- 数据结构单循环链表
- ※数据结构※→☆线性表结构(list)☆============单向循环链表结构(list circular single)(四)
- 数据结构之设置尾指针的单向循环链表(参考整理严蔚敏数据结构)