Create and operate a C ++ linked list

Source: Internet
Author: User

We know that an array-type computer will automatically allocate a continuous storage unit to it based on the pre-defined array type and length. The positions and distances of the same array are fixed, that is, the address of any array element can be calculated using a simple formula. Therefore, this structure can effectively perform random access to array elements. However, if an array element is inserted or deleted, a large amount of data is moved, making simple data processing very complicated and inefficient.
To effectively solve these problems, a data structure called "linked list" has been widely used.
1. Linked List Overview
A linked list is a dynamic data structure that stores data elements in a group of any storage unit (continuous or discontinuous.
Each element in the linked list becomes a "Node", and each node is composed of a data field and a pointer field. The pointer field in each node points to the next node. Head is the "head Pointer", indicating the start of the linked list. It is used to point to the first node. the pointer field of the last pointer is null (empty address), indicating the end of the linked list.
It can be seen that the linked list structure can only be achieved through pointers, that is, a node must contain a pointer variable to store the address of the next node.
In fact, each node in the linked list can use several data and several pointers. The linked list with only one pointer in the node is called a single-chain table, which is the simplest chain table structure.
Implementing a single-chain table in C ++ is relatively simple. For example, the simplest form of a single-chain table can be defined is as follows:
Struct Node
{
Int data;
Node * next;
};
The struct type is used here. * Next is the pointer field used to point to the next node of the node. Data is an integer variable used to store data in the node. Of course, data can be any data type, including struct type or class type.
On this basis, we define a list of linked list classes, including member functions such as insertion, deletion, and output of linked list nodes.

Class list
{
Node * head;
Public:
List () {head = NULL ;}
Void insertlist (INT adate, int bdate); // insert a linked list Node
Void deletelist (INT adate); // delete a linked list Node
Void outputlist (); // output of the linked list Node
Node * gethead () {return head ;}
};
2. Access to linked list nodes
Since each node in the linked list is linked by a pointer, its storage unit is continuous. Therefore, the address of any node cannot be the same as that of an array, A simple formula is used for random access. You can only start from the head pointer (head) of the linked list. Use a pointer P to point to the first node, and then locate the next node based on node p. And so on until you find the node you want to access or the last node (the pointer is null.
The output function of the above linked list is given below;
Void list: outputlist ()
{
Node * Current = head;
While (current! = NULL)
{
Cout <Current-> data <"";
Current = Current-> next;
}
Cout <Endl;
}
3. Insert a linked list Node
If you want to insert Node B before node A in the linked list, consider the following situations.
(1) The linked list before insertion is an empty table. After inserting the new node B.
(2) If a is the first node of the linked list, after insertion, Node B is the first node.
(3) If a exists in the linked list and is not the first node, first find the previous node A_k of A and then point the pointer field of A_k to B, insert the pointer field of B to.
(4) If a does not exist in the linked list, it is inserted at the end. First find the last node a_n of the linked list, and then point the pointer field of a_n to Node B, while the pointer of pointer B is null.
The following is the node insertion function of the linked list class. Obviously, it also has the function of creating a linked list.
Void list: insertlist (INT adate, int bdate) // set adate to data in node A, and bdate to data in Node B
{
Node * P, * q, * s; // P points to node A, Q points to node A_k, and s points to Node B
S = (node *) New (node); // dynamically allocates a new node.
S-> DATA = bdate; // Set B to this node
P = head;
If (Head = NULL) // If the table is empty, use B as the first node.
{
Head = s;
S-> next = NULL;
}
Else
If (p-> DATA = adate) // if A is the first node
{
S-> next = P;
Head = s;
}
Else
{
While (p-> data! = Adate & P-> next! = NULL) // query node
{
Q = P;
P = p-> next;
}
If (p-> DATA = adate) // if there is a node
{
Q-> next = s;
S-> next = P;
}
Else // if there is no node;
{
P-> next = s;
S-> next = NULL;
}
}
}
4. Delete linked list nodes
If you want to delete node A in the linked list and release the storage space occupied by the deleted node, consider the following situations.
(1) If node A to be deleted is the first node, point the head to the next node of node.
(2) If node A to be deleted exists in the linked list, but is not the first node, the pointer field of the last node a_k-1 of a should point to the next node A_k + 1 of.
(3) If the empty table or node A to be deleted does not exist, no changes are made.
The following are the Node Deletion functions of the linked list class.
Void list: deletelist (INT adate) // set adate to be a data member in node A to be deleted
{
Node * P, * q; // P is used to point to node A, and Q is used to point to the previous node of node.
P = head;
If (P = NULL) // If the table is empty
Return;
If (p-> DATA = adate) // if A is the first node
{
Head = p-> next;
Delete P;
}
Else
{
While (p-> data! = Adate & P-> next! = NULL) // query node
{
Q = P;
P = p-> next;
}
If (p-> DATA = adate) // If node A exists
{
Q-> next = p-> next;
Delete P;
}
}
}
Example: Use the insertlist and deletelist. outputlist functions of the above three linked lists to form the following simple Linked List Operation Program.
# Include "iostream. H"
Struct Node
{
Int data;
Node * next;
};
Class list
{
Node * head;
Public:
List () {head = NULL ;}
Void insertlist (INT aData, int bdata );
Void deletelist (INT aData );
Void outputlist ();
Node * gethead () {return head ;}
};

Void list: insertlist (INT aData, int bdata) // set aData to data in node A, and bdata to data in Node B
{
Node * P, * q, * s; // P points to node A, Q points to node A_k, and s points to Node B
S = (node *) New (node); // dynamically allocates a new node.
S-> DATA = bdata; // Set B to this node
P = head;
If (Head = NULL) // If the table is empty, use B as the first node.
{
Head = s;
S-> next = NULL;
}
Else
If (p-> DATA = aData) // if A is the first node
{
S-> next = P;
Head = s;
}
Else
{
While (p-> data! = AData & P-> next! = NULL) // query node
{
Q = P;
P = p-> next;
}
If (p-> DATA = aData) // if there is a node
{
Q-> next = s;
S-> next = P;
}
Else // if there is no node;
{
P-> next = s;
S-> next = NULL;
}
}
}
Void list: deletelist (INT aData) // set aData to be a data member in node A to be deleted
{
Node * P, * q; // P is used to point to node A, and Q is used to point to the previous node of node.
P = head;
If (P = NULL) // If the table is empty
Return;
If (p-> DATA = aData) // if A is the first node
{
Head = p-> next;
Delete P;
}
Else
{
While (p-> data! = AData & P-> next! = NULL) // query node
{
Q = P;
P = p-> next;
}
If (p-> DATA = aData) // if there is node
{
Q-> next = p-> next;
Delete P;
}
}
}
Void list: outputlist ()
{
Node * Current = head;
While (current! = NULL)
{
Cout <Current-> data <"";
Current = Current-> next;
}
Cout <Endl;
}
Void main ()
{
List A, B;
Int data [10] = {1,121 };
A. insertlist (0, data [0]); // create the first node of A in the linked list.
For (INT I = 1; I <10; I ++)
A. insertlist (0, data [I]); // insert data in sequence.
Cout <"/N linked list :";
A. outputlist ();
A. deletelist (data [7]);
Cout <"after deleting the element data [7 ";
A. outputlist ();
B. insertlist (0, data [0]); // create the first node of Table B.
For (I = 0; I <10; I ++)
B. insertlist (B. gethead ()-> data, data [I]); // insert data at the first node in sequence.
Cout <"/N linked list B :";
B. outputlist ();
B. deletelist (67 );
Cout <"after deleting element 67 ";
B. outputlist ();
}
The program running result is
Linked list A; 1,121
After the element data [7] is deleted;
121
Linked List B,
Delete element 67;
, 25,

The following is the code of the Yang Hui triangle:
# Include <iostream>
# Include <iomanip>
Using namespace STD;
Int main ()
{
Const int n = 11;
Int I, j, a [n] [N];
For (I = 1; I <n; I ++)
{
A [I] [I] = 1;
A [I] [1] = 1;
}
For (I = 3; I <n; I ++)
{
For (j = 2; j <= I-1; j ++)
A [I] [J] = A [I-1] [J-1] + A [I-1] [J];
}
For (I = 1; I <n; I ++)
{
For (j = 1; j <= I; j ++)
Cout <SETW (5) <A [I] [J] <"";
Cout <Endl;
}
Cout <Endl;
Return 0;
}

Related Article

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.