Table data structure of a linear 2-

Linear table concept

Definition: a finite sequence having the same characteristic data elements.

The number of elements is called the length of the linear form, is represented by n (n ≧ 0), and when n is 0, represents a linear table is empty table


1. Logical Characteristics: linear structure

Definition of linear structure: to-one linear relationship between data elements.

① When there is a first element (header), there is a last element (footer) empty table, the first / last element is empty

② In addition to the first element, the other element has a unique predecessor
except for the last element, the other element has a unique successor


2. Storage Structure: Sequence / chain

Sequential storage structure is a linear random access table (of course also the order of access) memory structure
linked storage structure of the linear structure of a table is stored sequentially accessed.

1. The sequence table (stored in order):

(1) Definition of: a linear table all elements according to their logical order, are sequentially stored into a designated storage location contiguous memory starting

(2) characteristics:
① random access

② occupy contiguous storage space, storage space-time distribution

③ To move a plurality of insert elements (inserted after the table number of the i-th element of the mobile elements ⇒ ni)

④ storage density = 1


2. list (chain store):

(1) Definition: logical relationship between the junction element by means of a pointer indicating the storage address (additional pointer field) FIG.
Such as a single chain precursor node address information contained in the successor node

(2) Characteristics:
① not occupy contiguous storage space, the storage space allocated times

② does not support random access, only sequential access (to be read must traverse all the elements of an element preceding it to find and read)

② Since the need to leave space for the pointer field,
the storage space utilization is low, storage density <1

③ support dynamic allocation of memory space (now divided active)

④ insert operation, without moving element


3. The head pointer head:

① pointer field pointer is a pointer to an independent variable, not a single linked list of nodes

③ head node list is not necessary,
but the chain (acyclic chain) must have the head pointer,

the position of the first element of the sequence table can also be used to indicate the head pointer

Only the head can be circular list pointer / tail pointer to indicate (head pointer is not required)

③ head pointer has the effect of identifying the list, the entire list must access a pointer to start from scratch, (the name of a pointer variable) name list head pointer labeled

④head-> Next ⇔ (head *) .next
(pointer to the head node pointer domain members, i.e. head points to the next nodes ⇒head-> next points to the next node the next)

⑤ If the first node has a list, regardless of whether the list is empty, the head pointer is not null. The head pointer is an essential element of the list. (First statement, no wild pointer to the head node)

Sequence table pointer is empty sequence table is empty


4. A list of five forms

The list can not distinguish between the lead and the lead junction nodes, use the same, single chain Case


1. Single list

Each node in addition to data fields, further comprising a next pointer field to point to the subsequent node. (End) node table tail pointer field is NULL

Single chain (1) First Node

In the lead in a single linked list of nodes, the first node having ambiguity
with the first element to the node element represents a node
of a node represents a node (the first node / first linear table element node)

① the first node (the length of the list may be stored). Range No information / metadata pointer field points to the first node

② head pointer head store the memory location of the first node (always points to the head node), the head pointer is always not empty (when the declaration head pointer although not the first node, but it is not empty wild pointers)
of pointer , the storage position who, who points to

③ the head node successor node storing the first data element node starts

④head-> next = NULL, the list is empty


(2) does not lead to a single linked list node

First (element) node as the start node

head directly to the start node, when the head is NULL, the list is empty


2. doubly linked list

It adds a pointer field in the prior single linked list node points to the predecessor of the current node

Still do not distinguish between the lead and the lead junction node, and the same single chain

(Normal) just added a doubly linked list pointer field precursor, any cycle not
determined Table empty single chain identical with ordinary

3. Cycle singly linked list

The last field a single linked list pointer (the end of the table pointer field junction) points to the first node.

Here the first node refers to the first node / nodes first element
because the loop can still distinguish between a single linked list node and lead without head node

(2) determination table empty
without head node:
head = NULL

First Node:
head = head-> Next
First Node cycle (single / dual) list pointer is not null

4. The doubly-linked circular

(1) Definition:
The double-linked terminal node (node footers) a first pointer to the next node in the linked list
to the first node prior list pointer to the terminating node

Distinguishable lead and non-lead junction node


(2) determines the empty table
without head node:
head = NULL

First Node:
head-> Next == head and head-> prior = head
of any determination to a
lead circular doubly-linked list node pointer is not null

A circular doubly linked list of any node not null pointer precursor

5. Static list

By means of a one-dimensional array, a linked list requires the continuous space

Each pointer points to the location of the node component successor node in the array

"Pointer component": integer variable pointing to the next nodes in the array relative position

An array of first idle position (similar to the first node), node points to the first element of
S [0] .cur node is the first position



The linked list of nodes associated with distinguishing the pointer

1. Relationship: When we define a pointer pointing to a node A, since the node name is not, so we used a pointer to the name A as the name of the node

Meaning A: ① the node ② a pointer to the node

2. distinguished:
① A point refers here who ⇒A pointer
②free () release A⇒A herein refers to the node
since the pointer variable is itself required storage space allocated by the system, the user need not release. The user's own allocated memory space (memory space the malloc / node) needs its own release



Linear list structure is defined and the basic operation

I. structure is defined

1. order table

(1) typedef struct {} Sqlist ;
define a sequence table (structure) and type of change called Sqlist

(2) members of the element of the array (by a pointer to the array element of the array base address elem) int * elem;
an array length and space length allocation size listsize

Initializing an empty list 1-2 linear

Status Initial (SqList & L) // initialize an empty array reference
{
L.elem = (int ) the malloc (listsize the sizeof (int))
//L.elem assignment array pointer points to the allocated memory

if exit (overflow) (L.elem!); // memory allocation failure (L.elem is NULL, the exit process and return OVERFLOW)

L.length=0;
L.listsize=listsize;
return ok;
}


2. singly linked list

(1) typedef struct LNode {} LNode, * LinkList;
defines a single linked list node (structure) and type of change called LNode,

And renaming struct LNode * (node ​​pointer) type LinkList

Binding int * (int pointers) type appreciated

To be understood as a type int *

LinkList L; defines a variable node pointer type L

L is (points to) node type of pointer variables,
identifying entire sequence table


(2)
node comprises a data field (members) int data

And a pointer field (members) struct LNode * next
is defined by the structure itself, so that the structure itself is not province name
next pointer to the next node

3. doubly linked list

(. 1) {} typedef struct DLNode DLNode, * DuLinkList;
DLNode double linked list node type,
DuLinkList double linked list node pointer types

(2) a data field int data;
two pointer field
struct DLNode * prior; points to the predecessor node pointer
struct DLNode * next; successor node pointer pointing



Two operation sequence table

1. Press the element values ​​of the search algorithm

Order to find the i-th value in the sequence tables satisfies r compare () element

int LocateElem(SqList L,Elemtype e,Status(* compare)(Elemtype,Elemtype)){~}

(1) for a call-SqList L
does not change the sequence table in the memory of

(2) function pointer

Status (* compare) (int, int)
function pointer points to compare return (type) of Status, Parameter (type) of int, int function of

Assignment (the call assignment): LocateElem (L, E, CoMP)
CoMP is a function of their definition, the pointer is pointing to compare, compare and can be called by

Call: compare (a, b) / (* compare) (a, b)


(3) a first element of the array value is stored in elem [0] / * elem, the (first address array elem == & elem [0])

* Elem access array elements with elements ⇒ bit sequence (1-n)

// i order value of the element, as the initial value. 1
int. 1 = I;

// p base address assigned to the order table, the order table to traverse through ++ p
elemType L.elem = * p;

while (i <= L.length && !(*compare)(*p++, e)) ++i;
if (i <= L.length) return i;
else return 0;
}


3. Insert element

Status Insert (SqList & L, int i, int e) // e is inserted before the i-th position
{
IF (i <. 1 || i> + L.length. 1) return ERROR;
// be the first (1 ~ n before insertion +1) positions

IF (L.length> = L.listsize)
{
elemType * newbase;
newbase = (int *) realloc (L.elem, (+ L.listsize increament) * the sizeof (elemType));
(! newbase) IF Exit (the OVERFLOW );
// add a determination, to prevent allocation failure, the original array is further emptied

L.elem=newbase;
L.listsize+=increament;

}

* P int;
P = & L.elem [-I. 1];
// insert element position (address variable assignments)
for (int * Q = & L.elem [-L.length. 1]; Q> = P; Q-)
// q is the end of the table element position
* (q + 1) = * q;

    *p=e;
L.length++;

return ok;
}

Published 46 original articles · won praise 15 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_41850194/article/details/97374861