赞
踩
1.SeqList.h 头文件
- #pragma once
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- //定义动态顺序表
- typedef int SLDatatype;
-
- typedef struct SeqLlist {
- SLDatatype* arr;
- int capacity;
- int size;
- }SL;
-
- //typedef struct SeqList Sl;
-
- //初始化
- void SLInit(SL* ps);
-
- //销毁
- void SLDestory(SL* ps);
-
- //尾插入数据
- void SLPushBack(SL* ps,SLDatatype x);
-
- //打印顺序表
- void SLPrint(SL* ps);
-
- //头插
- void SLPushFront(SL* ps,SLDatatype x);
-
- //删除
- void SLPopBack(SL* ps);
- void SLPopFront(SL* ps);
-
- //在指定位置之前插入数据
- void SLInsert(SL* ps, SLDatatype x, int pos);
- //删除指定位置的数据
- void SLErase(SL* ps, int pos);
-
- //查找数据
- int SLFind(SL* ps, SLDatatype x);
-

2.SeqList.c文件
- #include "Seqlist.h"
-
- //初始化
- //错误1:使用了未初始化的局部变量“s”
- //void SLInit(SL s)--值传递
- void SLInit(SL* ps)
- {
- ps->arr = NULL;
- ps->capacity = ps->size = 0;
- }
-
- //判断空间是否充足
- void SLCheckCapacity(SL* ps)
- {
- if (ps->size == ps->capacity) {
- //增容0*2=0
- //若capacity为0,给个默认值,否则*2
- int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
- SLDatatype* tmp = (SLDatatype*)realloc(ps->arr, newCapacity * sizeof(SLDatatype));
- if (tmp == NULL)
- {
- perror("realloc faile!");
- exit(1);
- }
- ps->arr = tmp;
- ps->capacity = newCapacity;
- }
- }
-
- //尾插
- void SLPushBack(SL* ps,SLDatatype x) {
-
- //温柔的解决方式
- //if (ps == NULL)
- //{
- // return;
- //}
- //粗暴的解决方式
- assert(ps);
-
- SLCheckCapacity(ps);
- ps->arr[ps->size] = x;
- ps->size++;
- }
- //打印操作
- void SLPrint(SL* ps)
- {
- for (int i = 0; i < ps->size; i++)
- {
- printf("%d ", ps->arr[i]);
- }
- printf("\n");
- }
-
- //头插
- void SLPushFront(SL* ps,SLDatatype x) {
- assert(ps);
- //判断空间是否足够
- SLCheckCapacity(ps);
-
- //数据整体后移动1位
- for (int i = ps->size; i > 0; i--)
- {
- ps->arr[i] = ps->arr[i - 1];
- }
- //下标为0的位置空出来
- ps->arr[0] = x;
- ps->size++;
- }
-
- //销毁
- void SLDestory(SL* ps)
- {
- if (ps->arr != NULL)
- {
- free(ps->arr);
- }
- ps->arr = NULL;
- ps->capacity = ps->size = 0;
- }
- //删除
- void SLPopBack(SL* ps) {
- assert(ps);
- assert(ps->size);
- ps->arr[ps->size - 1] = 0;
- ps->size--;
-
- }
-
- void SLPopFront(SL* ps) {
-
- assert(ps);
- assert(ps->size);
-
- //数据整体向前挪动一个位置
- for (int i = 0; i < ps->size - 1; i++)
- {
- ps->arr[i] = ps->arr[i + 1];
- }
- ps->size--;
- }
-
- //在指定位置之前插入数据(空间足够才能插入数据)
- void SLInsert(SL* ps, SLDatatype x, int pos)
- {
- assert(ps);
- assert(pos >= 0 && pos <= ps->size);
- SLCheckCapacity(ps);
-
- //pos之后的数据向后移动一位
- for (int i = ps->size; i > pos; i--)
- {
- ps->arr[i] = ps->arr[i-1];
- }
- ps->arr[pos] = x;
- ps->size++;
- }
- //删除指定位置的数据
- void SLErase(SL* ps, int pos)
- {
- assert(ps);
- assert(pos >= 0 && pos <= ps->size);
-
- //pos之后的数据整体向前移动1位
- for (int i = 0; i < ps->size - 1; i++)
- {
- ps->arr[i] = ps->arr[i + 1];
- }
- ps->size--;
- }
-
- //查找数据
- int SLFind(SL* ps, SLDatatype x)
- {
- assert(ps);
- for (int i = 0; i < ps->size; i++)
- {
- if (ps->arr[i] = x)
- {
- return i;
- }
- }
- //没有找到,返回一个无效的下标
- return -1;
- }

3.test.c文件
- #include "Seqlist.h"
-
- void SLtest01()
- {
- SL s;
- SLInit(&s);
-
- //SLPushBack(&s, 1);
- //SLPushBack(&s, 2);
- //SLPushBack(&s, 3);
- //SLPushBack(&s, 4);
- //SLPushBack(&s, 5);
- //SLPushBack(&s, 6);
- //SLPushBack(NULL, 6);
-
- SLPushFront(&s, 1);
- SLPushFront(&s, 2);
- SLPushFront(&s, 3);
- SLPushFront(&s, 4);
-
- //尾删除
- //SLPopBack(&s);
- //SLPopBack(&s);
- //SLPopBack(&s);
- //SLPopBack(&s);
- //SLPopBack(&s);
-
- //头删除
- //SLPopFront(&s);
- //为空不能删除
-
- //SLInsert(&s,11,0);
- //SLInsert(&s,22,s.size);
-
- //SLErase(&s, 0);
- //SLErase(&s, s.size-1);
-
- SLPrint(&s);
- //查找数据
- int find = SLFind(&s, 2);
- if (find >= 0)
- {
- printf("找到了\n");
- }
- else
- {
- printf("没有找到\n");
- }
-
- }
- int main()
- {
- SLtest01();
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。