静态数组实现的顺序表(C语言实现)
程序员文章站
2024-03-20 14:17:04
...
在写顺序表之前我们先来了解下相关的一些知识,以便我们后期能够写出更好的东西。
数据结构是相互之间存在一种或多种特定关系的数据元素的集合。根据数据元素之间关系的不同特性,通常有如下种基本结构:
集合:结构中的元素之间除了“同属于一个集合”的关系外,别无其他的关系。如:广义表。
线性结构:结构中的数据元素之间存在着一个对一个的关系。如:链表。
树形结构:结构中的数据元素之间存在着一个对多个的关系。如二叉树。
图(网)状结构:结构中的数据元素之间存在着多个对多个的关系。如:图。
在线性结构中,根据存储方式分为顺序表、链表,根据对表的操作限制,分为栈和队列。
顺序表的特征是:在内存中占用连续的存储单元,可以简单的理解为顺序表就是数组,知识根据需要,在实际应用中动态分配顺序表用的内存单元,而数组是在编译的时候,预分配了指定大小的内存单元,顺序表采用的是数组,但是数组有静态数组和动态数组所以顺序表有静态顺序表和动态顺序表。
顺序表的功能如下:
1、初始化
2、尾插
3、尾删
4、头插
5、头删
6、任意位置插入
7、任意位置删除
8、读取任意位置元素并返回元素值
9、更改任意位置元素值
10、查找任意元素,并返回数组的下标
test.h
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 1000
typedef int DataType;//定义数据的类型;
typedef struct SeqList
{
DataType arr[MAX_SIZE];
int size;//顺序表的长度
}SeqList;
void SeqListPrint(SeqList *seqlist);//打印顺序表
void SeqListInit(SeqList *seqlist);//初始化顺序表
void SeqListPushFront(SeqList* seqlist, DataType value);//头插
void SeqListPushBack(SeqList* seqlist, DataType value);//尾插
void SeqListPopFront(SeqList* seqlist);//头删
void SeqListPopBack(SeqList* seqlist);//尾删
void SeqListErase(SeqList* seqlist);//任意位置删
void SeqListInsert(SeqList* seqlist, int pos, DataType value);//任意位置插入
DataType SeqListRead(SeqList* seqlist, int pos);//读取任意位置元素,并返回元素值
void SeqListChange(SeqList* seqlist, int pos, DataType value);//更改任意位置元素值
DataType SeqListFindi(SeqList* seqlist, DataType value);//查找任意位置数组元素,并返回数组下标
test.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include "test.h"
void SeqListPrint(SeqList* seqlist)//打印顺序表中的数组
{
//printf(__FUNCTION__);
if (seqlist == NULL){
//__FUNCTION__功能是以编程方式从在当前运行的函数中得到函数名;
return;
}
int i = 0;
int len = 0;
len = seqlist->size;
for (i = 0; i < len; i++)
{
printf("%d ", seqlist->arr[i]);
}
printf("\n");
}
void SeqListInit(SeqList* seqlist)//初始化顺序表
{
if (seqlist == NULL){
printf(__FUNCTION__);
return;
}
memset(seqlist->arr, 0, MAX_SIZE*sizeof(DataType));
seqlist->size = 0;
}
void SeqListPushFront(SeqList* seqlist, DataType value)//头插
{
if (seqlist == NULL){
printf(__FUNCTION__);
return;
}
if (seqlist->size == MAX_SIZE){
printf("顺序表已满\n");
}
int i = seqlist->size;
for (; i > 0; i--)
{
seqlist->arr[i] = seqlist->arr[i -1];
}
seqlist->arr[0] = value;
(seqlist->size)++;
}
void SeqListPopFront(SeqList* seqlist)//头删
{
if (seqlist == NULL){
printf(__FUNCTION__);
return;
}
if (seqlist->size == 0){
printf("结构体已经空了\n");
}
int i = 0;
for (i = 1; i < seqlist->size; i++){
seqlist->arr[i] = seqlist->arr[i - 1];
}
seqlist->size--;
}
void SeqListPushBack(SeqList* seqlist, DataType value) //尾插
{
//printf(__FUNCTION__);
if (seqlist == NULL){
printf("非法输入\n");
return;
}
if (seqlist->size == MAX_SIZE){
printf("顺序表已满\n");
return;
}
seqlist->arr[seqlist->size] = value;
seqlist->size++;
}
void SeqListPopBack(SeqList* seqlist)//尾删
{
if (seqlist == NULL){
printf(__FUNCTION__);
return;
}
if (seqlist->size == 0){
printf("结构体已经空了\n");
}
seqlist->size--;
}
void SeqListInsert(SeqList* seqlist, int pos, DataType value)//任意位置插入
{
if (seqlist == NULL){
return;
}
if (pos > seqlist->size){
return;
}
if (seqlist->size == MAX_SIZE){
printf("顺序表已满\n");
}
int i = seqlist->size-1 ;
for (; i>=pos-1; --i){
seqlist->arr[i+1] = seqlist->arr[i];
}
seqlist->arr[pos-1] = value;
seqlist->size++;
return;
}
void SeqListErase(SeqList* seqlist,int pos)//任意位置删
{
if (seqlist == NULL){
//printf(__FUNCTION__);
return;
}
if (seqlist->size == 0){
printf("顺序表空了\n");
return;
}
if (pos > seqlist->size){
printf("位置错误\n");
return;
}
int i =pos ;
for ( ; i < seqlist->size; i++){
seqlist->arr[i-1] = seqlist->arr[i];
}
seqlist->size--;
}
DataType SeqListRead(SeqList* seqlist, int pos)//读取任意位置元素,并返回元素值
{
if (seqlist == NULL){
return;
}
if (seqlist->size == 0){
printf("结构体为空\n");
return;
}
if ((pos >= seqlist->size) || (pos<0)){
printf("没有找到\n");
}
else
printf("%d", seqlist->arr[pos]);
return seqlist->arr[pos];
}
void SeqListChange(SeqList* seqlist, int pos, DataType value)//更改任意位置元素值
{
if (seqlist == NULL){
return;
}
if ((pos >= seqlist->size)||(pos<0)){
printf("非法更改\n");
return;
}
else
printf("%d\n", seqlist->arr[pos]);
seqlist->arr[pos] = value;
return;
}
DataType SeqListFindi(SeqList* seqlist, int pos)//查找任意位置数组元素,并返回数组下标
{
if (seqlist == NULL){
return;
}
if (seqlist->size == 0){
printf("结构体为空\n");
return;
}
if ((pos >= seqlist->size) || (pos<0)){
printf("访问非法\n");
}
else{
printf("%d", seqlist->arr[pos]);
}
return pos ;
}
main.c
#define _CRT_SECURE_NO_WARNINGS
#include "test.h"
void test1()//任意位置插入的测试
{
SeqList seqlist;
SeqListInit(&seqlist);
SeqListPushBack(&seqlist, 1);
SeqListPushBack(&seqlist, 2);
SeqListPushBack(&seqlist, 3);
SeqListPushBack(&seqlist, 4);
SeqListPushBack(&seqlist, 5);
SeqListPrint(&seqlist);
SeqListInsert(&seqlist, 3, 6);
SeqListInsert(&seqlist, 1, 8);
SeqListPrint(&seqlist);
SeqListErase(&seqlist, 2);
SeqListErase(&seqlist, 2);
SeqListErase(&seqlist, 2);
SeqListErase(&seqlist, 2);
SeqListErase(&seqlist, 2);
SeqListErase(&seqlist, 8);
SeqListPrint(&seqlist);
}
void test2()//读取任意位置元素,并返回元素值
{
SeqList seqlist;
SeqListInit(&seqlist);
SeqListPushBack(&seqlist, 1);
SeqListPushBack(&seqlist, 2);
SeqListPushBack(&seqlist, 3);
SeqListPushBack(&seqlist, 4);
SeqListPushBack(&seqlist, 5);
SeqListPrint(&seqlist);
SeqListRead(&seqlist, -1);
}
void test3()//更改任意位置元素值
{
SeqList seqlist;
SeqListInit(&seqlist);
SeqListPushBack(&seqlist, 2);
SeqListPushBack(&seqlist, 3);
SeqListPushBack(&seqlist, 4);
SeqListPrint(&seqlist);
SeqListChange(&seqlist, -1,8);
SeqListPrint(&seqlist);
}
void test4(){
//查找任意位置数组元素,并返回数组下标
SeqList seqlist;
SeqListInit(&seqlist);
SeqListPushBack(&seqlist, 4);
SeqListPushBack(&seqlist, 5);
SeqListPushBack(&seqlist, 6);
SeqListPushBack(&seqlist, 7);
SeqListPushBack(&seqlist, 8);
SeqListPrint(&seqlist);
SeqListFindi(&seqlist, 5);
}
void testseqlistpush()
{
SeqList seqlist;
SeqListInit(&seqlist);
SeqListPushBack(&seqlist, 2);
SeqListPushBack(&seqlist, 3);
SeqListPushBack(&seqlist, 4);
SeqListPrint(&seqlist);
SeqListPushBack(&seqlist,8);//尾插
SeqListPushBack(&seqlist,9);
SeqListPushBack(&seqlist,10 );
SeqListPrint(&seqlist);
SeqListPopBack(&seqlist);
SeqListPopBack(&seqlist);
SeqListPrint(&seqlist);
}
int main()
{
//SeqList seqlist;
//SeqListInit(&MySqeList);//初始化顺序表
//SeqListPushFront(&MySqeList,1);//头插
//SeqListPushFront(&MySqeList, 2);//头插
//SeqListPushFront(&MySqeList, 3);//头插
//SeqListPushFront(&MySqeList,4);//头插
//SeqListPushFront(&MySqeList, 5);//头插
//SeqListPopFront(&MySqeList);//头删
//SeqListPrint(&MySqeList);
//SeqListPushBack(&seqlist,8);//尾插
//SeqListPushBack(&seqlist,9);
//SeqListPushBack(&seqlist,10 );
//SeqListPopBack(&seqlist);
//SeqListPopBack(&seqlist);
//SeqListPrint(&seqlist);
//testseqlistpush();
//test1();
//test2();
//test3();
test4();
getchar();
return 0;
}
简单的顺序表就写到这了,后期会继续完善的!!!!上一篇: 顺序表的c语言实现
下一篇: Servlet Cookie