数据结构——顺序表基础知识

头文件:

#pragma once


#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <assert.h>


//////////////////////////////////////////////////////////////////////


//定义顺序表
#define MAX_SIZE 10
typedef int DataType;
typedef struct SeqList
{
DataType _array[MAX_SIZE];
int _size;
}SeqList, *PSeqList;


typedef struct SeqList SeqList;
typedef struct SeqList* PSeqList;


void SeqListlnit(PSeqList ps);                           // 初始化顺序表
void SeqListPushBack(PSeqList ps, DataType data);        //尾插
void SeqListPopBack(PSeqList ps);                        //尾删
void SeqListPushFront(PSeqList ps, DataType data);       //头插
void SeqListPopFront(PSeqList ps);                       //头删
void SeqListInsert(PSeqList ps, int pos, DataType data);  //在任意位置(pos位置)插入元素data
void SeqListErase(PSeqList ps, int pos);                  //删除任意位置(pos位置)的元素
void Remove(PSeqList ps, DataType data);                 //移除顺序表中第一个值为data的元素
void RemoveAll(PSeqList ps, DataType data);              //移除所有值为data的元素
int PSeqListFind(PSeqList ps, DataType data);            //找到顺序表中数值为data的元素,并且返回其位置,否则返回-1
int PSeqListSize(PSeqList ps);                              //求出顺序表中元素的个数
void SeqListPrint(PSeqList ps);                          //打印顺序表




////////////////////////////////////////////////////////////////////////////////////////////

顺序表的基础功能实现函数

#define _CRT_SECURE_NO_WARNINGS


#include "SeqList.h"




//打印顺序表
void SeqListPrint(PSeqList ps)
{
assert(ps);


int tem = 0;
printf("顺序表为:");
for (; tem < ps->_size; tem++)
{
printf("%d ", ps->_array[tem]);
}
printf("\n");
}


//初始化
void SeqListlnit(PSeqList ps)
{
assert(ps);
memset(ps, 0, sizeof(DataType)*MAX_SIZE);    //将ps多指向的内存全部赋值为0
ps->_size = 0;
}


//尾插
void SeqListPushBack(PSeqList ps, DataType data)
{
assert(ps);
ps->_array[ps->_size] = data;     //对size(正好是顺序表最后)进行赋值
ps->_size++;
}




//尾删
void SeqListPopBack(PSeqList ps)
{
assert(ps);


if (ps->_size == 0)      //判断顺序表中是否有数据
{
return;
}
ps->_size--;    ///直接减少数据个数  不清空数据
}


//头插
void SeqListPushFront(PSeqList ps, DataType data)
{
assert(ps);


int tem;


for (tem = ps->_size; tem > 0; tem--)
{
ps->_array[tem] = ps->_array[tem - 1];   //交换顺序表中的数据
}


ps->_array[0] = data;     // 将数据插入到顺序表开头
ps->_size++;


}


//头删
void SeqListPopFront(PSeqList ps)
{
assert(ps);


if (ps->_size == 0)      //判断顺序表中是否有数据
{
return;
}


int tem = 0;


for (; tem < ps->_size - 1; tem++)
{
ps->_array[tem] = ps->_array[tem + 1];    ///  直接覆盖第一个数据
}
ps->_size--;         ///数据个数减1
}




//在任意位置(pos位置)插入元素data
void SeqListInsert(PSeqList ps, int pos, DataType data)
{
assert(ps);


if (ps->_size == 0)    //判断顺序表中是否有数据
{
return;
}


int tem = 0;


tem = ps->_size;
for (; tem >= ps->_size - pos + 1; tem--)
{
ps->_array[tem] = ps->_array[tem - 1];    //将pos位置后面的数据依次往后移一位
}


ps->_array[pos - 1] = data;     // 将数据data 赋值于 pos位置


ps->_size++;      // 数据个数加1
}




//删除任意位置(pos位置)的元素
void SeqListErase(PSeqList ps, int pos)
{
assert(ps);


if (ps->_size == 0)    //判断顺序表中是否有数据
{
return;
}


int tem = 0;
for (tem = pos - 1; tem < ps->_size; tem++)
{
ps->_array[tem] = ps->_array[tem + 1];
}
ps->_size--;
}




//移除顺序表中第一个值为data的元素
void Remove(PSeqList ps, DataType data)
{
assert(ps);


int tem = 0;


if (ps->_size == 0)     //判断顺序表表中是否有元素
{
return;
}


tem = PSeqListFind(ps, data); //找到第一个值为data的元素位置


SeqListErase(ps, tem);    //删除此位置的元素
}






//移除所有值为data的元素
void RemoveAll(PSeqList ps, DataType data)
{
assert(ps);


int tem = 0;
int count = 0;
int str = 0;


if (ps->_size == 0)     //判断顺序表表中是否有元素
{
return;
}


while (tem < ps->_size)   //遍历顺序表
{
if (data == ps->_array[tem])  //找到data位置
{
tem++;                   
count++;
}
ps->_array[str] = ps->_array[tem];   //  重新对顺序表进行赋值
tem++;
str++;
}
}




//找到顺序表中数值为data的元素,并且返回其位置,否则返回-1
int PSeqListFind(PSeqList ps, DataType data)
{
assert(ps);


int tem = 0;


if (ps->_size == 0)
{
return -1;
}


for (; tem < ps->_size; tem++)
{
if (ps->_array[tem] == data)
{
return tem + 1;
}
}
}




//求出顺序表中元素的个数
int PSeqListSize(PSeqList ps)
{
assert(ps);

return ps->_size;

}



测试主函数:

#define _CRT_SECURE_NO_WARNINGS


#include "SeqList.h"


int main()
{
SeqList Seq;
int scr = 0;
SeqListlnit(&Seq);
SeqListPushBack(&Seq, 1);
SeqListPushBack(&Seq, 3);
SeqListPushBack(&Seq, 9);
SeqListPushBack(&Seq, 4);
SeqListPushBack(&Seq, 3);
SeqListPrint(&Seq);




RemoveAll(&Seq, 3);
Remove(&Seq, 9);


scr = PSeqListSize(&Seq);


printf("顺序表中有 %d 个元素\n", scr);
scr = PSeqListFind(&Seq, 3);
printf(" 4 出现在顺序表中的第 %d 个\n", scr);
SeqListPrint(&Seq);






RemoveAll(&Seq, 3);     // 移除顺序表中所有数值为3的元素
Remove(&Seq, 9);            //移除顺序表中数值为9的元素
SeqListErase(&Seq, 3);     //删除顺序表中第3位置的数据
SeqListInsert(&Seq, 3, 9); //在顺序表中第3位置插入数据9
SeqListPopBack(&Seq);  //尾删
SeqListPopFront(&Seq);   //头删
SeqListPushFront(&Seq, 0); // 头插
return 0;

}



各位大佬给意见  谢谢!

猜你喜欢

转载自blog.csdn.net/code_zx/article/details/79968110