单循环链表(头指针指在头结点)『3』
2011-03-24 23:00
260 查看
为了便于学习单循环链表。
没有写那么多函数。
只写了一个插入函数。便于理解,这种情况下是,只有一个空的头结点,然后插入一个节点,既是在头部插入节点,也是在尾部插入节点。其他情况比这个情况都易于理解。所以只要会这一个,其他就都会了。
这个单循环链表(头指针指在头结点)。
强调:数据结构不是为了会写程序,只是利用程序设计语言来掌握一种思想。
VS2008下编译通过。调试成功。
没有写那么多函数。
只写了一个插入函数。便于理解,这种情况下是,只有一个空的头结点,然后插入一个节点,既是在头部插入节点,也是在尾部插入节点。其他情况比这个情况都易于理解。所以只要会这一个,其他就都会了。
这个单循环链表(头指针指在头结点)。
强调:数据结构不是为了会写程序,只是利用程序设计语言来掌握一种思想。
/* author:star Data:2011.04.23 一个简单的单循环链表。 */ #include<iostream> #include<stdio.h> #include<malloc.h> using namespace std; typedef int Status; typedef int ElemType; #define ERROR 0 #define OK 1 #define LEN sizeof( LNode ) #define OVERFLOW 0 typedef struct LNode { ElemType data; struct LNode *next; }LNode,*LinkList; void InitList( LinkList &L ) { L = ( LinkList ) malloc ( sizeof( LNode ) ); if( !L ) exit( OVERFLOW ); L ->next= L; } int ListLength( LinkList L ) { LNode *p = L ->next ; int length = 0; while ( p!=L ) { length++; p = p->next; } return length; } Status ListInsert( LinkList &L, int i, ElemType e) {//在带头结点的单循环链表中第i个位置之前插入元素e if ( i < 1 || i > ListLength( L ) + 1 ) return ERROR; LinkList p,s; int j = 0; p = L ->next; while( ( p != L ) && j < i - 1 )//与单链表不同之处 { p = p ->next; j++; } s = ( LinkList ) malloc ( sizeof ( LNode ) ); s ->data = e; s ->next = p ->next; p ->next = s; return OK; } void outList( LinkList L ) { LinkList p; p = L->next; while( ( p != L ) ) { cout << p ->data<< " "; p = p ->next; } cout << "/n"; } int main() { LinkList head; int i =0; InitList( head ); i = ListInsert( head, 1, 5 ); if( i == ERROR ) cout << "Insert ERROR!"; else outList( head ); system("pause"); return 0; }
VS2008下编译通过。调试成功。
相关文章推荐
- 无头节点的单循环链表中删除指针s所指结点的前驱结点
- [转载]链表、头指针、头结点
- 题目:将二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向
- 复杂链表的复制(结点包含随机指针)
- C++不带头结点的单循环链表解决约瑟夫环问题
- [求助]C语言单链表头指针 头结点 首元节点关系
- 链表、头指针、头结点
- 线性链表中的头结点和头指针
- 结构之美:单链表的头结点与头指针
- 有两个双向循环链表A,B,知道其头指针为:pHeadA,pHeadB,请写一函数将两链表中data值相同的结点删除
- 给定链表的头指针和一个结点指针,在O(1)时间删除该结点
- 输入完全二叉树的先序序列,用#代表虚结点(空指针),如ABD###CE##F##,建立二叉树的二叉链表。 (2)写出对用二叉链表存储的二叉树进行先序、中序和后序遍历的递归和非递归算法。 (3)写出
- 链表 头指针 头结点
- 链表、头指针、头结点
- 数据结构学习笔记(二) 链表之带表尾指针的单循环链表
- 在链表中使用头结点与尾指针
- 有两个双向循环链表A,B,知道其头指针为:pHeadA,pHeadB,请写一函数将两链表中data值相同的结点删除
- 链表中头指针和头结点
- 数据结构——单链表头指针与头结点
- 链表、头指针、头结点(经典)