Order table C

C sequence table (Sequence List)

/*
 * sequence_list.c
 * Order form
 * sl = sequence list
 * Sequential storage means is a linear table in memory with the address of a successive memory elements in a linear order to store each data in the table
 * A linear form of this stored table is called the sequence table
 * */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAXSIZE 25

/*
 * Sequence table storage structure
 *     datatype data[MAXSIZE];  // 数组
 * Int last; the last element in the array index //
 *                              // length = last+1
 * */
typedef struct sl {
    int data[MAXSIZE];
    int last;
} sl;

sl *init_sl();
bool insert_sl(sl *list, int index, int value);
bool inserts_sl(sl *list, int values[], int len);
bool delete_sl(sl *list, int index);
int locate_sl(sl *list, int value);
void traverse_sl(sl *list);
sl *merge_sl(sl *la, sl *lb);
void sort_sl(sl *list);

int main(void)
{
    sl *list = init_sl();    

    insert_sl(list, 1, 1);
    insert_sl(list, 2, 2);
    insert_sl(list, 3, 3);
    insert_sl(list, 2, 1);
    traverse_sl(list);
    delete_sl(list, 4);
    traverse_sl(list);

    printf("1 is in [%d]\n", locate_sl(list, 1));
    printf("5 is in [%d]\n", locate_sl(list, 3));

    sl *adder = init_sl();
    int nums[3] = {7, 8, 9};
    inserts_sl(adder, nums, 3);
    traverse_sl(adder);

    traverse_sl(merge_sl(list, adder));
    sl * = sortsl merge_sl (adder, list);
    traverse_sl (sortsl);
    sort_sl (sortsl);
    traverse_sl (sortsl);


    return 0;
}

/*
 * Initialization sequence table
 * Returns a pointer to the sequence table
 * Last is set to -1, indicating empty
 * */
sl *init_sl() 
{
    sl *list = (sl *)malloc(sizeof(sl));
    list->last = -1;
    return list;
}

/*
 * Insert data value in index-1 position sequence table
 * It should be noted, index user input is a number from 1 to start
 * Sequentially table index number is zero
 * It is necessary to index-1
 * */
bool insert_sl(sl *list, int index, int value)
{
    / * Confirm whether the table is empty * / 
    IF (list-> Last the MAXSIZE == - . 1 ) {
        printf("The sequence list is full.\n");
        return false;
    }
    / * Insertion index check, list-> has to be inserted before the data Data [index] * / 
    IF (index < . 1 || index> list-> Last + 2 ) {
        printf("Wrong index.\n");
        return false;
    }
    /*
     * From the last start, all data in the order of 1 index-back after moving a
     * */
    for (int i = list->last; i >= index-1; i--)
        list->data[i+1] = list->data[i];
    list->data[index-1] = value;
    list->last++;
    return true;
}

/*
 * The list of values ​​to be inserted into the sequence table
 * */
bool inserts_sl(sl *list, int values[], int len)
{
    int i, j;

    for (i = 0, j = list->last+2; i < len; i++, j++)
    {
        if(!insert_sl(list, j, values[i]))
            return false;
    }
    
    return true;
}

/*
 * Data on indexi-1 position sequence table Delete
 * */
bool delete_sl(sl *list, int index)
{
    / * To delete the index only index order of the table last (included) before
     * To check whether the table is empty, and the index element exists
     * */
    if (index < 1 || index > list->last+1)
    {
        printf("[%d] doesn't exist.\n", index);
        return false;
    }
    /*
     * From index, the order of all data in index-1 are moving forward after a
     * */
    for (int i = index; i <= list->last; i++)
        list->data[i-1] = list->data[i];
    list->last--;
    return true;
}

/*
 * Find in accordance with the value
 * */
int locate_sl(sl *list, int value)
{
    / * Loop find whether there is value in the value sequence table, it is determined whether or not i is greater than the Last, if greater than the value described no value, present value and vice versa on the index i * / 
    int i = 0 ;
     the while (i <= list- > Last && list-> Data [I]! = value)
        i++;    
    if (i > list->last)
        return -1;
    else
        return i+1;
}

/*
 * Traversal order list
 * */
void traverse_sl(sl *list)
{
    / * If the list is empty * / 
    IF (Last list- ==> - . 1 ) {
        printf("[]\n");
        return;
    }
    printf("[");
    for (int i = 0; ; i++)
    {
        if (i > list->last)
        {
            printf("\b\b]\n");
            break;
        }
        printf("%d, ", list->data[i]);
    }
}

/*
 * Integration of the two tables order
 * */
sl *merge_sl(sl *la, sl *lb)
{
    / * Dynamically assigns a new sequence table, the data la and lb to be inserted therein in order * / 
    SL * = List (SL *) the malloc ( the sizeof (SL));
    list->last = -1;
    int i, j;

    for (i = 0, j = 1; i <= la->last; i++, j++)
    {
        if (!insert_sl(list, j, la->data[i]))
            break;
    }

    for (i = 0; i <= lb->last; i++, j++)
    {
        if (!insert_sl(list, j, lb->data[i]))
            break;
    }

    return list;
}

/*
 * Table sorting order
 * Bubble Sort (Bubble Sort)
 * */
void sort_sl(sl *list)
{
    int i, j;

    for (i = list->last; i >= 0; i--) 
    {
        for (j = 0; j < i; j++)
        {
            if (list->data[j] > list->data[j+1])
            {
                int tmp = list->data[j];
                list->data[j] = list->data[j+1];
                list->data[j+1] = tmp;
            }
        }
    }
}

Guess you like

Origin www.cnblogs.com/noonjuan/p/12075389.html