1. Linear table
Linear table (Linear list): A linear structure that forms an ordered sequence of data elements of the same type
? The number of elements in a table is called the length of a linear table
? When a linear table has no elements, it is called an empty table
? Table start position is called Table header, table end position is called footer
1.1. Sequential storage implementation of linear tables
typedef struct
{
ElementType Data[maxsize];
int last;
} List;
List L, *ptrl;
1. Initialize (Create empty order table)
List *makeempty ()
{List *ptrl;
Ptrl = (list *) malloc (sizeof (list));
Ptrl->last =-1;
return Ptrl;
}
2. Find
int Find (ElementType X, List *ptrl)
{int i = 0;
while (i <= ptrl->last && ptrl->data[i]!= X)
i++;
if (i > Ptrl->last) return-1; /* If not found, return-1 */
else return i; /* Returns the storage location when found */
}
3. Insert Operation implementation
void Insert (ElementType X, int i, List *ptrl)
{int J;
if (ptrl->last = = MAXSIZE-1) {/* table space is full and cannot be inserted */
printf ("Table full");
Return
}
if (I < 1 | | i > PTRL->LAST+2) {/* Check the validity of the insertion position */
printf ("Location is not legal");
Return
}
for (j = ptrl->last; J >= i-1; j--)
PTRL->DATA[J+1] = ptrl->data[j]; /* Move ai~ an reverse backwards */
Ptrl->data[i-1] = X; /* NEW Element Insert */
ptrl->last++; /*last still points to the last element */
Return
}
4. Delete operation implementation
void Delete (int i, List *ptrl)
{int J;
if (I < 1 | | i > PTRL->LAST+1) {/* Check empty table and delete location legality */
printf ("There is no%d element", i);
return;
}
for (j = i; J <= ptrl->last; J + +)
PTRL->DATA[J-1] = ptrl->data[j]; /* Move ai+1~ an sequence forward */
ptrl->last--; /*last still points to the last element */
Return
}
1.2. Chain-store implementation of linear tables
typedef struct node{
ElementType Data;
struct Node *next;
} List;
List L, *ptrl;
1. Length of the table
int Length (List *ptrl)
{List *p = Ptrl;/* p points to the first node of the table */
int j = 0;
while (p) {
p = p->next;
j + +; /* Current P points to the J node */
}
Return J;
}
2. Find
(1) Search by ordinal: findkth
List *findkth (int K, list *ptrl)
{List *p = Ptrl;
int i = 1;
while (P!=null && i < K) {
p = p->next;
i++;
}
if (i = = K) return p;
/* Find the first k, return the pointer */
else return NULL;
/* Otherwise return empty */
}
(2) Search by value: Find
List *find (ElementType X, List
*ptrl)
{
List *p = Ptrl;
while (p!=null && p->data! = X)
p = p->next;
return p;
}
3. Insert Operation implementation
List *insert (ElementType X, int i, list *ptrl)
{List *p, *s;
if (i = = 1) {/* new node inserted in header */
s = (list *) malloc (sizeof (list)); /* Apply, fill the knot */
S->data = X;
S->next = Ptrl;
return s; /* Return new header pointer */
}
p = findkth (i-1, Ptrl); /* Find the first i-1 node */
if (p = = NULL) {/* I-1 does not exist and cannot be inserted */
printf ("Parameter I Wrong");
return NULL;
}else {
s = (list *) malloc (sizeof (list)); /* Apply, fill the knot */
S->data = X;
S->next = p->next; /* The new node is inserted at the back of the first i-1 node */
P->next = s;
return Ptrl;
}
}
4. Delete
List *delete (int i, list *ptrl)
{List *p, *s;
if (i = = 1) {/* To delete the first node of a table */
s = Ptrl; /*s points to the 1th node */
if (ptrl!=null) Ptrl = ptrl->next; /* Remove from linked list */
else return NULL;
Free (s); /* Release the deleted node */
return Ptrl;
}
p = findkth (i-1, Ptrl); /* Find the first i-1 node */
if (p = = NULL) {
printf ("%d nodes not present", i-1); return NULL;
} else if (P->next = = NULL) {
printf ("%d nodes do not exist", I); return NULL;
} else {
s = p->next; /*s points to the I node */
P->next = s->next; /* Remove from linked list */
Free (s); /* Release the deleted node */
return Ptrl;
}
}
2. Stacks 3. Queues
1-Linear structure