目录
顺序表:取值时,优先判断是否为NULL,增加时要看是否满了(链表就无需担心)
null 、curSize、MaxSize
法一:直接把数据丢到末尾,然后依次swap至index指定位置
法二:现平移出位置,然后对空位进行赋值
优化:提供push_front()和push_back()两种接口
对于有序表:
step1:先将data插入到表的最后面,
step2:再依次和前面进行比较,找到比其小的位置(注意边界问题:不能越界&&记得不管是增还是删除,及时更新curSize!!!)
->真正的删除在curSize--
优化:提供pop_front()和pop_back()两种接口
sqlist.h
- #pragma once
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #define MAX 10 //最大元素个数
- typedef int DataType;
- //结构体抽象---> 抽象长相
- enum INFO {OK=1,ERROR=-1};
- typedef struct
- {
- DataType* dataMemory; //一级指针动态内存申请变成一位数组
- int maxSize; //最大元素个数-->容量
- int curSize; //当前元素个数
- }Sqlist,*LPSqlist;
-
- LPSqlist createSqlist(int maxSize);
- //万金油函数-->所有数据结构都可以有的
- int size(LPSqlist sqlist);
- int empty(LPSqlist sqlist);
- //增删改查
- DataType getData(LPSqlist sqlist, int index);
- int insertSqlist(LPSqlist sqlist, int index, DataType data);
- void printSqlist(LPSqlist sqlist);
- int deleteSqlist(LPSqlist sqlist, int index);
- void push_front(LPSqlist sqlist,DataType data); //头部插入
- void pop_front(LPSqlist sqlist);
- void push_back(LPSqlist sqlist, DataType data);
- void pop_back(LPSqlist sqlist);
- int searchSqlist(LPSqlist sqlist, DataType data);
- void insert_sort(LPSqlist sqlist, DataType data);
- void destroySqlist(LPSqlist* sqlist);
sqlist.c
- #include "sqlist.h"
- LPSqlist createSqlist(int maxSize)
- {
- //创建过程,就是初始化数据
- LPSqlist sqlist = (LPSqlist)malloc(sizeof(Sqlist));
- assert(sqlist);
- sqlist->curSize = 0;
- sqlist->maxSize = maxSize;
- //指针变成一个数组
- sqlist->dataMemory = (DataType*)malloc(sizeof(DataType)*maxSize);
- assert(sqlist->dataMemory);
- return sqlist;
- }
- int size(LPSqlist sqlist)
- {
- if (sqlist == NULL)
- return ERROR;
- return sqlist->curSize;
- }
- int empty(LPSqlist sqlist)
- {
- if (sqlist == NULL)
- return ERROR;
- return sqlist->curSize==0;
- }
-
- DataType getData(LPSqlist sqlist, int index)
- {
- if (sqlist == NULL||sqlist->curSize==0)
- {
- printf("sqlist is empty,unable to get......\n");
- return ERROR;
- }
- if ( sqlist->curSize < index)
- {
- //invalid 无效
- printf("index is invalid,unable to get......\n");
- return ERROR;
- }
- //index是第几个元素
- return sqlist->dataMemory[index-1];
- }
-
- int insertSqlist(LPSqlist sqlist, int index, DataType data)
- {
- //数组实现的数据结构:放进去需要考虑满
- if (sqlist == NULL)
- {
- printf("sqlist is empty,unable to insert......\n");
- return ERROR;
- }
- if (sqlist->curSize == sqlist->maxSize)
- {
- printf("sqlist is full,unbale to insert......\n");
- return ERROR;
- }
- if (index == 0)
- {
- sqlist->dataMemory[sqlist->curSize++] = data;
- return OK;
- }
- if (sqlist->curSize < 1 || sqlist->curSize < index)
- {
- printf("index is invalid,unable to insert......\n");
- return ERROR;
- }
- #if 0
- int pos = sqlist->curSize;
- sqlist->dataMemory[pos] = data; //插在最后面
- while (pos != index)
- {
- DataType temp = sqlist->dataMemory[pos];
- sqlist->dataMemory[pos] = sqlist->dataMemory[pos - 1];
- sqlist->dataMemory[pos - 1] = temp;
- pos--;
- }
- sqlist->curSize++;
- #endif
- for (int k = sqlist->curSize; k >= index; k--)
- {
- sqlist->dataMemory[k] = sqlist->dataMemory[k - 1];
- }
- sqlist->dataMemory[index - 1] = data;
- sqlist->curSize++;
- return OK;
- }
-
- void printSqlist(LPSqlist sqlist)
- {
- if (sqlist == NULL||sqlist->curSize==0)
- {
- printf("sqlist is empty,unable to print......\n");
- return;
- }
- for (int i = 0; i < sqlist->curSize; i++)
- {
- printf("%d\t", sqlist->dataMemory[i]);
- }
- printf("\n");
- }
- int deleteSqlist(LPSqlist sqlist, int index)
- {
- if (sqlist == NULL || sqlist->curSize == 0)
- {
- printf("sqlist is empty,unable to delete......\n");
- return ERROR;
- }
- if (sqlist->curSize < index)
- {
- printf("index is invalid,unbale to delete......\n");
- return ERROR;
- }
- for (int k = index - 1; k < sqlist->curSize; k++)
- {
- sqlist->dataMemory[k] = sqlist->dataMemory[k + 1];
- }
- sqlist->curSize--; //伪删除
- return OK;
- }
- void push_front(LPSqlist sqlist, DataType data)
- {
- insertSqlist(sqlist, 1, data);
- }
- void pop_front(LPSqlist sqlist)
- {
- deleteSqlist(sqlist, 1);
- }
- void push_back(LPSqlist sqlist, DataType data)
- {
- if (sqlist == NULL)
- {
- printf("sqlist is empty,unable to pushback");
- return;
- }
- if (sqlist->curSize == sqlist->maxSize)
- {
- printf("sqlist is full,unable to pushback");
- return;
- }
- sqlist->dataMemory[sqlist->curSize++] = data;
- }
- void pop_back(LPSqlist sqlist)
- {
- deleteSqlist(sqlist, sqlist->curSize);
- }
- int searchSqlist(LPSqlist sqlist, DataType data)
- {
- if (sqlist == NULL)
- {
- printf("sqlist is empty,unable to search......\n");
- return ERROR;
- }
- for (int i = 0; i < sqlist->curSize; i++)
- {
- if (sqlist->dataMemory[i] == data)
- {
- return i;
- }
- }
- return ERROR;
- }
- void insert_sort(LPSqlist sqlist, DataType data)
- {
- if (sqlist == NULL)
- {
- printf("sqlist is empty,unable to insert......\n");
- return;
- }
- if (sqlist->curSize == sqlist->maxSize)
- {
- printf("sqlist is full,unable to insert......\n");
- return;
- }
- if (sqlist->curSize == 0)
- {
- sqlist->dataMemory[sqlist->curSize++] = data;
- }
- sqlist->dataMemory[sqlist->curSize] = data;
- int pos = sqlist->curSize;
- for (int k = sqlist->curSize;k>0; k--)
- {
- if (sqlist->dataMemory[k] < sqlist->dataMemory[k - 1])
- {
- DataType temp = sqlist->dataMemory[k];
- sqlist->dataMemory[k] = sqlist->dataMemory[k - 1];
- sqlist->dataMemory[k - 1] = temp;
- }
- else
- {
- break;
- }
- }
- sqlist->curSize++;
- }
-
- void destroySqlist(LPSqlist* sqlist)
- {
- if (*sqlist == NULL)
- {
- printf("sqlist is empty, unable to destory......\n");
- return;
- }
- free((*sqlist)->dataMemory);
- free(*sqlist);
- *sqlist = NULL;
- }
Main顺序表.c
- #include "sqlist.h"
- int main()
- {
- LPSqlist sqlist = createSqlist(MAX);
- printf("test insert:\n");
- insertSqlist(sqlist, 0, 0); //0
- insertSqlist(sqlist, 1, 1); //1 0
- insertSqlist(sqlist, 1, 2); //2 1 0
- insertSqlist(sqlist, 1, 3); //3 2 1 0
- insertSqlist(sqlist, 4, 444);
- printSqlist(sqlist);
- printf("test delete:\n");
- deleteSqlist(sqlist, 4);
- printSqlist(sqlist);
- printf("test push and pop:\n");
- push_front(sqlist, 666);
- push_back(sqlist, 999);
- printSqlist(sqlist);
- pop_front(sqlist);
- pop_back(sqlist);
- printSqlist(sqlist);
- int pos = searchSqlist(sqlist, 3);
- printf("pos:%d\n", pos);
- destroySqlist(&sqlist);
- LPSqlist sortSqlist = createSqlist(MAX);
- insert_sort(sortSqlist, 1);
- insert_sort(sortSqlist, 100);
- insert_sort(sortSqlist, 77);
- insert_sort(sortSqlist, -1);
- printSqlist(sortSqlist);
- destroySqlist(&sortSqlist);
- return 0;
- }
输出:
test insert:
3 2 1 444 0
test delete:
3 2 1 0
test push and pop:
666 3 2 1 0 999
3 2 1 0
pos:0
-1 1 1 77 100
sqlist.h
- #pragma once
- #pragma once
- #include <iostream>
- using namespace std;
- #define MAX 10 //最大元素个数
- typedef int DataType;
- //结构体抽象---> 抽象长相
- enum INFO { OK = 1, ERROR = -1 };
- class Sqlist
- {
- public:
- Sqlist(int MaxSize);
- int size() const;
- int empty()const;
- DataType getData(int index);
- int insertSqlist(int index, DataType data);
- void printSqlist();
- int deleteSqlist( int index);
- void push_front( DataType data); //头部插入
- void pop_front();
- void push_back(DataType data);
- void pop_back();
- int searchSqlist( DataType data);
- void insert_sort(DataType data);
- void destroySqlist();
- protected:
- DataType* dataMemory;
- int maxSize;
- int curSize;
- };
sqlist.cpp
- #include "sqlist.h"
-
- Sqlist::Sqlist(int MaxSize)
- {
- //创建过程,就是初始化数据
- curSize = 0;
- maxSize = MaxSize;
- //指针变成一个数组
- dataMemory =new DataType[maxSize];
- }
-
- int Sqlist::size() const
- {
- if (dataMemory == NULL)
- return ERROR;
- return curSize;
- }
-
- int Sqlist::empty() const
- {
- if (dataMemory == NULL)
- return ERROR;
- return curSize==0;
- }
-
- DataType Sqlist::getData(int index)
- {
- if (curSize == 0)
- {
- cout<<"sqlist is empty,unable to get......"<<endl;
- return ERROR;
- }
- if (curSize < index)
- {
- //invalid 无效
- cout << "index is invalid,unable to get......" << endl;;
- return ERROR;
- }
- //index是第几个元素
- return dataMemory[index - 1];
- }
-
- int Sqlist::insertSqlist(int index, DataType data)
- {
- //数组实现的数据结构:放进去需要考虑满
- if (curSize == maxSize)
- {
- printf("sqlist is full,unbale to insert......\n");
- return ERROR;
- }
- if (index == 0)
- {
- dataMemory[curSize++] = data;
- return OK;
- }
- if (curSize < 1 || curSize < index)
- {
- printf("index is invalid,unable to insert......\n");
- return ERROR;
- }
- #if 0
- int pos = sqlist->curSize;
- dataMemory[pos] = data; //插在最后面
- while (pos != index)
- {
- DataType temp = dataMemory[pos];
- dataMemory[pos] = dataMemory[pos - 1];
- dataMemory[pos - 1] = temp;
- pos--;
- }
- curSize++;
- #endif
- for (int k = curSize; k >= index; k--)
- {
- dataMemory[k] = dataMemory[k - 1];
- }
- dataMemory[index - 1] = data;
- curSize++;
- return OK;
- }
-
- void Sqlist::printSqlist()
- {
- if (curSize == 0)
- {
- cout << "sqlist is empty,unable to print......\n" << endl;
- return;
- }
- for (int i = 0; i < curSize; i++)
- {
- printf("%d\t", dataMemory[i]);
- }
- printf("\n");
- }
-
- int Sqlist::deleteSqlist(int index)
- {
- if (curSize == 0)
- {
- cout<<"sqlist is empty,unable to delete......\n";
- return ERROR;
- }
- if (curSize < index)
- {
- cout<<"index is invalid,unbale to delete......\n";
- return ERROR;
- }
- for (int k = index - 1; k < curSize; k++)
- {
- dataMemory[k] = dataMemory[k + 1];
- }
- curSize--; //伪删除
- return OK;
- }
-
- void Sqlist::push_front(DataType data)
- {
- insertSqlist(1, data);
- }
-
- void Sqlist::pop_front()
- {
- deleteSqlist(1);
- }
-
- void Sqlist::push_back(DataType data)
- {
- if (curSize == maxSize)
- {
- printf("sqlist is full,unable to pushback");
- return;
- }
- dataMemory[curSize++] = data;
- }
-
- void Sqlist::pop_back()
- {
- deleteSqlist(curSize);
- }
-
- int Sqlist::searchSqlist(DataType data)
- {
- for (int i = 0; i < curSize; i++)
- {
- if (dataMemory[i] == data)
- {
- return i;
- }
- }
- return ERROR;
- }
-
- void Sqlist::insert_sort(DataType data)
- {
- if (curSize ==maxSize)
- {
- cout<<"sqlist is full,unable to insert......\n";
- return;
- }
- if (curSize == 0)
- {
- dataMemory[curSize++] = data;
- }
- dataMemory[curSize] = data;
- int pos = curSize;
- for (int k = curSize; k > 0; k--)
- {
- if (dataMemory[k] < dataMemory[k - 1])
- {
- DataType temp = dataMemory[k];
- dataMemory[k] = dataMemory[k - 1];
- dataMemory[k - 1] = temp;
- }
- else
- {
- break;
- }
- }
- curSize++;
- }
-
- void Sqlist::destroySqlist()
- {
- delete [] dataMemory;
- dataMemory = nullptr;
- }
Main顺序表.cpp
- #include "sqlist.h"
- int main()
- {
- Sqlist *pSqlist=new Sqlist(MAX);
- printf("test insert:\n");
- pSqlist->insertSqlist( 0, 0); //0
- pSqlist->insertSqlist( 1, 1); //1 0
- pSqlist->insertSqlist( 1, 2); //2 1 0
- pSqlist->insertSqlist( 1, 3); //3 2 1 0
- pSqlist->insertSqlist( 4, 444);
- pSqlist->printSqlist();
- printf("test delete:\n");
- pSqlist->deleteSqlist( 4);
- pSqlist->printSqlist();
- printf("test push and pop:\n");
- pSqlist->push_front(666);
- pSqlist->push_back(999);
- pSqlist->printSqlist();
- pSqlist->pop_front();
- pSqlist->pop_back();
- pSqlist->printSqlist();
- int pos = pSqlist->searchSqlist(3);
- printf("pos:%d\n", pos);
- pSqlist->destroySqlist();
- delete pSqlist;
- pSqlist = nullptr;
- Sqlist sortSqlist(10);
- sortSqlist.insert_sort(1);
- sortSqlist.insert_sort(100);
- sortSqlist.insert_sort(77);
- sortSqlist.insert_sort(-1);
- sortSqlist.printSqlist();
- sortSqlist.destroySqlist();
- return 0;
- }