顺序表的初始化,取值,查找,插入,删除操作

//在几个数字之间实现取值,查找,插入,和删除操作
#include<iostream>
#include<string>
using namespace std;

#define MAXSIZE 100
#define OK 1
#define OVERFLOW -2
#define ERROR -2

typedef struct
{
	int *elem;
	int length;
}sqlist;

//初始化
int InitList(sqlist &L)
{
	L.elem = new int[MAXSIZE];
	if (!L.elem)
	{
		exit(OVERFLOW);
	}
	L.length = 0;
	return OK;
}
//插入操作
int ListInsert_Sq(sqlist &L, int i, int e)
{
	if (i<1 || i>L.length + 1)
	{
		return -1;	//i值不合法
	}
	if (L.length == MAXSIZE)
	{
		return ERROR;   //当前存储空间已满  
	}
	for (int j = L.length - 1; j >= i - 1; j--)
	{
		L.elem[j + 1] = L.elem[j];  //插入位置及之后的元素后移
	}

	L.elem[i - 1] = e;   //将新元素e放入第i个位置
	++L.length;		    //表长增1
	return OK;
}
//取值操作
int GetElem(sqlist L, int i, int &e)
{
	if (i<1 || i>L.length)
	{
		return ERROR;
	}
	e = L.elem[i - 1];   //第i-1的单元存储着第i个数据
	return OK;
}

//顺序表的查找
int LocateELem(sqlist L, int e)
{
	for (int i = 0; i < L.length; i++)
	{
		if (L.elem[i] == e)
		{
			return i + 1;
		}
		
	}
	return 0;
	
}


//顺序表的删除
int ListDelete_Sq(sqlist &L, int i)
{
	if ((i < 1) || (i > L.length))
	{
		return ERROR;	 //i值不合法
	}
	for (int j = i; j <= L.length - 1; j++)
	{
		L.elem[j - 1] = L.elem[j];       //被删除元素之后的元素前移  
	}
	--L.length;               	                 //表长减1
	return OK;
}

int main()
{
	sqlist L;
	int initflag=InitList(L);
	if (initflag == 1)
	{
		cout << "创建成功" << endl;
	}
	else
	{
		cout << "创建失败" << endl;;
	}
	for (int i = 0; i < 20; i++)
	{
		L.elem[i] = i + 100;
	}
	L.length = 20;
	for (int j = 0; j < 20; j++)
	{
		cout << L.elem[j] << " ";
	}
		
	//2.获得元素
	int k, value, getElem_flag;
	cout << endl;
	cout << "请输入序号取值:";
	cin >> k;
	getElem_flag = GetElem(L, k, value);
	if (getElem_flag == 1)
	{
		cout << "获取到的数据为:" << value << endl;
	}
	else
	{
		cout << "输入的位置越界!" << endl;

	}

	//3.查找值
	cout << endl;
	cout << endl;
	int e, local_flag;
	cout << "输入查找指定的值:";
	cin >> e;
	local_flag = LocateELem(L, e);
	if (local_flag)
	{
		cout << "该元素的位置是:" << local_flag << endl;
	}
		
	else
	{
		cout << "没有找到该元素!" << endl;
	}
	//4.插入元素
	cout << endl;
	int Insert_i, Insert_value, Insert_flag;
	cout << "请输入插入的序号:";
	cin >> Insert_i;
	cout << "请输入插入的值:";
	cin >> Insert_value;
	Insert_flag = ListInsert_Sq(L, Insert_i, Insert_value);
	if (Insert_flag)
	{  //插入成果循环输出L中元素
		cout << "插入成功!" << endl;
		cout << "顺序表长度:" << L.length << endl;
		for (int h = 0; h < L.length; h++)
		{
			cout << L.elem[h] << " ";
		}
			
		cout << endl;
	}

	//5.删除元素
	cout << endl;
	int delete_i, delete_flag;
	cout << "请输入要删除的序号:";
	cin >> delete_i;
	delete_flag = ListDelete_Sq(L, delete_i);
	if (delete_flag)
	{
		//删除成功
		cout << "删除成功!" << endl;
		cout << "顺序表的长度:" << L.length << endl;
		for (int h = 0; h < L.length; h++)
		{
			cout << L.elem[h] << " ";
		}
			
		cout << endl;
	}
	system("pause");
	return 0;
}


  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单的图书表的实,包括了您所需的功能: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 100 // 图书信息结构体 typedef struct Book { char name[50]; // 书名 char author[30]; // 作者 char publisher[30]; // 出版社 float price; // 价格 } Book; // 顺序表结构体 typedef struct SeqList { Book data[MAX_SIZE]; // 数组存储数据 int length; // 当前长度 } SeqList; // 初始化顺序表 void InitList(SeqList* L) { L->length = 0; } // 插入元素 int Insert(SeqList* L, int pos, Book book) { if (pos < 1 || pos > L->length + 1) { printf("插入位置不合法!\n"); return 0; } if (L->length == MAX_SIZE) { printf("顺序表已满,无法插入!\n"); return 0; } for (int i = L->length; i >= pos; i--) { L->data[i] = L->data[i-1]; // 向后移动元素 } L->data[pos-1] = book; L->length++; return 1; } // 根据位置取值 Book Get(SeqList L, int pos) { if (pos < 1 || pos > L.length) { printf("取值位置不合法!\n"); exit(0); } return L.data[pos-1]; } // 根据关键字查找 int Find(SeqList L, char* name) { for (int i = 0; i < L.length; i++) { if (strcmp(L.data[i].name, name) == 0) { return i+1; } } return 0; } // 删除元素 int Delete(SeqList* L, int pos) { if (pos < 1 || pos > L->length) { printf("删除位置不合法!\n"); return 0; } for (int i = pos-1; i < L->length-1; i++) { L->data[i] = L->data[i+1]; // 向前移动元素 } L->length--; return 1; } // 测试 int main() { SeqList L; InitList(&L); Book book1 = {"C语言程序设计", "谭浩强", "清华大学出版社", 36.5}; Insert(&L, 1, book1); printf("插入书籍:%s\n", book1.name); Book book2 = {"数据结构与算法分析", "Mark Allen Weiss", "机械工业出版社", 98.0}; Insert(&L, 2, book2); printf("插入书籍:%s\n", book2.name); Book book3 = {"算法导论", "Thomas H. Cormen", "机械工业出版社", 99.0}; Insert(&L, 3, book3); printf("插入书籍:%s\n", book3.name); int pos = 2; Book book = Get(L, pos); printf("获取第%d本书:%s\n", pos, book.name); char* name = "数据结构与算法分析"; int index = Find(L, name); if (index == 0) { printf("未找到书籍:%s\n", name); } else { printf("查找到书籍:%s,位置为:%d\n", name, index); } pos = 1; Delete(&L, pos); printf("删除第%d本书\n", pos); return 0; } ``` 在上面的代码中,我们定义了一个`Book`结构体,表示图书信息,包括书名、作者、出版社和价格四个字段。然后定义了一个`SeqList`结构体,表示顺序表,包括一个数组和当前长度两个字段。 在`InitList`函数中,我们将当前长度设为0,表示顺序表为空。 在`Insert`函数中,我们首先判断插入位置是否合法,如果不合法则返回0。然后判断顺序表是否已满,如果已满则返回0。接着从后往前遍历,将元素向后移动一位,腾出位置给新元素,最后将新元素插入到指定位置。插入成功后,当前长度加1,并返回1表示插入成功。 在`Get`函数中,我们首先判断取值位置是否合法,如果不合法则退出程序。否则,直接返回该位置上的元素。 在`Find`函数中,我们从前往后遍历顺序表查找指定书名的书籍,如果找到则返回该书籍在顺序表中的位置,否则返回0表示未找到。 在`Delete`函数中,我们首先判断删除位置是否合法,如果不合法则返回0。然后从指定位置开始,将元素向前移动一位,覆盖要删除的元素。最后当前长度减1,并返回1表示删除成功。 在`main`函数中,我们首先初始化顺序表,然后插入三本书籍,分别输出插入的书名。接着获取第二本书籍,输出其书名。再根据关键字查找书籍,输出查找结果。最后删除第一本书籍,输出删除成功信息。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值