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版Tomcat安装
挺好安装的,就是网上资料有的错了. 1.下载Tomcat 网址:http://tomcat.apache.org,解压在~/Downloads 目录下,我的版本是apache-tomcat-7.0.7 ...
- Spring MVC(一)
MVC这种设计模式,不光运用于Web领域,而且也能用于非Web领域,MVC特指一种表现层设计模式,不限于Java语言 spring mvc属于spring框架的后续产品,用在基于MVC的表现层开发,类 ...
- Gym 101102B The Little Match Girl(贪心+规律)
这个题目的做法不止一种,solve1:每个数字使用的火柴棒都在2~7之间,而且是连续的,就是2-7之前没有空着的数.这样就有一个结论,在下界为l,上界为r的情况下,假设有n个数,那么火柴棒总数一定在n ...
- github上一些觉得对自己工作有用的项目收集
usefullProjectCollect github上一些觉得对自己工作有用的项目收集 技能类 markdown语法中文说明 全文检索 elasticsearch bigdesk elastics ...
- Spring 创建 IOC 容器 ClassPathXmlApplicationContext 类
一.ClassPathXmlApplicationContext 类的作用 在 Java 环境中使用 Spring 时,需要用 new ClassPathXmlApplicationContext(c ...
- IE中float元素如果同时设置了margin值,此时margin的值会变为双倍的解决方法
IE中float元素如果同时设置了margin值,此时margin的值会变为双倍, 解决办法: 是在该元素中加入display:inline.
- Haskell 函数式编程
Haskell是纯函数式编程,它强调不函数不改变外部世界状态,即,一个函数的输出只由函数接收的输入决定.那如何与外面沟通呢,比如读取一个文件内容并输出这个文件内容(字符串),显然这种函数非纯函数,因为 ...
- 安卓---高德地图API应用
说明:定位需要导入android_location 的jar包,如果没有会报错,这个官方网站好像找不到,这是我在网上找到的一个链接 http://download.csdn.net/detail/ra ...
- php观察者模式
观察者模式(有时又被称为发布/订阅模式)是软件设计模式的一种.在此种模式中,一个目标对象管理所有相依于它的观察者对象,并且在它本身的状态改变时主动发出通知.这通常透过呼叫各观察者所提供的方法来实现.此 ...
- [ An Ac a Day ^_^ ] hdu 2553 N皇后问题 搜索
曾经想过一天一AC 坚持下来的确不容易额 (我是没坚持下来 尽量以后坚持…… 经典的N皇后问题 搜索的入门问题 学了这么久竟然一直没敲过 今天敲一下…… 这道题也不是很简单额 纯暴力就超时了 要打一下 ...