C语言实现顺序表

来源:互联网 发布:九章算法 大数据 编辑:程序博客网 时间:2024/06/05 06:54

顺序表:要求实现增删查改,打印,排序,查找,二分查找等功能
静态版本:

//SepList.h#define _CRT_SECURE_NO_WARNINGS 1#ifndef __SEQLIST_H__#define __SEQLIST_H__#include<stdio.h>#include<stdlib.h>#include<assert.h>#define MAX 100typedef int DataType;typedef struct SeqList{    DataType arr[MAX];    int count;}SeqList,*pSeqList;void InitSeqList(pSeqList p); //初始化void PushBack(pSeqList p, DataType d);//尾插void PushFront(pSeqList p, DataType d);//头插void PopBack(pSeqList p);//尾删void PopFront(pSeqList p);//头删int Find(pSeqList p,DataType d);//查找void Remove(pSeqList p, DataType d);//删除一个void RemoveAll(pSeqList p, DataType d);//删除多个void Sort(pSeqList p);//排序int BinSearch(pSeqList p, DataType d);//二分查找void Show(pSeqList p);//打印#endif
//SeqList.c#define _CRT_SECURE_NO_WARNINGS 1#include"SeqList.h"void InitSeqList(pSeqList p){    p->count = 0;    memset(p->arr, 0, MAX*sizeof(DataType));}void PushBack(pSeqList p, DataType d){    assert(p != NULL);    if (p->count == MAX)    {        printf("顺序表已满\n");        return;    }    p->arr[p->count] = d;    p->count++;}void PushFront(pSeqList p, DataType d){    assert(p != NULL);    if (p->count == MAX)    {        printf("顺序表已满\n");        return;    }    int i = p->count;    for (; i > 0; i--)    {        p->arr[i] = p->arr[i-1];    }    p->arr[0] = d;    p->count++;}void PopBack(pSeqList p){    assert(p != NULL);    if (p->count == 0)    {        printf("顺序表为空!!!\n");        return;    }    p->count--;}void PopFront(pSeqList p){    assert(p != NULL);    int i = 0;    if (p->count == 0)    {        printf("顺序表为空!!!\n");        return;    }    while (i < p->count-1)    {        p->arr[i] = p->arr[i + 1];        i++;    }    p->count--;}int Find(pSeqList p, DataType d){    assert(p != NULL);    int i = 0;    if (p->count == 0)    {        printf("顺序表没有元素\n");        return ;    }    for (i = 0; i < p->count; i++)    {        if (p->arr[i] == d)        {            return i+1;        }    }    return -1;}void Remove(pSeqList p, DataType d){    assert(p != NULL);    if (p->count == 0)    {        printf("数据为空无法删除\n");        return;    }    int i = 0;    int j = 0;    for (i = 0; i < p->count; i++)    {        if (d == p->arr[i])        {            for (j = i; j < p->count-1; j++)            {                p->arr[j] = p->arr[j + 1];            }            p->count--;            printf("删除成功\n");            return;        }    }}void RemoveAll(pSeqList p, DataType d){    assert(p != NULL);    if (p->count == 0)    {        printf("数据为空无法删除\n");        return;    }    int i = 0;    int j = 0;    for (i = 0; i < p->count; i++)    {        if (d == p->arr[i])        {            for (j = i; j < p->count - 1; j++)            {                p->arr[j] = p->arr[j + 1];            }            p->count--;            printf("删除成功\n");        }    }}void Sort(pSeqList p){    assert(p != NULL);    if (p->count == 0)    {        printf("顺序表没有元素无法排序\n");        return;    }    int i = 0;    int j = 0;    for (i = 0; i < p->count-1; i++)    {        for (j = 0; j < p->count - i-1; j++)        {            if (p->arr[j]>p->arr[j + 1])            {                DataType tmp = p->arr[j];                p->arr[j] = p->arr[j + 1];                p->arr[j + 1] = tmp;            }        }    }}int BinSearch(pSeqList p, DataType d){    assert(p);    int left = 0;    int right = p->count - 1;    int mid = 0;    while (left <= right)    {        mid = (left + right) / 2;        if (p->arr[mid] > d)        {            right = mid - 1;        }        else if (p->arr[mid] < d)        {            left = mid + 1;        }        else        {            return mid+1;        }    }    return -1;}void Show(pSeqList p){    assert(p != NULL);    int i = 0;    if (p->count == 0)    {        printf("顺序表没有元素\n");        return;    }    for (i = 0; i < p->count; i++)    {        printf("%d ", p->arr[i]);    }    printf("\n");}
//test.c#define _CRT_SECURE_NO_WARNINGS 1#include"SeqList.h"void menu(){    printf("****************************************\n");    printf("***            SeqList               ***\n");    printf("***  1.PushBack      2.PushFront     ***\n");    printf("***  3.PopBack       4.PopFront      ***\n");    printf("***  5.Remove        6.RemoveAll     ***\n");    printf("***  7.Sort          8.BinSearch     ***\n");    printf("***  9.show          10.Find         ***\n");    printf("***           0.exit                 ***\n");    printf("****************************************\n");}int main(){    int input = 0;    SeqList L;    InitSeqList(&L);    do    {        menu();        printf("请选择:");        scanf("%d", &input);        switch (input)        {        case 1:        {                  DataType ret = 0;                  printf("请输入数:");                  scanf("%d", &ret);                  PushBack(&L, ret);        }            break;        case 2:        {                  DataType ret = 0;                  printf("请输入数:");                  scanf("%d", &ret);                  PushFront(&L, ret);        }            break;        case 3:            PopBack(&L);            break;        case 4:            PopFront(&L);            break;        case 5:        {                  DataType ret = 0;                  printf("请输入要删除的数:");                  scanf("%d", &ret);                  Remove(&L, ret);        }            break;        case 6:        {                  DataType ret = 0;                  printf("请输入要删除的数:");                  scanf("%d", &ret);                  RemoveAll(&L, ret);        }            break;        case 7:            Sort(&L);            break ;        case 8:        {                  DataType ret = 0;                  DataType tmp = 0;                  printf("请输入要找的数:");                  scanf("%d", &ret);                  tmp = BinSearch(&L, ret);                  if (tmp == -1)                  {                      printf("没找到!!!\n");                  }                  else                  {                      printf("找到了这个数是第%d个数\n", tmp);                  }        }            break;        case 9:            Show(&L);            break;        case 10:        {                   DataType ret = 0;                   DataType tmp = 0;                   printf("请输入要找的数:");                   scanf("%d", &ret);                   tmp=Find(&L, ret);                   if (tmp == -1)                   {                       printf("没找到!!!\n");                   }                   else                   {                       printf("找到了这个数是第%d个数\n", tmp);                   }        }            break;        case 0:            printf("程序结束!!!\n");            break;        default :            printf("输入错误请重新输入<1~9>:");            break;        }    } while (input);    system("pause");    return 0;}

动态版本:

//SeqList.h#define _CRT_SECURE_NO_WARNINGS 1#ifndef __SEqList_H__#define __SEqList_H__#include<stdio.h>#include<stdlib.h>#include<assert.h>#define DEFAULT_SZ 1#define INC_SZ 2typedef int DataType;typedef struct SeqList{    DataType *pData;//存放元素地址    int count; //元素个数    int capacity;//容量}SeqList, *pSeqList;void InitSeqList(pSeqList p);void PushBack(pSeqList p, DataType d);void PushFront(pSeqList p, DataType d);void PopBack(pSeqList p);void PopFront(pSeqList p);int Find(pSeqList p, DataType d);void Remove(pSeqList p, DataType d);void RemoveAll(pSeqList p, DataType d);void sort(pSeqList p);int BinSearch(pSeqList p, DataType d);void show(pSeqList p);void DestorySeqlist(pSeqList p);//释放#endif
//SeqList.c#define _CRT_SECURE_NO_WARNINGS 1#include"SeqList.h"void InitSeqList(pSeqList p)//初始化{    p->pData = (DataType*)malloc(DEFAULT_SZ*(sizeof(DataType)));//开辟空间    if (p->pData == NULL)    {        perror("malloc");        return;    }    p->count = 0;    p->capacity = DEFAULT_SZ;    memset(p->pData, 0, DEFAULT_SZ*sizeof(DataType));}void CheckCapacity(pSeqList p){    assert(p != NULL);    if (p->capacity == p->count)    {        DataType *tmp = (DataType*)realloc(p->pData, (p->capacity + INC_SZ)*sizeof(DataType));        if (tmp != NULL)        {            p->pData = tmp;            p->capacity += INC_SZ;        }    }}void PushBack(pSeqList p, DataType d){    assert(p != NULL);    CheckCapacity(p);    p->pData[p->count] = d;    p->count++;}void PushFront(pSeqList p, DataType d){    assert(p != NULL);    int i = p->count;    CheckCapacity(p);    while (i)    {        p->pData[i] = p->pData[i - 1];        i--;    }    p->pData[0] = d;    p->count++;}void PopBack(pSeqList p){    assert(p != NULL);    if (p->count == 0)    {        printf("数据为空\n");        return;    }    p->count--;}void PopFront(pSeqList p){    assert(p != NULL);    if (p->count == 0)    {        printf("数据为空\n");        return;    }    int i = 0;    for (i = 0; i < p->count - 1; i++)        p->pData[i] = p->pData[i + 1];    p->count--;}int Find(pSeqList p, DataType d){    assert(p != NULL);    int i = 0;    for (i = 0; i < p->count; i++)    {        if (p->pData[i] == d)            return i + 1;    }    return -1;}void Remove(pSeqList p, DataType d){    assert(p != NULL);    if (p->count == 0)    {        printf("数据为空无法删除\n");        return;    }    int i = 0;    int j = 0;    for (i = 0; i < p->count; i++)    {        if (p->pData[i] == d)        {            for (j = i; j < p->count - 1; j++)            {                p->pData[j] = p->pData[j + 1];            }            p->count--;            return;        }    }}void sort(pSeqList p){    assert(p != NULL);    if (p->count == 0)    {        printf("没有数据无法排序\n");        return;    }    int i = 0;    int j = 0;    for (i = 0; i < p->count - 1; i++)    {        for (j = 0; j < p->count - 1 - i; j++)        {            if (p->pData[j]>p->pData[j + 1])            {                int tmp = p->pData[j];                p->pData[j] = p->pData[j + 1];                p->pData[j + 1] = tmp;            }        }    }}void RemoveAll(pSeqList p, DataType d){    assert(p != NULL);    if (p->count == 0)    {        printf("数据为空无法删除\n");        return;    }    int i = 0;    int j = 0;    for (i = 0; i < p->count; i++)    {        if (p->pData[i] == d)        {            for (j = i; j < p->count - 1; j++)            {                p->pData[j] = p->pData[j + 1];            }            p->count--;        }    }}int BinSearch(pSeqList p, DataType d){    assert(p != NULL);    if (p->count == 0)    {        printf("没有数据\n");        return ;    }    int left = 0;    int right = p->count - 1;    while (left <= right)    {        int mid = (left + right) / 2;        if (p->pData[mid] == d)        {            return mid + 1;        }        else if (p->pData[mid] > d)        {            right = mid - 1;        }        else        {            left = mid + 1;        }    }    return -1;}void show(pSeqList p){    assert(p != NULL);    int i = 0;    for (i = 0; i < p->count; i++)        printf("%d ", p->pData[i]);    printf("\n");}void DestorySeqlist(pSeqList p){    assert(p != NULL);    free(p->pData);}
//test.c#define _CRT_SECURE_NO_WARNINGS 1#include"SeqList.h"void menu(){    printf("***************************************\n");    printf("****   1.PushBack      2.PushFront ****\n");    printf("****   3.PopBack       4.PopFront  ****\n");    printf("****   5.Find          6.Remove    ****\n");    printf("****   7.RemoveAll     8.Sort      ****\n");    printf("****          9.BinarySearch       ****\n");    printf("****   0.exit          10.show     ****\n");    printf("***************************************\n");}int main(){    int input = 0;    SeqList L;    InitSeqList(&L);    do{        menu();        printf("请选择:\n");        scanf("%d", &input);        switch (input)        {        case 1:        {                  DataType ret = 0;                  printf("请输入要插入的数:\n");                  scanf("%d", &ret);                  PushBack(&L, ret);        }            break;        case 2:        {                  DataType ret = 0;                  printf("请输入要插入的数:\n");                  scanf("%d", &ret);                  PushFront(&L, ret);        }            break;        case 3:            PopBack(&L);            break;        case 4:            PopFront(&L);            break;        case 5:        {                  DataType ret = 0;                  printf("请输入要查找的数:\n");                  scanf("%d", &ret);                  DataType wet = Find(&L, ret);                  if (-1 == wet)                      printf("没找到\n");                  else                      printf("找到了这个数是第%d个数\n", wet);        }            break;        case 6:        {                  DataType ret = 0;                  printf("请输入要删除的数:\n");                  scanf("%d", &ret);                  Remove(&L, ret);        }            break;        case 7:        {                  DataType ret = 0;                  printf("请输入要删除的数:\n");                  scanf("%d", &ret);                  RemoveAll(&L, ret);        }            break;        case 8:            sort(&L);            break;        case 9:        {                  DataType ret = 0;                  printf("请输入要查找的数:\n");                  scanf("%d", &ret);                  DataType wet = BinSearch(&L, ret);                  if (-1 == wet)                      printf("没找到\n");                  else                      printf("找到了这个数是第%d个数\n", wet);        }            break;        case 10:            show(&L);            break;        case 0:            DestorySeqlist(&L);            break;        default:            printf("输入有误请重新选择:\n");            break;        }    } while (input);    return 0;}
0 0
原创粉丝点击