Data structure (2) Application of linked list

Source: Internet
Author: User

Data structure (2) Application of linked list
A linked list is a basic data structure that is an appropriate type of data in the abstract data structure of the Collection class. Unlike the numeric structure, it is easier to insert or delete elements in the linked list. Definition: a column of elements represented by a linked list, composed of a series of nodes, is a recursive data structure. A node is an abstract entity that can contain any type of data. Its application pointing to a node reflects its role in a linked list. Structure: copy the code private class Node {public T Data; public Node Next; public Node (T data) {Data = data ;}} copy the code above the one-way linked list, the advantage is that it is simple and efficient, but it is not convenient to delete or add any node. In this case, you can use two-way linked list and other data structures. Today, I will show you how to use a one-way linked list to implement the stack and queue data structures. Application: 1. the feature of implementing a generic stack data structure stack is the advanced post-output (FILO). According to this feature, when we design, the Push () method inserts a new node into the head of the linked list, the Pop () method extracts the head node of the linked list. The implementation is very simple. Compared to the previous blog post that used arrays to store data in a structured manner, the use of linked lists is much more flexible. We no longer need to manually adjust the stack capacity to automatically change the size flexibly. The implementation code is as follows: copy the code class NodeStack <T>: IEnumerable {private int N; private Node first; public T firstData () {return first. data;} public int Size () {return N;} public void Push (T item) {N ++; var oldFirst = first; first = new Node (item); first. next = oldFirst;} public T Pop () {-- N; var oldFirst = first; first = first. next; return oldFirst. data;} private class Node {public T Data; public Node Next; Public Node (T data) {Data = data ;}} public IEnumerator GetEnumerator () {var oldFirst = first; while (first! = Null) {var node = first; first = first. next; yield return node. data;} first = oldFirst;} copy Code 2. it is also very easy to implement a generic Queue Based on the linked list data structure. According to its first-in-first-out feature, the instance variable first points to its header during design, the instance variable last points to its tail node. The enqueue () method adds an element to the end of the table. The dequeu () method returns and deletes the header. The implementation code is as follows: copy the code class NodeQueue <T>: IEnumerable {private int N; private Node first; private Node last; public bool isEmpty () {return first = null ;} public int Size () {return N;} public void enqueue (T item) {var oldLast = last; last = new Node (item); if (isEmpty ()) first = last; else oldLast. next = last; N ++;} public T dequeue () {N --; var oldFirst = first; first = first. next; if (isEmpty () la St = null; return oldFirst. data;} private class Node {public T Data; public Node Next; public Node (T data) {Data = data;} public IEnumerator GetEnumerator () {var oldFirst = first; while (first! = Null) {var node = first; first = first. next; yield return node. data;} first = oldFirst;} copy code Summary: the Data structure of the linked list is an important alternative to arrays. This alternative solution has been used for decades. Relatively speaking, the Linked List data structure is more flexible. As learning continues to deepen, two-way linked list and tree related content will continue to be added. By studying linked lists and implementing common API types, you can understand them more flexibly. This simple, flexible, and efficient structure is impressive.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.