单链表的链接存储结构实现
2014-09-21 11:46
309 查看
#include <iostream> using namespace std; class Node { public: int element; Node *next; }; class List { private: Node *head; public: List();//无参构造函数 List(int a[], int n); //有参构造函数 ~List();//析构函数 void Length(); //求长度 int Min(); void Delete( int n); void Inserte( int m,int n); //在第n个位置插入m void PrintList();//遍历操作 }; List::List() { head = new Node; ///生成一个头结点 head->next = NULL; ///头结点的指针域置空 } //此方法会生成逆序的链表 /*List::List(int a[],int n) { int i; Node* s; head = new Node; head->next = NULL; ///初始化一个空链表 for (i = 0; i < n; i++) { s = new Node; s->element = a[i]; ///为每个数组元素建立一个数据域为a[i]的结点 s->next = head->next; head->next = s; ///将结点s插入到头指针之后 } }*/ //此方法会生成正常序链表 List::List(int a[],int n) { head = new Node; //生成头结点 Node *r,*s; r = head; //尾指针初始化 for (int i = 0; i < n; i++) { s = new Node; s->element = a[i]; //为每个数组元素建立一个结点 r->next = s; r = s; //将结点s插入到终端结点之后 } r->next=NULL; } List::~List() { Node* q; while (head) { ///释放单链表每一个结点的存储空间 q = head; ///暂存被释放结点 head = head->next; ///head指向被释放结点的下一个结点 delete q; } } void List::Length() { Node *p; p=head; int count = 0; while (p) { p = p->next; count++; } cout << count-1 <<endl; } int List::Min() { Node *s,*z; z = head->next; int min = 1000; while (z) { s = z; if (s->element < min) { min = s->element; } z = z->next; } cout << min <<endl; return min; } void List::Delete(int n) { Node *b,*c; b = head; while (b) { if(b->element != n) { c = b; b = b->next; } else { c->next = b->next; b = b->next; return; } } } void List::Inserte(int m,int n) { Node *q,*p,*s; s = new Node; p = head; for ( int i = 0; i < n; i++) { q = p; p = p->next; } s->element = m; s->next = q->next; q->next = s; } void List::PrintList() { Node* p = head->next; ///工作指针p初始化 while (p) { cout << p->element; p = p->next; ///工作指针后移,注意不能写成p++ } } int main() { int a[]={1,2,3,4,5}; List B(a,5); B.Length(); //int n = B.Min(); //B.Delete(n); //B.Inserte(6,2); B.PrintList(); cout << endl; return 0; }
相关文章推荐
- 线性表的链接存储结构(单链表)的实现
- C++ 单链表基本操作分析与实现 链表 链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结
- 栈的链接存储结构---链栈的实现
- 第四周【项目6-2 - 多项式加法在链表存储结构下的实现】
- 栈的链接存储结构--链栈 图解和代码实现
- 采用中序线索链表存储结构,实现中序遍历
- 队列的链接存储结构——链队列 图解和代码实现
- 数据结构(12)--二叉树的静态链表存储和遍历的实现
- 队列的链接存储结构——链队列 图解和代码实现
- 链队列的链接存储结构及实现
- 数据结构(C++版) 栈的链接存储结构及实现
- 线性表的链接存储结构及JAVA实现
- 试分别以顺序表和单链表作存储结构,各写一实现线性表就地逆置的算法
- 单链表数据存储结构(c语言实现)
- 栈的链接存储结构--链栈 图解和代码实现
- 集合链表存储结构的实现和操作
- 【数据结构】线性表的单链表存储结构表示和实现
- 采用中序线索链表存储结构,实现中序遍历
- 数据结构_串_用链表做存储结构实现KMP算法_C++实现
- 数据结构之---C语言实现广义表头尾链表存储表示