Chain storage structure of linear tables _ One-way linked list [leading node] _ C # implementation

Source: Internet
Author: User

[References: 1. yan Weimin. data structure (c); 2. chen Guang. data structure (C # language description); 3. michael McMillan. data Structures and Algorithms Using C #]

1. Chain storage structure of linear tables:

An arbitrary set of storage units (with no address continuity required) are used to store the elements of a linear table. Each element corresponds to a group of storage units (called nodes). Each node contains two fields: data domains for storing data element information and direct Storage

The pointer field of the next position.

A table composed of N nodes by pointer fields is called a Linear Linked List (single-chain table ).

. The pointer field of the last node of the Linear Linked List is "NULL" (NULL or ^ );

. Use a header pointer to indicate the storage location of the first node in the linked list;

. Linked List L = (a1, a2 ,...... An) logical representation:

L-> a1-> a2-> an ^

Empty table: L = NULL.

2. Linked List of the leading node:

A node (header node) is attached to the first element node of the Linear Linked List. Its data field does not store any information, and its pointer field stores the storage location of the first element node. the header pointer L points to this header node.

When the table is empty: L-> next = NULL

The leading node linked list is introduced to make the algorithm empty and process consistent. (The empty table contains the header node)

3. Features of linear table chain storage structure:

(1) Logically adjacent elements, whose physical locations are not necessarily adjacent; the joining relationship between elements is indicated by the pointer field.

(2) The linked list is a non-random access storage structure. The access to the linked list must start from the pointer. The linked list nodes can only access the list in sequence, but the random access performance is poor.

(3) The linked list is a dynamic storage structure. The linked list node can call new () Application and dispose () to release [of course C # uses the Garbage Collector to manage memory].

(4) The insert/delete operation is very convenient. You only need to modify the corresponding pointer value.

The following is a single-chain table class implemented in C:

Using System;
Namespace shortlist
{

Public class upload list
{
// Member
Private int count; // number of record elements; table length; Initial Value: 0
Private Node head;

// Attributes
Public int Count // number of elements in a single-chain table
{
Get {return count ;}
}
// Indexer
Public object this [int index]
{
Get {return Get (index). item ;}
Set {Get (index). item = value ;}
}

// Method

// Constructor
Public shortlist () // construct a single-chain table with a header Node
{
Head = new Node ("head Node ");
}

Private Node Get (int index) // find the elements of the specified index: [1 <= index <= table length]
{
Int j = 1;
Node p = this. head. next; // initialization, p points to the first Node, and j is the counter
While (p! = Null) & (j <index) // find the index node. [the condition for the output loop is: (1) p = null: empty table OR index> table length; AND (2) j = index: The table is located at the index node.]
{
P = p. next;
++ J;
}
If (p = null) | (j> index ))
{
// Console. WriteLine ("empty table or I smaller than 1 or greater than the table length ");
// Console. ReadLine ();
// System. Environment. Exit (0 );
Throw new ArgumentOutOfRangeException ("empty table or I less than 1 or greater than the table length ");

}
Else // *** [condition: p! = Null AND j = index]
{
Return p;
}

// If (p! = Null) & (j = index) // It can also be written like this!
//{
// Return p;
//}
// Else
//{
// Throw new ArgumentOutOfRangeException ("empty table or I less than 1 or greater than the table length ");
//}

}
Public void Add (object value) // Add an element at the end of the linked list
{
Node p = new Node (value );
If (this. head. next = null) // if the table is empty, insert it directly after the header node.
{
Head. next = p;
}
Else
{
Get (count). next = p;
}

Count ++;
}

Public void Insert (int index, object value) // Insert the element before the specified index: [1 <= index <= table length + 1]
{
Int j = 0;
Node p = this. head; // initialization. p points to the header Node, and j indicates the counter.
While (p! = Null) & (j <(index-1) // search for the index-1 node. [the condition for the output loop is: (1) p = null: index> table length + 1; AND (2) j = index-1: located in the previous node of index]
{
P = p. next;
++ J;
}
If (p = null) | (j> (index-1) // if the index is smaller than 1 or greater than the table length, and 1 is added. [ERROR return condition: (1) p = null: index> table length + 1; OR (2) j> (index-1): index position <1]
{
Throw new ArgumentOutOfRangeException ("I is less than 1 or greater than the table length plus 1 ");
}
Else // *** [condition: p! = Null AND j = index-1]
{
Node s = new Node (value); // generate a new Node
S. next = p. next; // insert
P. next = s;

Count ++; // table length + 1

}
}

Public void Delete (int index) // Delete the specified index element: [1 <= index <= table length]
{
Int j = 0;
Node p = this. head; // initialization. p points to the header Node, and j indicates the counter.
While (p. next! = Null) & (j <(index-1) // search for the index-1 node. [the condition for the output loop is: (1) p. next = null: index> table length; (2) j = index-1: The previous node located in the index]
{
P = p. next;
++ J;
}
If (p. next = null) | (j> (index-1) // index smaller than 1 or greater than the table length [ERROR return condition: (1) p. next = null: index> table length; OR (2) j> (index-1): index position <1]
{
Throw new ArgumentOutOfRangeException ("I less than 1 or greater than the table length ");
}
Else // *** [condition: p. next! = Null AND j = index-1]
{
P. next = p. next. next; // Delete

Count --; // table length-1
}
}

Public override string ToString () // print the entire linked list
{
String s = string. Empty;
For (Node p = head. next; p! = Null; p = p. next)
{
S + = p. item. ToString () + "";
}
Return s;

}
Private class Node // Node class
{
Public object item; // data field
Public Node next; // pointer Field

Public Node ()
{
Item = null;
Next = null;
}
Public Node (object value)
{
Item = value;
Next = null;
}
}

}
}

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.