List------Linked 链表
1、Definition
Linked list consists of a series of nodes. Each nodes contains the element and a pointer which points to the next node. The last node's next link points to NULL.
Linked=data+pointer
use the pointer to describe the logical relationship

2、Implementation
template<class List_entry>
class List
{
public:
List();
int size();
bool empty();
bool full();
...
protected:
int count;
Node<List_entry> *head; //当声明一个linked时,只会拥有一个头指针
} template<class List_entry>
struct Node
{
List_entry entry; // data
Node<List_entry> *next; // point to the next node
Node();
Node(List_entry, Node<List_entry>*link=NULL);
}
3、Operations
(1)two types of insert
①在p节点所在位置插入

New(S)
S->data=a;
q->next=S;
S->next=p;
②在p节点之后插入

New(S)
S->data=a;
S->next=p->next;
p->next=S;
(2) create a linked list
In order to create a linked list, we have this algorithm
①To create a head
②To create new node S
③Insert S



void CreateList(Head)
{
new(Head);
Head->next=NULL;
scanf("%c",ch);
while(ch<>'#')do
{
new(S);
S->data=ch;
S->next=Head->next;
Head->next=S;
}
}
上述构造方式为从头构造,在头元素出一个一个地插入
下面一种是从链尾增添
void CreateList(Head)
{
new(Head);
Head->next=NULL;
Last=Head;
scanf("%c",ch);
while(ch<>'#')do
{
new(S);
S->data=ch;
S->next=NULL;
Last->next=S;
Last=S;
}
}
(3)insert an element at location i
Status ListInsert L(LinkList &L, int i, DataType e)
{
LinkList p, s;
p=L;
int j=0; while(p&&j<i-1)
{
p=p->next;
j++;
} if(!p) return ERROR;
s=(LinkList)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s; return OK;
}
(4)Delete
Status ListDelete(LinkList &L, int i)
{
LinkList p, q;
p=L;
int j=0; while(p&&j<i-1)
{
p=p->next;
j++;
} if(!p) return ERROR; q=p->next;
p->next=q->next;
free(q);
}
(5)Search
①按位查找
Status ListSearch(LinkList &L, int i)
{
LinkList p;
int j=0;
while(p&&j<i-1)
{
p=p->next;
j++;
}
if(!p)return ERROR;
e=p->next;
return OK;
}
③按值查找
Status ListSearch(LinkList &L, DataType e)
{
LinkList p;
p=L;
int j=0;
while(p&&(p->next)!=e)
{
p=p->next;
j++;
}
if(!p)
{
cout<<"Not found"<<endl;
return ERROR;
}
else
{
return (j);
}
}
3、circular linked list

4、Doubly linked list

(1)Insert

p->next=current->next;
p->prior=current;
current->next->prior=p;
current->next=p;
(2)Delete

current->next->prior=current->prior;
current->prior->next=current->next;
C++style code
//C++style构建链表,有头结点
//operations
/*
LenList(L)链长
GetElem(i, e)换值
SearchElem(e, i)按值查找
InertElem(i, e)插值
DeleteElem(i) 删值
*/ template<typename Type>
struct Node
{
Type data;
Node<Type>*next;
}; template<typename Type>
class LinkList
{
public:
LinkList()
{
head = new Node<Type>;
head->next = NULL;
len = 0;
Type ch;
Node<Type>*p;
while (cin >> ch)
{
p = new Node<Type>;
p->data = ch;
p->next = head->next;
head->next = p;
len++;
}
}
~LinkList()
{
Node<Type>*p = head;
while (p->next)
{
DeleteElem();
}
delete head;
} //LenList(L)链长
int LenList()
{
return len;
} //InertElem(e)从尾插值
void InsertElem(Type e)
{
Node<Type>*p = head;
while (p->next)
{
p = p->next;
} Node<Type>*q = new Node<Type>;
q->data = e;
q->next = p->next;
p->next = q;
len++;
} //InertElem(i, e)从第i位插值
void InsertElem(int i, Type e)
{
Node<Type>*p = head;
int j = 0;
while (p->next && j<i - 1)
{
j++;
p = p->next;
}
Node<Type>*q = new Node<Type>;
q->data = e;
q->next = p->next;
p->next = q;
len++;
} //GetElem(i, e)换值
void GetElem(int i, Type e)
{
int j = 0;
Node<Type>*p = head;
while (p->next && j<i - 1)
{
j++;
p = p->next;
}
p->next->data = e;
} //DeleteElem(i) 第i位删值
void DeleteElem(int i)
{
int j = 0;
Node<Type>*p = new Node;
while (p->next && j<i - 1)
{
j++;
p = p->next;
} Node<Type>*q = p->next;
p->next = q->next;
delete q; len--;
} //DeleteElem() 尾删值
void DeleteElem()
{
Node<Type>*p = head;
while (p->next->next)
{
p = p->next;
}
Node<Type>*q = p->next;
p->next = q->next;
delete q;
len--;
} //SearchElem(e)按值查找
int SearchElem(Type e)
{
Node<Type>*p = head;
int i = 0;
while (p->next && p->data != e)
{
i++;
p = p->next;
} if (i == 0)
return -1;
else return i;
}
//SearchElem(i)按位查找
Type SearchElem(int i)
{
Node<Type>*p = head;
int j = 0;
while (p->next && j<i)
{
j++;
p = p->next;
}
return p->data;
} private:
Node<Type>*head;
int len;
};
List------Linked 链表的更多相关文章
- GO语言数据结构之链表
链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点包括两个部分: ...
- [置顶]
一个demo学会c#
学习了c#4.5高级编程这本书,自己喜欢边学边总结边写demo,所以写了这篇文章,包含了大部分的c#编程知识.让你一个demo掌握c#编程,如果有问题可以留言. 此demo主要包括五个文件:Stude ...
- 一行一行读Java源码——LinkedBlockingQueue
1.LinkedBlockingQueue概述 Linked:链表+Blocking:阻塞+Queue:队列 Queue:首先想到的是FIFO Linked:,Queue:其结构本质上是线性表,可以有 ...
- java 基础词汇 必须 第九天
Collection 集合 List 列表集合 Set 不重复集合 Linked 链表 Vector 线程安全集合 Hash 哈希值 tree 树型结构 Map 键值对集合 add 增加 remove ...
- 集合类(Collection和Map接口)简介
集合分为Collection和Map,详细分类如下图所示: 以下是测试验证代码: //HashSet,无序(存取不一致).去重 Set set_H = new HashSet(); set_H.add ...
- [LeetCode] Linked List Random Node 链表随机节点
Given a singly linked list, return a random node's value from the linked list. Each node must have t ...
- [LeetCode] Plus One Linked List 链表加一运算
Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...
- [LeetCode] Odd Even Linked List 奇偶链表
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- [LeetCode] Delete Node in a Linked List 删除链表的节点
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- [LeetCode] Palindrome Linked List 回文链表
Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...
随机推荐
- mac版VMware fusion
百度网盘链接:链接: https://pan.baidu.com/s/1o8BAsrg 安装教程网上很多的,首先要下载一个window 10或其他版本的iso镜像文件,然后很好安装的.
- sql语句的学习(2)
7.统计:学号.姓名.语文.数学.英语.总分.平均成绩 8.列出各门课程的平均成绩.课程,平均成绩 9.列出数学成绩的排名(要求显示字段:学号,姓名,成绩,排名) 10.列出数学成绩在2-3名的学生( ...
- angular指令
转自:http://www.cnblogs.com/rohelm/p/4051437.html 对于指令,可以把它简单的理解成在特定DOM元素上运行的函数,指令可以扩展这个元素的功能. 首先来看个完整 ...
- Windows后渗透
My 命令行下收集主机信息 使用wmic识别安装到系统中的补丁情况: wmic qfe get description,installedOn 识别正在运行的服务: sc query type= se ...
- 《JS正则表达式》
1.精通 JS正则表达式: http://www.cnblogs.com/aaronjs/archive/2012/06/30/2570970.html 2.js常用正则表达式: http://www ...
- Qt ImageProvider 的使用
QQuickImageProvider 是一个可以支持在QML中使用 qpixmap 和 图片加载线程的类. 它支持在qml中使用Image加载的高级特性, 包括 使用 QPixmap 替代实际的im ...
- apache 安装[转]
Apache简介 Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广 ...
- XmlNode和XmlElement区别
今天在做ASP.NET操作XML文档的过程中,发现了两个类:XmlNode和XmlElement.这两个类的功能极其类似(因为我们一般都是在对Element节点进行操作).上网搜罗了半天,千篇一律的答 ...
- SSL读书笔记
摘要: 第一次写博客,为读书笔记,参考书目如下: <HTTP权威指南> <图解HTTP> <大型分布式网站架构设计与实践> 作者:陈康贤 一. HTTP+SSL=H ...
- 第1章 初识java----输出多行的语句写法
public class onesixtwo{ public static void main(String[] args){ System.out.println("----------- ...