Single-chain table C ++ implementation for linear tables, linear single-chain
Single-chain table of linear table
I. header file: Listing list. h
// A single-chain table stores the elements of a linear table with any storage unit, which can be continuous or discontinuous, or even scattered in any location in the memory.
// Single-Chain header file
# Include <iostream>
Using namespace std;
// Define the node of a single-chain table-struct type
Template <class DataType>
Struct Node
{
// Data field, which stores the data of this node
DataType data;
// Pointer field pointing to the next node
Node <DataType> * next;
};
Template <class DataType>
Class program list {
Public:
// No parameter constructor for a single-chain table
Vertex list ();
// A single-chain table has a parameter constructor.
LinkedList (DataType array [], int n );
LinkedList (int n, DataType array []);
// Single-chain table destructor
~ Vertex list ();
// Obtain the length of a single-chain table
Int GetLength ();
// Search for the serial number of the specified element in a single-chain table
Int GetLocal (DataType x );
// Obtain the sequence number of a single-chain table
DataType GetElement (int index );
// Insert the specified element at the specified position in a single-chain table
Void Insert (int index, DataType x );
// Delete the element at the specified position in the single-link table
DataType Delete (int index );
// Output elements in a single-chain table by serial number
Void printmedialist ();
Private:
// Declare the header pointer of a single-chain table
Node <DataType> * first;
};
// Implement the no-argument constructor of a single-chain table
Template <class DataType>
Upload list <DataType>: Upload list ()
{
First = new Node <DataType>;
First-> next = NULL;
}
// Create a single-chain table by using the header Insertion Method
Template <class DataType>
LinkedList <DataType >:: LinkedList (int n, DataType array [])
{
// Initialize an empty linked list
First = new Node <DataType>;
First-> next = NULL;
For (int I = 0; I <n; I ++)
{
// Apply for a new node for each array element
Node <DataType> * s = new Node <DataType>;
// Assign an array element to the node data field
S-> data = array [I];
// Before inserting the node into the header Node
S-> next = first-> next;
First-> next = s;
}
}
// Create a single-chain table by means of end insertion
Template <class DataType>
LinkedList <DataType >:: LinkedList (DataType array [], int n)
{
// Generate the header Node
First = new Node <DataType>;
// Define the End Node
Node <DataType> * r = first;
For (int I = 0; I <n; I ++)
{
// Apply for a node for each array element
Node <DataType> * s = new Node <DataType>;
// Assign an array element to the data field of the node
S-> data = array [I];
// After each node is appended to the terminal node
R-> next = s;
R = s;
}
// The End Node is NULL.
R-> next = NULL;
}
// Implement the destructor of a single-chain table
Template <class DataType>
LinkedList <DataType> ::~ Partition list ()
{
// Declare the working pointer
Node <DataType> * q;
While (first! = NULL)
{
// Save the released Node
Q = first;
// Point the header pointer to the next node of the node to be released
First = first-> next;
Delete q;
}
}
// Insert a single-chain table: Insert a specified element at a specified position
Template <class DataType>
Void upload list <DataType>: Insert (int index, DataType x)
{
// Define the working pointer
Node <DataType> * p = first-> next;
// Define the counter. The initial value is 0.
Int count = 0;
While (p! = NULL & count <index-1)
{
// Move the working pointer behind
P = p-> next;
Count ++;
}
// Locate index-1
If (p = NULL)
{
Throw "the inserted position is incorrect ";
}
Else
{
// Apply for a new node
Node <DataType> * s;
S = new Node <DataType>;
// Its data domain is x
S-> data = x;
// Store the pointer field of the working pointer p in the pointer field of the new node
S-> next = p-> next;
// Insert node s to node p
P-> next = s;
}
}
// Implement value-based search for a single-chain table, and return the sequence number of the specified Element in the single-chain table (if it does not exist, return 0)
Template <class DataType>
Int sort list <DataType >:: GetLocal (DataType x)
{
// Define the working pointer
Node <DataType> * p = first-> next;
// Define the counter. The initial value is 1.
Int count = 1;
// Find the position corresponding to the serial number
While (p! = NULL)
{
If (p-> data = x)
{
Return count;
}
// Move the working pointer behind
P = p-> next;
// Add 1 to the counter
Count ++;
}
// If this element cannot be found, 0 is returned.
Return 0;
}
// Implement bitwise search for a single-chain table and return the elements at the specified position
Template <class DataType>
DataType category list <DataType >:: GetElement (int index)
{
// Define the working pointer
Node <DataType> * p = first-> next;
// Define the counter. The initial value is 1.
Int count = 1;
// Find the position corresponding to the serial number
While (p! = NULL & count <index)
{
// Move the working pointer behind
P = p-> next;
// Add 1 to the counter
Count ++;
}
// If the end of a single-chain table cannot be found, an exception is thrown.
If (p = NULL)
{
Throw "Incorrect search location ";
}
Else
{
// When a proper position is found, the elements at the position are returned.
Return p-> data;
}
}
// Obtain the length of a single-chain table
Template <class DataType>
Int parameter list <DataType >:: GetLength ()
{
// Define a counter to calculate the length of a single-chain table
Int count = 0;
// Define the working pointer
Node <DataType> * p = first-> next;
While (p! = NULL)
{
P = p-> next;
Count ++;
}
Return count;
}
// Output elements of a single-chain table by serial number
Template <class DataType>
Void upload list <DataType >:: printmedialist ()
{
// Declare the working pointer
Node <DataType> * p;
// Initialize the working pointer
P = first-> next;
While (p! = NULL)
{
Cout <p-> data <"";
// Move the working pointer backward
P = p-> next;
}
Cout <endl;
}
// Delete a single-chain table
Template <class DataType>
DataType category list <DataType>: Delete (int index)
{
Node <DataType> * p = first-> next;
Int count = 0;
// Search for the index-1 Node
While (p! = NULL & count <index-1)
{
P = p-> next;
Count ++;
}
// If you can find
If (p = NULL | p-> next = NULL)
{
Throw "The deletion location is incorrect ";
}
Else
{
Node <DataType> * q = p-> next;
DataType x = q-> data;
P-> next = q-> next;
Delete q;
Return x;
}
}
Ii. Test the source file of a single-chain table of a linear table: test1_list. cpp
# Include <iostream>
# Include "listing list. h"
Using namespace std;
Void show ()
{
Cout <"---------------------------------------" <endl;
}
Int main ()
{
Int array [] = {1, 3, 5, 2, 7, 6, 9, 8, 10, 4 };
// Declare a single-chain table
Shortlist <int> shortlist = shortlist <int> (10, array );
Cout <"output single-chain table:" <endl;
Listing list. printmedialist ();
Show ();
Cout <"length of a single-chain table:" <shortlist. GetLength () <endl;
Cout <"the 5th elements in a single-chain table are:" <shortlist. GetElement (5) <endl;
Cout <"the position of element 5 in a single-chain table is:" <shortlist. GetLocal (5) <endl;
Show ();
Cout <"insert element 22 on the 5th locations of a single-chain table" <endl;
Into list. Insert (5, 22 );
Cout <"output single-chain table:" <endl;
Listing list. printmedialist ();
Cout <"length of a single-chain table:" <shortlist. GetLength () <endl;
Show ();
Cout <"delete element at location 5th" <endl;
Repeated list. Delete (5 );
Cout <"output single-chain table:" <endl;
Listing list. printmedialist ();
Cout <"length of a single-chain table:" <shortlist. GetLength () <endl;
Show ();
Return 0;
}