//"common.h"
#ifndef _COMMON_H_
#define _COMMON_H_
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <assert.h>
#include <stdbool.h>
#include <vld.h>//检查内存泄漏
#pragma warning(disable:4996)
#define ElemType int
#define SLDateType size_t
#endif _COMMON_H_
//"seqlist.h"
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
#include "common.h"
#define SEQLIST_DEFAULT_SIZE 20//顺序表的容量
typedef struct SeqList//顺序表
{
ElemType* data;
size_t capacity;//容量
size_t size;//个数
}SeqList;
void SeqListInit(SeqList* plist);//初始化
void SeqListDestory(SeqList* ps);//清空
void SeqListPrint(SeqList* ps);//打印
void SeqListPushBack(SeqList* ps, SLDateType x);//尾插
void SeqListPushFront(SeqList* ps, SLDateType x);//头插
void SeqListPopFront(SeqList* ps);//头删
void SeqListPopBack(SeqList* ps);//尾删
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos);
//
bool IsFull(SeqList* ps)//判断顺序表是否为满
{
assert(ps);
return ps->size >= ps->capacity;
}
bool IsEmpty(SeqList* ps)//判断顺序表是否为空
{
assert(ps);
return ps->size == 0;
}
void SeqListInit(SeqList* ps)
{
assert(ps != NULL);
ps->capacity = SEQLIST_DEFAULT_SIZE;
ps->data = (ElemType*)malloc(sizeof(ElemType) * ps->capacity);
ps->size = 0;
}
void SeqListDestory(SeqList* ps)
{
assert(ps);
ps->size = 0;
}
void SeqListPrint(SeqList* ps)
{
assert(ps);
size_t i = 0;
while (i < ps->size){
printf("%d->", ps->data[i++]);
}
printf("over\n");
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
assert(ps != NULL);
if (IsFull(ps)){//判断是否已满
printf("顺序表已满!\n");
return;
}
ps->data[ps->size++] = x;
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
assert(ps);
if (IsFull(ps)){//判断是否已满
printf("顺序表已满!\n");
return;
}
size_t i = ps->size;
while (i > 0){
ps->data[i] = ps->data[i - 1];
i--;
}
ps->data[0] = x;
ps->size++;
}
void SeqListPopFront(SeqList* ps)
{
assert(ps);
if (IsEmpty(ps)){
printf("顺序表已空,无法删除!\n");
return;
}
size_t i = 0;
while (i < ps->size - 1){
ps->data[i] = ps->data[i + 1];
i++;
}
ps->size--;
printf("删除成功!\n");
}
void SeqListPopBack(SeqList* ps)
{
assert(ps);
if (IsEmpty(ps)){
printf("顺序表已空,无法删除!\n");
return;
}
ps->size--;
printf("删除成功!\n");
}
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x)
{
assert(ps);
if (IsEmpty(ps)){
printf("顺序表已空,无法查找!\n");
return -1;
}
size_t i = 0;
while (i < ps->size){
if (ps->data[i] == x){
return i;
}
i++;
}
return -1;
}
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x)
{
assert(ps);
if (IsFull(ps)){//判断是否已满
printf("顺序表已满!\n");
return;
}
if (pos >= 0 && pos <= ps->size){
size_t i = ps->size;
while (i > pos){
ps->data[i] = ps->data[i - 1];
i--;
}
ps->data[pos] = x;
ps->size++;
printf("插入成功!\n");
}
}
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos)
{
assert(ps);
if (IsEmpty(ps)){
printf("顺序表已空,无法删除!\n");
return;
}
if (pos >= 0 && pos < ps->size){
size_t i = pos;
while (i < ps->size - 1){
ps->data[i] = ps->data[i + 1];
i++;
}
ps->size--;
printf("删除成功!\n");
}
}
#endif _SEQLIST_H_
//"main.c"
#include "seqList.h"
int main()
{
int input = 1;//输入
int select = 1;//选择
SeqList List;//定义顺序表
SeqListInit(&List);//顺序表初始化
int value = 0;//值
int pos = 0;//位置
do{
printf("*********************************************\n");
printf("* [1] push_back [2] push_front *\n");
printf("* [3] show_list [4] find_pos *\n");
printf("* [5] pop_back [6] pop_front *\n");
printf("* [7] insert_pos [8] pop_pos *\n");
printf("* [9] destory [0]exit *\n");
printf("*********************************************\n");
printf("请选择:>");
scanf("%d", &select);
switch (select){
case 0:
return 0;
case 1:
printf("请输入数据(以-1为结束标志):>");
while (scanf("%d", &input) && input != -1){
SeqListPushBack(&List, input);
}
printf("尾插法插入成功!\n");
break;
case 2:
printf("请输入数据(以-1为结束标志):>");
while (scanf("%d", &input) && input != -1){
SeqListPushFront(&List, input);
}
printf("头插法插入成功!\n");
break;
case 3:
SeqListPrint(&List);
break;
case 4:
printf("请输入要查找的值:>");
scanf("%d", &value);
printf("下标为:%d", SeqListFind(&List, pos));
break;
case 5:
SeqListPopBack(&List);
break;
case 6:
SeqListPopFront(&List);
break;
case 7:
printf("请输入要插入的值:>");
scanf("%d", &value);
printf("请输入要插入的位置:>");
scanf("%d", &pos);
SeqListInsert(&List, pos, value);
break;
case 8:
printf("请输入要删除的位置:>");
scanf("%d", &pos);
SeqListErase(&List, pos);
break;
case 9:
SeqListDestory(&List);
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
system("pause");
system("cls");
} while (input);
system("pause");
return 0;
}
顺序表的增删查改(C语言实现)
最新推荐文章于 2024-05-01 21:00:53 发布