A chained linear table.
Before implementation, you must first understand the characteristics of the Chain Mode of the linear table:
In general, chained linear tables are logically adjacent elements, but physical locations are not necessarily adjacent. Since physical locations are not adjacent
How does one express its logical location? The logical relationship is maintained by the previous element. Each element of a chained linear table is
Stored in a node, the node is a memory area. It contains two regional data domains and pointer domains. Stored element values in the data domain
The pointer field stores the direction of the next node.
How is it? Let's look at the node data structure:
[Cpp] v
Typedef struct LNode
{
ElemType data; // node data Information Field
Struct LNode * next; // pointer to the next node
} LNode, * LinkList;
Typedef struct LNode
{
ElemType data; // node data Information Field
Struct LNode * next; // pointer to the next node
} LNode, * LinkList;
Then let's take a look at its specific implementation and operations, and make a summary of the chained linear table.
[Cpp]
/**
Author Kiritor
Chained Implementation of linear tables */
# Include "stdafx. h"
# Include <stdio. h>
# Include <stdlib. h>
# Include <conio. h>
# Include <malloc. h>
# Define OK 1
# Define OVERFLOW-1
# Define ERROR-1
# Define TRUE 1
# Define FALSE 0
# Define ElemType int
// Storage structure of a single-chain table of a linear table
Typedef struct LNode
{
ElemType data; // node data Information Field
Struct LNode * next; // pointer to the next node
} LNode, * LinkList;
// Initialize a single-chain table
Int init_List (LinkList & l)
{
Printf ("initialize linear table \ n ");
// Generate a header node and point L to the header Node
L = (LinkList) malloc (sizeof (LNode ));
If (! L)
Exit (OVERFLOW); // failed to allocate space
L-> next = NULL; // the pointer field of the header node is NULL, indicating no element.
L-> data = 0; // The data field of the header node is used to indicate the length of the linked list.
Return OK;
}
// Destroy the chained unidirectional linear table
Void destory_List (LinkList & l)
{
Printf ("Destroy linear table ");
LinkList q;
/* Release the memory space of each node cyclically */
While (l)
{
Q = l-> next;
Free (l );
L = q;
}
}
// Reset the linear table to an empty table instead of destroying it.
Int clear_List (LinkList & l)
{
Printf ("clear linear table, not destroy \ n ");
LinkList p, q;
P = l-> next; // p points to the first node
While (p)
{
Q = p-> next;
Free (p );
P = q;
}
// Then, set the direction of the header pointer field to null.
L-> next = NULL;
L-> data = 0;
Return OK;
}
// Insert a linear table (Leading node)
// Insert e before position I, and the I value starts from 1
Int insert_List (LinkList & l, int I, ElemType e)
{
Printf ("insert % d \ n", I, e in position % d );
// First look for the I-th Node
LinkList q = l, s;
Int j = 0; // counter used for Loop
While (q & j <i-1)
{
Q = q-> next;
J ++;
}
If (! Q | j> I-1)
{
Return ERROR;
}
S = (LinkList) malloc (sizeof (LNode); // generate a new node
S-> data = e;
S-> next = q-> next;
Q-> next = s;
L-> data ++; // linear table length plus 1
Return OK;
}
/* The element that obtains the I position of the linear table. If it exists, Return OK and put it in e.
Otherwise, an ERROR is returned. Note that l is the single-chain table of the leading node.
*/
Int getElem (LinkList l, int I, ElemType & e)
{
LNode * p = l-> next;
Int count = 1;
// Initialize and point p to the first node. count is the counter.
While (p & count <I)
{
P = p-> next;
Count ++;
}
// The p pointer moves to the node where the element to be obtained is located.
If (! P | count> I)
Return ERROR;
E = p-> data;
Return e;
}
/* Determine whether the linear table is empty */
Int isEmpty (LinkList & l)
{
If (l-> data = 0)
Return FALSE;
Else
Return TRUE;
}
/* Delete a position element in a linear table
I is from 1 **/
Int delete_LinkList (LinkList & l, int I)
{
Printf ("deleting element \ n at position % d of a linear table", I );
Int j = 0;
LinkList p = l;
// First locate the position of the element to be deleted
While (p-> next & I> = 1 & j <i-1)
{
P = p-> next;
J ++;
}
If (! P-> next | j> i-1)
Return ERROR; // The deletion location is incorrect.
LinkList q = p-> next;
P-> next = q-> next;
Free (q );
L-> data --;
Return OK;
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
LinkList l;
Int e;
Init_List (l );
Insert_List (l, 1, 3 );
Insert_List (l, 1, 4 );
Insert_List (l, 1, 5 );
Insert_List (l, 1, 6 );
Printf ("the first position element is % d \ n", getElem (l, 1, e ));
Printf ("number of elements in a linear table; % d \ n", l-> data );
Clear_List (l );
Destory_List (l );
_ Getch ();
Return 0;
}
/**
Author Kiritor
Chained Implementation of linear tables */
# Include "stdafx. h"
# Include <stdio. h>
# Include <stdlib. h>
# Include <conio. h>
# Include <malloc. h>
# Define OK 1
# Define OVERFLOW-1
# Define ERROR-1
# Define TRUE 1
# Define FALSE 0
# Define ElemType int
// Storage structure of a single-chain table of a linear table
Typedef struct LNode
{
ElemType data; // node data Information Field
Struct LNode * next; // pointer to the next node
} LNode, * LinkList;
// Initialize a single-chain table
Int init_List (LinkList & l)
{
Printf ("initialize linear table \ n ");
// Generate a header node and point L to the header Node
L = (LinkList) malloc (sizeof (LNode ));
If (! L)
Exit (OVERFLOW); // failed to allocate space
L-> next = NULL; // the pointer field of the header node is NULL, indicating no element.
L-> data = 0; // The data field of the header node is used to indicate the length of the linked list.
Return OK;
}
// Destroy the chained unidirectional linear table
Void destory_List (LinkList & l)
{
Printf ("Destroy linear table ");
LinkList q;
/* Release the memory space of each node cyclically */
While (l)
{
Q = l-> next;
Free (l );
L = q;
}
}
// Reset the linear table to an empty table instead of destroying it.
Int clear_List (LinkList & l)
{
Printf ("clear linear table, not destroy \ n ");
LinkList p, q;
P = l-> next; // p points to the first node
While (p)
{
Q = p-> next;
Free (p );
P = q;
}
// Then, set the direction of the header pointer field to null.
L-> next = NULL;
L-> data = 0;
Return OK;
}
// Insert a linear table (Leading node)
// Insert e before position I, and the I value starts from 1
Int insert_List (LinkList & l, int I, ElemType e)
{
Printf ("insert % d \ n", I, e in position % d );
// First look for the I-th Node
LinkList q = l, s;
Int j = 0; // counter used for Loop
While (q & j <i-1)
{
Q = q-> next;
J ++;
}
If (! Q | j> I-1)
{
Return ERROR;
}
S = (LinkList) malloc (sizeof (LNode); // generate a new node
S-> data = e;
S-> next = q-> next;
Q-> next = s;
L-> data ++; // linear table length plus 1
Return OK;
}
/* The element that obtains the I position of the linear table. If it exists, Return OK and put it in e.
Otherwise, an ERROR is returned. Note that l is the single-chain table of the leading node.
*/
Int getElem (LinkList l, int I, ElemType & e)
{
LNode * p = l-> next;
Int count = 1;
// Initialize and point p to the first node. count is the counter.
While (p & count <I)
{
P = p-> next;
Count ++;
}
// The p pointer moves to the node where the element to be obtained is located.
If (! P | count> I)
Return ERROR;
E = p-> data;
Return e;
}
/* Determine whether the linear table is empty */
Int isEmpty (LinkList & l)
{
If (l-> data = 0)
Return FALSE;
Else
Return TRUE;
}
/* Delete a position element in a linear table
I is from 1 **/
Int delete_LinkList (LinkList & l, int I)
{
Printf ("deleting element \ n at position % d of a linear table", I );
Int j = 0;
LinkList p = l;
// First locate the position of the element to be deleted
While (p-> next & I> = 1 & j <i-1)
{
P = p-> next;
J ++;
}
If (! P-> next | j> i-1)
Return ERROR; // The deletion location is incorrect.
LinkList q = p-> next;
P-> next = q-> next;
Free (q );
L-> data --;
Return OK;
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
LinkList l;
Int e;
Init_List (l );
Insert_List (l, 1, 3 );
Insert_List (l, 1, 4 );
Insert_List (l, 1, 5 );
Insert_List (l, 1, 6 );
Printf ("the first position element is % d \ n", getElem (l, 1, e ));
Printf ("number of elements in a linear table; % d \ n", l-> data );
Clear_List (l );
Destory_List (l );
_ Getch ();
Return 0;
}
Check the execution result of the program:
The most important operation is the insert and delete operations!
Insert operation process:
Delete operation process:
By reading the code, we can see the main differences between linear tables in chained mode and linear tables in sequential mode.
The physical address is not continuous, and the cost of inserting and deleting elements in the sequential mode is higher than that in the chained mode,
It is more convenient to obtain elements in sequence mode. Therefore, you need to select the linear table based on the actual situation!