Linked List(链表)
来源:互联网 发布:听觉音乐 淘宝 编辑:程序博客网 时间:2024/05/22 15:11
Linked List
/** Node of a singly linked list of strings. */ public class Node { private String element; // we assume elements are character strings private Node next; /** Creates a node with the given element and next node. */ public Node(String s, Node n) { element = s; next = n; } /** Returns the element of this node. */ public String getElement() { return element; } /** Returns the next node of this node. */ public Node getNext() { return next; } // Modifier methods: /** Sets the element of this node. */ public void setElement(String newElem) { element = newElem; } /** Sets the next node of this node. */ public void setNext(Node newNext) { next = newNext; } }1.2 Class for Singly Linked List
<span style="font-family: Arial, Helvetica, sans-serif; color: rgb(255, 0, 128);">/** Node of a singly linked list of strings. */</span><span style="font-family: Arial, Helvetica, sans-serif;"> </span><p><span style="color:#8000A0;"> public</span><span style="color:#FF8000;">class</span>Node { </p><p> <span style="white-space:pre"></span><span style="color:#8000A0;">private String</span> element; <span style="color:#FF0080;">//we assume elements are character strings</span> </p><p> <span style="white-space:pre"></span><span style="color:#8000A0;">private </span>Node next; <span style="color:#FF0080;">/**Creates a node with the given element and next node. */</span> </p><p> <span style="white-space:pre"></span><span style="color:#8000A0;">public </span><span style="color:blue;">Node</span>(<span style="color:#8000A0;">String</span> s, <span style="color:#8000A0;">Node </span>n) { </p><p> element = s; </p><p> next = n; </p><p> } </p><p> <span style="color:#FF0080;">/** Returns the element of thisnode. */</span></p><p> <span style="white-space:pre"></span><span style="color:#8000A0;">public String</span> <span style="color:blue;">getElement</span>() { <span style="color:#FF8000;">return</span>element; }</p><p> <span style="white-space:pre"></span><span style="color:#FF0080;">/** Returns the next node of thisnode. */</span></p><p> <span style="white-space:pre"></span><span style="color:#8000A0;">public </span>Node <span style="color:blue;">getNext</span>() { <span style="color:#FF8000;">return</span>next; } <span style="color:#FF0080;">//Modifier methods:</span></p><p> <span style="white-space:pre"></span><span style="color:#FF0080;">/** Sets the element of thisnode. */</span></p><p> <span style="white-space:pre"></span><span style="color:#8000A0;">public void</span> <span style="color:blue;">setElement</span>(<span style="color:#8000A0;">String</span> newElem) { element = newElem; } </p><p> <span style="white-space:pre"></span><span style="color:#FF0080;">/** Sets the next node of thisnode. */</span></p><p> <span style="white-space:pre"></span><span style="color:#8000A0;">public void</span> <span style="color:blue;">setNext</span>(Node newNext) { next = newNext; }</p><p> <span style="font-family: Arial, Helvetica, sans-serif;">} </span></p>
1.3 Basic Operations:
1.Insertion At Head
a.Allocate a new node
b.Insert the new element
c.Have the new node point to old head
d.Update head to point to the new node
2.Removal At Head
a.Update head to point to the next node
b.Allow garbage collector to reclaim the former first node
3.Insertion At Tail
a.Allocate a new node
b.Insert the new element
c.Have the new node point to null
d.Have the old last node point to new node
e.Update tail to point to new node
4.Removal At Tail
a.Update the last second node to point to null
b.Allow garbage collector to reclaim the last node
2.Doubly Linked List
A doubly linked list is a concrete data structure consisting of a sequence of nodes, and each node contains: element, link to previous node and link to the next node.
2.1 The Node Class For Doubly Linked List
2.2 The Class For Doubly Linked List
/** Doubly linked list .*/ public class DoublyLinkedList { protected Node header; // head node of the list protected Node trailer; // trailer node of the list protected long size; // number of nodes in the list /** Default constructor that creates an empty list */ public DoublyLinkedList() { header = null; trailer = null; size = 0; } // ... update and search methods would go here ... }
Basic Operations:
1.Insertion At Head
2.Insertion In The Middle
3.Removal At Tail
4.Removal In Middle
- Linked List(链表)
- 链表(linked list)
- Singly Linked List(链表)
- C001:数据结构7-链表(Linked list)
- C++链表(single-linked list)
- Leetcode141 - Linked List Cycle(链表)
- Leetcode234 - Palindrome Linked List(链表)
- Leetcode142 - Linked List Cycle II(链表)
- 141. Linked List Cycle (链表)
- 203. Remove Linked List Elements(链表)
- 206. Reverse Linked List (链表)
- 92. Reverse Linked List II(链表)
- 链表---Reverse Linked List
- 数据结构:链表(linked-list)
- Palindrome Linked List---链表
- Linked List 链表
- List(double linked)
- leetcode Palindrome Linked List 链表
- 结构体占内存字节问题.
- 古代赌局——蓝桥杯2012年Java决赛
- hdu 4333 Revolving Digits (扩展kmp)
- 【LeetCode笔记】String to Integer (atoi)
- hdu 1524 A Chess Game 博弈论
- Linked List(链表)
- Data Mining(1)
- WV.55-三种求最大公约数算法比较-连续整数检测法- 欧几里得算法-分解质因数算法
- Soot I: 基本了解
- 关于c++的一些自我认识(3)
- Linux流量监控工具 - iftop
- 嵌入式系统组成
- SQL优化-索引篇
- html5和CSS3超酷购物车结算动画特效