对顺序表的基本操作
线性顺序表最基本的数据结构,所以掌握顺序表的头插、尾插、头删、尾删、排序、二分查找等操作显得尤为重要!
线性顺序表的实现必须创建一个结构体来保存线性表的元素和元素的个数:
typedef struct SeqList
{
DataType arr[MAX];
int count; //统计顺序表元素个数
}SeqList, *pSeqList;
建一个工程,并新建三个文件 1、seqlist.c 2、seqlist.h 3、test.c
在test.c文件中写main函数实现测试;
在seqlist.h文件中完成各函数的声明和顺序表结构体的定义;
在seqlist.c文件中实现具体的函数;
具体代码如下:
seqlist.h文件:
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#define MAX 1000
typedef int DataType;
typedef struct SeqList
{
DataType arr[MAX];
int count; //统计顺序表元素个数
}SeqList, *pSeqList;
void menu();
void InitSeqList(pSeqList p); //初始化顺序表
void PushBack(pSeqList p, DataType d); //尾插
void PopBack(pSeqList p); //尾删
void PushFront(pSeqList p, DataType d); //头插
void PopFront(pSeqList p); //头删
int Find(pSeqList p, DataType d); //查找(返回下标)
void Remove(pSeqList p, DataType d); //删除第一个找到的元素
void RemoveAll(pSeqList p, DataType d); //删除所有找到的元素
void Show(pSeqList p); //打印顺序表
void Sort(pSeqList p); //排序
int BinarySeach(pSeqList p, DataType d); //二分查找
#endif //__SEQLIST_H__
seqlist.c文件:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"seqlist.h"
void menu()
{
printf("***********************************\n");
printf("****1.PushBack 2.PopBack ****\n");
printf("****3.PushFront 4.PopFront ****\n");
printf("****5.Find 6.Remove ****\n");
printf("****7.RemoveAll 8.Show ****\n");
printf("****9.Sort 10.BinarySeach****\n");
printf("**** 0.exit ****\n");
printf("***********************************\n");
}
void InitSeqList(pSeqList p)
{
memset(p->arr, 0, sizeof(DataType)*MAX);
p->count = 0;
}
void PushBack(pSeqList p, DataType d) //尾插
{
assert(p);
if (p->count == MAX)
{
printf("顺序表已满!\n");
return;
}
p->arr[p->count] = d;
p->count++;
}
void PopBack(pSeqList p) //尾删
{
assert(p);
if (p->count == 0)
{
printf("顺序表已空!\n");
return;
}
p->count--; //直接将有效元素减一即可
}
void PushFront(pSeqList p, DataType d) //头插
{
assert(p);
int i = p->count-1;
if (p->count == MAX)
{
printf("顺序表已满!\n");
return;
}
for (i = p->count - 1; i >= 0; i--)
{
p->arr[i + 1] = p->arr[i];
}
p->arr[0] = d;
p->count++;
}
void PopFront(pSeqList p) //头删
{
assert(p);
int i = 0;
if (p->count == 0)
{
printf("顺序表已空!\n");
return;
}
for (i = 0; i < p->count; i++)
{
p->arr[i] = p->arr[i + 1];
}
p->count--;
}
int Find(pSeqList p, DataType d) //查找(返回下标)
{
assert(p);
int i = 0;
for (i = 0; i < p->count; i++)
{
if (p->arr[i] == d)
{
return i;
}
}
return -1;
}
void Remove(pSeqList p, DataType d) //删除第一个找到的元素
{
assert(p);
int ret = 0;
ret = Find(p, d);
if (ret == -1)
{
printf("找不到要删除的元素\n");
return;
}
for (int i = ret; i < p->count-1; i++)
{
p->arr[i] = p->arr[i + 1];
}
p->count--;
printf("删除成功!\n");
}
void RemoveAll(pSeqList p, DataType d) //删除所有找到的元素
{
assert(p);
int ret = 0;
while (ret != -1)
{
ret = Find(p, d);
if (ret != -1)
{
for (int i = ret; i < p->count - 1; i++)
{
p->arr[i] = p->arr[i + 1];
}
p->count--;
}
}
}
void Show(pSeqList p) //打印顺序表
{
assert(p);
int i = 0;
for (i = 0; i < p->count;i++)
{
printf("%d ", p->arr[i]);
}
printf("\n");
}
void Sort(pSeqList p) //排序
{
assert(p);
for (int i = 0; i < p->count - 1; i++)
{
for (int j = 0; j < p->count - 1 - i; j++)
{
if (p->arr[j] > p->arr[j + 1])
{
int tmp = p->arr[j];
p->arr[j] = p->arr[j + 1];
p->arr[j + 1] = tmp;
}
}
}
}
int BinarySeach(pSeqList p, DataType d) //二分查找
{
assert(p);
Sort(p); //要进行二分查找先进行排序
int left = 0;
int right = p->count - 1;
while (left <= right)
{
int mid =left - ((left - right) >> 1);
if (p->arr[mid] > d)
{
right = mid - 1;
}
else if (p->arr[mid] < d)
{
left = mid + 1;
}
else
{
return mid;
}
}
return -1;
}
test.c文件:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"seqlist.h"
void test()
{
int input = 0;
SeqList list;
InitSeqList(&list);
do
{
int ret = 0;
int data = 0;
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 0:
break;
case 1:
{
printf("请输入要尾插的元素:");
scanf("%d",&data);
PushBack(&list, data);
}
break;
case 2:
PopBack(&list);
printf("尾删成功!\n");
break;
case 3:
{
printf("请输入要头插的元素:");
scanf("%d", &data);
PushFront(&list, data);
}
break;
case 4:
PopFront(&list);
printf("头删成功!\n");
break;
case 5:
{
printf("请输入要查找的元素:");
scanf("%d", &data);
ret = Find(&list, data);
if (ret != -1)
{
printf("该元素下标为:%d\n", ret);
}
else
{
printf("查找不到该元素!\n");
}
}
break;
case 6:
{
printf("请输入要删除的元素:");
scanf("%d", &data);
Remove(&list, data);
}
break;
case 7:
{
printf("请输入要删除的元素:");
scanf("%d", &data);
RemoveAll(&list, data);
}
break;
case 8:
Show(&list);
break;
case 9:
Sort(&list);
printf("排序成功!\n");
break;
case 10:
{
printf("请输入要查找的元素:");
scanf("%d", &data);
ret = BinarySeach(&list, data);
if (ret == -1)
{
printf("无该元素!\n");
}
else
{
printf("该元素下标为:%d\n", ret);
}
}
break;
default:
printf("请正确选择!\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}