线性表

//sqlist.h
//sqlist/h
#ifndef _SQLIST_H_
#define _SQLIST_H_

typedef int data_t;
#define N 128

typedef struct{
    data_t data[N];
    int last;
}sqlist, *sqlink;

sqlink list_create();
int list_destroy(sqlink L);
int list_clear(sqlink L);
int list_empty(sqlink L);
int list_length(sqlink L);
int list_search(sqlink L, data_t value);
int list_insert(sqlink L, data_t value, int pos);
void list_show(sqlink L);
int list_delete(sqlink L, int pos);
int list_merge(sqlink L1, sqlink L2);
int list_delete_repeat(sqlink L);

#endif
//sqlist.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sqlist.h"

sqlink list_create()
{
    sqlink L = (sqlink)malloc(sizeof(sqlist));
    if(L == NULL){
        printf("malloc failed\n");
        return NULL;
    }
    
    memset(L, 0, sizeof(sqlist));
    L->last = -1;
    
    return L;
}

int list_destroy(sqlink L)
{
    if(L == NULL){
        printf("this is NULL list,destroy failed\n");
        return -1;
    }
    
    free(L);
    L = NULL;
    
    return 0;
}

int list_clear(sqlink L)
{
    if(L == NULL){
        printf("this is NULL list,clear failed\n");
        return -1;
    }
    
    memset(L, 0, sizeof(sqlist));
    L->last = -1;
    
    return 0;
}

int list_empty(sqlink L)
{
    if(L == NULL)
        return -1;
    else    
        return 0;
}

int list_length(sqlink L)
{
    if(L == NULL){
        printf("this is NULL list,length failed\n");
        return -1;
    }
    
    return L->last+1;
}

int list_search(sqlink L, data_t value)
{
    int i;
    for(i = 0; i <= L->last; i++){
        if(L->data[i] == value){
            return i;
        }
    }
    return -1;
}

int list_insert(sqlink L, data_t value, int pos)
{
    int i = 0;
    if(L->last == N-1){
        printf("list is full,not insert!\n");
        return -1;
    }
    
    if(pos < 0 || pos >= N-1){
        printf("position not correct,not insert!\n");
        return -1;
    }
    
    //从最后一个开始移,移到想要的位置pos
    if(pos >= 0 && pos <= L->last){
        for(i = L->last; i >= pos; i--){
            L->data[i+1] = L->data[i];
        }
        L->data[pos] = value;    
        L->last++;
    }else if(pos > L->last && pos < N-1){
        L->data[pos] = value;
        L->last = pos;
    }
    
    return 0;
}

void list_show(sqlink L)
{
    if(L == NULL){
        printf("this is NULL list,show failed\n");
    }
    int i;
    for(i = 0; i <= L->last; i++){
        printf("%d\t",L->data[i]);
    }
    printf("\n");
    return ;
}

int list_delete(sqlink L, int pos)
{
    int i;
    if(L->last == -1){
        printf("list is empty,not delete\n");
        return -1;
    }
    if(pos < 0 || pos > L->last){
        printf("pos is invalue\n");
        return -1;
    }
    if(pos == L->last){
        L->last--;
    }else if(pos < L->last){
        for(i = pos; i < L->last; i++){
            L->data[i] = L->data[i+1];    
        }
        L->last--;
    }
    return 0;
}

int list_merge(sqlink L1, sqlink L2){
    int i = 0;
    int ret;
    while(i <= L2->last){
        if(list_search(L1, L2->data[i]) == -1){
            list_insert(L1, L2->data[i], L1->last+1);
        }
        i++;
    }
    return 0;
}

int list_delete_repeat(sqlink L)
{
    int i = 0;
    int j = 0;
    while(i < L->last){
        j = i + 1;
         while(j <= L->last){
            if(L->data[i] == L->data[j]){
                list_delete(L, j);
            }else
            {
                j++;
            } 
        } 
        i++;
    }
    
    return 0;
}
//sqlist_test.c
#include <stdio.h>
#include "sqlist.h"

void test_insert()
{
    sqlink L = NULL;
    L = list_create();
    
    list_insert(L, 10, 10);    
    list_show(L);
    
    list_insert(L, 100, 5);    
    list_show(L);        
}

void test_delete()
{
    sqlink L = NULL;
    L = list_create();
    
    list_insert(L, 20, 0);    
    list_insert(L, 30, 1);    
    list_insert(L, 40, 5);
    list_insert(L, 90, 9);    
    list_insert(L, 100, 10);
    list_show(L);
    
    list_delete(L, 11);
    list_show(L);
}
void test_merge()
{
    sqlink L1 = NULL;
    L1 = list_create();
    
    list_insert(L1, 20, 0);    
    list_insert(L1, 30, 1);    
    list_insert(L1, 40, 5);
    list_insert(L1, 90, 9);    
    list_insert(L1, 100, 10);
    list_show(L1);
    
    sqlink L2 = NULL;
    L2 = list_create();
    
    list_insert(L2, 32, 0);    
    list_insert(L2, 30, 1);    
    list_insert(L2, 45, 5);
    list_insert(L2, 12, 9);    
    list_insert(L2, 100, 10);
    list_show(L2);
    
    list_merge(L1,L2);
    list_show(L1);
    list_show(L2);
}

void test_delete_repeat()
{
    sqlink L = NULL;
    L = list_create();
    
    list_insert(L, 30, 2);    
    list_insert(L, 30, 3); 
    list_insert(L, 30, 4);
    list_insert(L, 30, 5);    
    list_insert(L, 30, 6);
    list_insert(L, 30, 7);
    list_insert(L, 30, 9);    
    list_insert(L, 30, 10);
    list_show(L);
    printf("=======================\n");
    list_delete_repeat(L);
    list_show(L);
}
int main()
{
    //test_insert();
    
    //test_delete();

    //test_merge();
    
    test_delete_repeat();
    

    return 0;
}
posted @ 2023-04-18 23:16  踏浪而来的人  阅读(15)  评论(0编辑  收藏  举报