1. 程式人生 > >簡單順序表的插入,刪除,指定位置,指定元素的插入刪除操作

簡單順序表的插入,刪除,指定位置,指定元素的插入刪除操作

標頭檔案 SeqList.h

#ifndef __SEQLIST_H__

#define __SEQLIST_H__
#include<stdio.h>
#include<string.h>
#include<assert.h>

#define MAX_SIZE 10
typedef int DataType;
typedef unsigned int size_t;

typedef struct SeqList
{
    DataType array[MAX_SIZE];
    size_t size;
}SeqList, *PSeqList;




// 初始化順序表
void InitSeqList(PSeqList seqList); // 順序表尾插 // 設計函式原型 // 引數檢測 // 邊界條件考慮 // 邏輯操作 // 尾插 void PushBack(PSeqList pSeqList, DataType data); // 尾刪 void PopBack(PSeqList pSeqList); // 列印順序表 void PrintSeqList(PSeqList pSeqList); // 頭插 void PushFront(PSeqList pSeqList, DataType data); // 頭刪 void PopFront(PSeqList pSeqList); // 任意位置插入
void Insert(PSeqList pSeqList, size_t pos, DataType data); // 在順序表中查詢元素data int Find(PSeqList pSeqList, DataType data); // 刪除順序表中pos位置上的元素 void Erase(PSeqList pSeqList, size_t pos); //移除順序表中的元素data void Remove(PSeqList pSeqList, DataType data); // 移除順序表中所有元素data void RemoveAll(PSeqList pSeqList, DataType data); #endif

實現函式 SeqList.c

#include "SeqList.h"


void InitSeqList(PSeqList seqList)// 初始化順序表
{
    memset(seqList->array, 0, MAX_SIZE*sizeof(DataType));
    seqList->size = 0;
}

// 列印順序表
void PrintSeqList(PSeqList pSeqList)
{
    size_t i = 0;
    if (pSeqList->size == 0)
    {
        printf("線性表為空,列印結束\n");
        return;
    }
    for (i = 0; i < pSeqList->size; i++)
    {
        printf("%d  ", pSeqList->array[i]);
    }
    printf("\n");
}

void PushBack(PSeqList pSeqList, DataType data)//從尾部插入
{
    assert(pSeqList);//判斷指標是否為空
    if ((pSeqList->size) >= MAX_SIZE)
    {
        printf("線性表已滿,無法繼續插入\n");
        return;
    }
    pSeqList->array[pSeqList->size] = data;
    pSeqList->size++;

}

void PopBack(PSeqList pSeqList)//從尾部刪除
{
    assert(pSeqList);
    if (pSeqList->size == 0)
    {
        printf("線性表為空\n");
        return;
    }
    pSeqList->size--;
    printf("刪除成功\n");
}

void PushFront(PSeqList pSeqList, DataType data)//從首部插入
{
    size_t i = pSeqList->size;
    assert(pSeqList);//判斷指標是否為空
    if ((pSeqList->size) >= MAX_SIZE)
    {
        printf("線性表已滿,無法繼續插入\n");
        return;
    }
    while (i)
    {
        pSeqList->array[ i ] = pSeqList->array[i-1];
        i--;
    }
    pSeqList->array[0] = data;
    printf("插入完成\n");
    pSeqList->size++;
}

void PopFront(PSeqList pSeqList)//刪除最前面一個
{
    size_t i = 0;
    assert(pSeqList);//判斷指標是否為空
    if (pSeqList->size == 0)
    {
        printf("線性表為空\n");
        return;
    }
    for (i = 0; i < pSeqList->size-1; i++)
    {
        pSeqList->array[i] = pSeqList->array[i + 1];
    }
    pSeqList->size--;
    printf("刪除完成\n");
}


int Find(PSeqList pSeqList, DataType data)//在順序表內查詢某元素
{
    size_t i = 0;
    assert(pSeqList);//判斷指標是否為空
    if (pSeqList->size == 0)
    {
        printf("線性表為空\n");
        return -1;
    }
    for (i = 0; i < pSeqList->size - 1; i++)
    {
        if (pSeqList->array[i] == data) 
        {
            return i;
        }
    }
    return -1;
}

void Insert(PSeqList pSeqList, size_t pos, DataType data)//在任意位置插入元素
{
    size_t i = pSeqList->size;
    assert(pSeqList);//判斷指標是否為空
    if ((pSeqList->size) >= MAX_SIZE)
    {
        printf("線性表已滿,無法繼續插入\n");
        return;
    }
    if (pos > pSeqList->size)
    {
        printf("插入位置不被允許\n");
        return;
    }
    for (i = pSeqList->size; i >pos; i--)
    {
        pSeqList->array[i] = pSeqList->array[i - 1];

    }
    pSeqList->array[pos] = data;
    pSeqList->size++;
    printf("新增完成\n");
}

// 刪除順序表中pos位置上的元素
void Erase(PSeqList pSeqList, size_t pos)
{
    size_t i = pos;
    assert(pSeqList);//判斷指標是否為空
    if (pSeqList->size == 0)
    {
        printf("線性表為空\n");
        return;
    }
    if (pos > pSeqList->size)
    {
        printf("刪除位置有誤\n");
        return;
    }

    for (i = pos; i < pSeqList->size-1; i++)
    {
        pSeqList->array[i] = pSeqList->array[i + 1];
    }
    pSeqList->size--;
    printf("刪除完成\n");
}

void RemoveAll(PSeqList pSeqList, DataType data)
{
    size_t i = 0;
    assert(pSeqList);//判斷指標是否為空
    if (pSeqList->size == 0)
    {
        printf("線性表為空\n");
        return;
    }
    for (i = 0; i < pSeqList->size; i++)
    {
        if (pSeqList->array[i] == data)
        {
            size_t j = i;
            while (j< pSeqList->size-1)
            {
                pSeqList->array[j] = pSeqList->array[j + 1];
                j++;
            }
            pSeqList->size--;
            printf("已刪除一個元素\n");
            i--;
        }
    }
    printf("刪除完成\n");
}




void Remove(PSeqList pSeqList, DataType data)
{
    int i = 0;
    assert(pSeqList);//判斷指標是否為空
    i = Find(pSeqList, data);
    if (i >= 0)
    {
        while (i <(int)pSeqList->size - 1)
        {
            pSeqList->array[i] = pSeqList->array[i + 1];
            i++;
        }
        pSeqList->size--;
        printf("刪除完成\n");
        return;
    }
    else
    {
        printf("沒有找到該元素");
        return;
    }
}

測試程式 test.c (可分三次測試)

#include "SeqList.h"


static void test1(PSeqList seqList)
{

    InitSeqList(seqList);
    PushBack(seqList, 1);
    PushBack(seqList, 2);
    PushBack(seqList, 3);
    PushBack(seqList, 4);
    PrintSeqList(seqList);
    PopBack(seqList);
    PrintSeqList(seqList);
}

static void test2(PSeqList seqList)
//測試PushFront ,PrintSeqList,PopFront等函式
{
    int ret = 0;
    InitSeqList(seqList);
    PushFront(seqList, 1);
    PushFront(seqList, 2);
    PushFront(seqList, 3);
    PushFront(seqList, 4);
    PrintSeqList(seqList);
    PopFront(seqList); 
    PopFront(seqList);
    PopFront(seqList);
    PopFront(seqList); 
    PopFront(seqList);
    PrintSeqList(seqList);
}

static void test3(PSeqList seqList)//測試Insert,Erase,Remove,RemoveALL函式
{
    int ret = 0;
    InitSeqList(seqList);
    PushFront(seqList, 1);//在首部加入 1
    PushFront(seqList, 2);
    PushFront(seqList, 3);
    PushFront(seqList, 4);
    Insert(seqList, 3, 5);//在下標3的位置插入 5
    PrintSeqList(seqList);
    Erase(seqList, 3);//刪除下標3位置上的元素
    PrintSeqList(seqList);
    Insert(seqList, 3, 2);
    Insert(seqList, 3, 2);
    PrintSeqList(seqList);
    Remove(seqList, 2);//刪除第一個 2
    PrintSeqList(seqList);
    RemoveAll(seqList, 2);//刪除所有的 2
    PrintSeqList(seqList);
}
static void test4(PSeqList seqList)//測試Find函式
{
    int ret = 0;
    InitSeqList(seqList);
    PushFront(seqList, 1);
    PushFront(seqList, 2);
    PushFront(seqList, 3);
    PushFront(seqList, 4);
    ret = Find(seqList, 2);
    printf("%d", ret);
}

int main()
{
    SeqList seqList;
    /*test1(&seqList);*/
    /*test2(&seqList);*/
    test3(&seqList);
    /*test4(&seqList);*/

    system("pause");
    return 0;
}