#include <stdio.h>
#include <stdlib.h>
typedef int ELEMTYPE;
/************************************************************************/
/* Here are 16 algorithms for linear table sequential storage operations.
/************************************************************************/
struct list{
Elemtype *list;
int size;
int maxSize;
};
void Againmalloc (struct List *l)
{
/* Space expands to twice times the original, and is pointed by the P pointer, the original content is automatically copied to the storage point P
Elemtype *p = ReAlloc (l->list, 2 * l->maxsize * sizeof (elemtype));
if (!p) {*/* allocation fails to exit the run * *
printf ("Storage space allocation failed!") ");
Exit (1);
}
L->list = p; /* Make list point to new linear table space * *
L->maxsize = 2 * l->maxsize; /* Change the size of the linear table space to the new length * *
}
/* 1 Initialize the linear table L, that is, the dynamic storage space allocation and set L as an empty table * *
void Initlist (struct List *l, int ms)
{
/* Check whether MS is valid, if invalid then quit running.
if (MS <= 0) {
printf ("MaxSize illegal!") ");
Exit (1); /* Execute this function to abort the program, this function is defined in stdlib.h.
}
L->maxsize = ms; /* Set linear table space size to MS * *
l->size = 0;
L->list = malloc (MS * sizeof (ELEMTYPE));
if (! L->list) {
printf ("Space allocation failed!") ");
Exit (1);
}
Return
}
* 2. Clears all elements in the linear table L, frees up storage space and makes it an empty table * *
void Clearlist (struct List *l)
{
if (l->list!= NULL) {
Free (l->list);
l->list = 0;
l->size = l->maxsize = 0;
}
Return
}
* 3. Return linear table L current length, if L is empty then return 0 * *
int sizelist (struct List *l)
{
Return l->size;
}
* 4. Determine if the linear table L is null, if empty then return 1, otherwise return 0 * * *
int emptylist (struct List *l)
{
if (L->size ==0) {
return 1;
}
else{
return 0;
}
}
/* 5. Returns the value of the POS element in the Linear table L, if the POS is out of range, stop the program running * *
Elemtype Getelem (struct List *l, int pos)
{
if (pos < 1 | | | pos > l->size) {/* If the POS is out of bounds, quit running. *
printf ("The element ordinal is out of bounds!") ");
Exit (1);
}
Return l->list[pos-1]; /* Returns the value of the element in the linear table with the ordinal value of POS.
}
/* 6. Sequential scan (that is, traversal) output of each element in the linear table l * *
void Traverselist (struct List *l)
{
int i;
for (i = 0; i < l->size; i++) {
printf ("%d", L->list[i]);
}
printf ("");
Return
}
* 7. Look for elements equal to x in the linear table L and return the position if the search succeeds, otherwise return-1 * *
int findlist (struct List *l, Elemtype x)
{
int i;
for (i = 0; i < l->size; i++) {
if (l->list[i] = = x) {
return i;
}
}
return-1;
}
* 8. Change the value of the POS element in the Linear table L to X, or return 0 if the modification succeeds to return 1.
int updateposlist (struct List *l, int pos, elemtype x)
{
if (pos < 1 | | | pos > l->size) {/* If POS crosses over, modify failure/
return 0;
}
L->LIST[POS-1] = x;
return 1;
}
/* 9. Insert element x to the table header of the linear table L/*
void Inserfirstlist (struct List *l, Elemtype x)
{
int i;
if (l->size = = l->maxsize) {
Againmalloc (L);
}
for (i = l->size-1; i >= 0; i--) {
L->list[i + 1] = L->list[i];
}
L->list[0] = x;
L->size + +;
Return
}
/* 10. Insert element x to the footer of the linear table L
void Insertlastlist (struct List *l, Elemtype x)
{
if (l->size = = L->maxsize) {/* Reassign larger storage space * *
Againmalloc (L);
}
L->list[l->size] = x; /* insert X to the end of the table * *
l->size++; /* Linear table length increased by 1 * *
Return
}
* 11. Insert element x to the position of POS element in linear table L, if insert succeeds to return 1, otherwise return 0 */
int insertposlist (struct List *l, int pos, elemtype x)
{
int i;
if (pos < 1 | | | pos > l->size + 1) {/* If POS crosses the line, insert failure. *
return 0;
}
if (l->size = = l->maxsize) {/* Reassign larger storage space * * *
Againmalloc (L);
}
for (i = l->size-1; i >= pos-1; i--) {
L->list[i + 1] = l->list[i];
}
L->LIST[POS-1] = x;
l->size++;
return 1;
}
/* 12. Insert element x into ordered linear table l so that it is still ordered after insertion.
void Insertorderlist (struct List *l, Elemtype x)
{
int I, J;
/* If the array space is used up, redistribute more storage space.
if (l->size = = l->maxsize) {
Againmalloc (L);
}
/* order to find the insertion position of x * *
for (i = 0; i < l->size; i++) {
if (x < l->list[i]) {
Break
}
}
/* From the end of the table to the subscript I element and then move one position, the position of I empty out * *
for (j = l->size-1; J >= i; j--)
L->LIST[J+1] = l->list[j];
/* Assign the x value to the element labeled I/*
L->list[i] = x;
/* Linear table length increased by 1 * *
l->size++;
Return
}
* 13. Remove the header element from the Linear table L and return it, stop the program if the deletion fails.
Elemtype deletefirstlist (struct List *l)
{
Elemtype temp;
int i;
if (L->size = = 0) {
printf ("The linear table is empty and cannot be deleted!") ");
Exit (1);
}
temp = l->list[0];
for (i = 1; i < l->size; i++)
L->LIST[I-1] = l->list[i];
l->size--;
return temp;
}
* 14. Deletes the footer element from the linear table L and returns it, stopping the program if the deletion fails.
Elemtype deletelastlist (struct List *l)
{
if (L->size = = 0) {
printf ("The linear table is empty and cannot be deleted!") ");
Exit (1);
}
l->size--;
return L->list[l->size]; /* Returns the value of the original footer element * *
}
* 15. Delete the first POS element from the Linear table L and return it, stop the program if the deletion fails.
Elemtype deleteposlist (struct List *l, int pos)
{
Elemtype temp;
int i;
if (pos < 1 | | | pos > L->size) {* * pos crossed out Delete failed * *
printf ("The POS value is out of bounds and cannot be deleted!") ");
Exit (1);
}
temp = l->list[pos-1];
for (i = pos; i < l->size; i++)
L->LIST[I-1] = l->list[i];
l->size--;
return temp;
}
/* 16. Delete the first element of x from the linear table L, and return 1 if successful, failure returns 0 * *
int deletevaluelist (struct List *l, Elemtype x)
{
int I, J;
/* To find the first element of x in order from the linear table.
for (i = 0; i < l->size; i++) {
if (l->list[i] = = x) {
Break
}
}
/* If the lookup fails, indicating that there is no element with a value of X, return 0 */
if (i = = l->size) {
return 0;
}
/* Delete element with value x l->list[i] * *
for (j = i + 1; j < l->size; J + +) {
L->LIST[J-1] = l->list[j];
}
l->size--;
return 1;
}
/************************************************************************/
void Main ()
{
int a[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int i;
struct List L;
Initlist (&l, 5);
for (i = 0; i < i++) {
Insertlastlist (&l, a[i]);
}
Insertposlist (&l, 11, 48); Insertposlist (&l, 1, 64);
printf ("%d", Getelem (&l, 1));
Traverselist (&L);
printf ("%d", Findlist (&l, 10));
Updateposlist (&l, 3, 20);
printf ("%d", Getelem (&l, 3));
Traverselist (&L);
Deletefirstlist (&L); Deletefirstlist (&L);
Deletelastlist (&L); Deletelastlist (&L);
Deleteposlist (&l, 5); ;d eleteposlist (&l, 7);
printf ("%d", Sizelist (&l));
printf ("%d", Emptylist (&l));
Traverselist (&L);
Clearlist (&L);
return 0;
}