(单向)循环链表类
来源:互联网 发布:java的quartz 编辑:程序博客网 时间:2024/06/13 05:22
循环链表类:文件名:linked_CList.h
#include <iostream>using namespace std;template <class T>struct node{T d;node * next;};template <class T>class linked_CList{private:node<T> *head; //循环表头指针public:linked_CList(); void prt_linked_CList();//int flag_linked_CList();void ins_linked_CList(T,T);//在包含元素x的结点前面插入新元素bint del_linked_CList(T);//删除包含元素x的结点};template <class T>linked_CList<T>::linked_CList(){node<T> *p;p=new node<T>;p->d=0;p->next=p;head=p;return;}template<class T>void linked_CList<T>::prt_linked_CList(){node<T> *p;p=head->next;if(p==head){cout<<"空的循环链表:"<<endl;return;}do {cout<<p->d<<endl;p=p->next;} while (p!=head);return;}//在包含元素x的结点前插入新元素btemplate<class T>void linked_CList<T>::ins_linked_CList(T x,T b){node <T> *p,*q;p=new node<T>;p->d=b;q=head;while((q->next!=head)&&((q->next)->d)!=x){q=q->next;}p->next=q->next;q->next=p;return;}template<class T>int linked_CList<T>::del_linked_CList(T x){node<T> *p,*q;q=head;while((q->next!=head)&&(((q->next)->d)!=x))q=q->next;if(q->next==head)return 0;p=q->next;q->next=p->next;delete p;return 1;}
应用实例:
#include "linked_Clist.h"
#include <stdlib.h>
int main()
{
linked_CList<int> s;
cout<<"第一次扫描输出循环链表s中的元素:"<<endl;
s.prt_linked_CList();
s.ins_linked_CList(10,10);
s.ins_linked_CList(10,20);
s.ins_linked_CList(10,30);
s.ins_linked_CList(40,40);
cout<<"第二次扫描输出循环链表s中的元素:"<<endl;
s.prt_linked_CList();
if(s.del_linked_CList(30))
cout<<"删除元素:30"<<endl;
else
cout<<"循环链表中没有元素30"<<endl;
if(s.del_linked_CList(50))
cout<<"删除元素:50"<<endl;
else
cout<<"循环链表中没有元素50"<<endl;
cout<<"第3次扫描输出循环链表s中的元素:"<<endl;
s.prt_linked_CList();
system("pause");
return 0;
}
实验结果:
- (单向)循环链表类
- 链表(单向、双向、单向循环、双向循环)
- 单向循环链表
- 单向循环链表
- 单向循环链表
- 单向循环链表
- 循环单向链表
- 单向循环链表
- 循环单向链表
- 单向链表循环
- 循环单向链表
- 单向循环链表
- 单向循环队列
- 单向循环链表
- 单向循环链表
- 单向循环链表
- 单向循环链表
- 单向循环链表
- Annotation & Enum :Java中注解和枚举的使用
- NetBeans 7.2支持 Swing Application Framework
- vc2008中将CString 转换为整数
- 用C++设计一个不能被继承的类
- c++实现字符串翻转
- (单向)循环链表类
- java文件输入和输出
- HTML DOM setInterval() 方法
- java web学习规划
- Windows 7系统安全模式的实用作用
- 导出CListCtrl控件到Excel文件
- 【模拟】拼写检查
- 插入、选择排序
- mysql自动化备份方案