2 顺序表的插入,删除,查找操作(详细)
一 順序表的結構體定義和函數聲明
#include<iostream>
using namespace std;
#define ElemType int? // 自定義
#define Ext 30
// 動態初始化
typedef struct seqList
{
? ? ElemType* data; // 指示動態分配數組的指針
? ? ElemType Maxsize,length; ?// 最大容量 和 當前長度
};
#define Maxsize 10
// 函數聲明
void Init_List(seqList &L); ? // 初始化函數
void Rev_List(seqList L); ?// 遍歷函數
void List_Insert(seqList &L, ElemType f, ElemType a); ?// 在指定位置插入 a 操作
ElemType List_delete(seqList &L, ElemType f); // 在指定位置刪除操作
ElemType List_find(seqList L, ElemType f); // 按序號查找
ElemType List_find2(seqList L, ElemType a); ?// 按值查找
二? 順序表的操作
2.1 順序表的插入操作
void List_Insert(seqList &L, ElemType f, ElemType a); ?// 在指定位置插入 a 操作
void List_Insert(seqList &L, ElemType f,ElemType a)
{
? ? if(f < 1 || f > L.length+1)
? ? {
? ? ? ? printf("插入位置有誤,請重新輸入\n");
? ? ? ? exit(-1);
? ? }
? ? if(f >= Maxsize)
? ? {
? ? ? ? printf("順序表空間已滿\n");
? ? ? ? exit(-1);
? ? }
? ? for(int i = L.length; i >= f; i--)
? ? {
? ? ? ? L.data[i] = L.data[i-1];
? ? }
? ? L.data[f-1] = a;
? ? L.length++;
}
int main()
{? ? ?
? ? ElemType a = 0; // 插入的位置
? ? ElemType b = 0; // 插入的元素
? ? seqList L; ?// 定義了一個順序表
? ? Init_List(L);
? ? Rev_List(L);
? ? printf("\n");
? ? printf("請輸入插入的位置 插入的元素\n");
? ? scanf("%d,%d",&a,&b);
? ? List_Insert(L, a, b);
? ? Rev_List(L);
}?
2.2?插入操作的演示圖片
2.3?順序表的刪除操作
ElemType List_delete(seqList &L, ElemType f); // 在指定位置刪除操作
ElemType List_delete(seqList &L, ElemType f)
{
? ? int a = 0;
? ? a = L.data[f-1]; ? // 查看被刪除的元素
? ? if(f < 1 || f >= L.length)
? ? {
? ? ? ? printf("刪除位置有誤,請重新輸入\n");
? ? ? ? exit(-1);
? ? }
? ? for(int i = f; i < L.length; i++)
? ? {
? ? ? ? L.data[i-1] = L.data[i];
? ? }
? ? L.length--;
? ? return a;
}
int main(void)
{
? ? ElemType c = 0; ?// 刪除的位置
? ? seqList L; ?// 定義了一個順序表
? ? Init_List(L);
? ? Rev_List(L);
? ? printf("\n");
? ??printf("請輸入刪除的位置\n");
? ? scanf("%d",&c);
????int b = List_delete(L,c);
? ? printf("刪除的元素為 %d \n", b);
? ? Rev_List(L);
}
2.4?刪除操作的演示
?
2.5 順序表的按序號查找
ElemType List_find(seqList L, ElemType f)
{
? ? ElemType a = 0;
? ? if(f < 1 || f > L.length)
? ? {
? ? ? ? printf("你輸入的查找位置有誤,請重新輸入\n");
? ? ? ? exit(-1);
? ? }
? ? a = L.data[f-1]; ?// 所查找位置上的值賦給a
? ? return a;
}
int main(void)
{
?? ElemType e = 0; // 查詢的序號
?? seqList L; ?// 定義了一個順序表
?? Init_List(L);
? ?Rev_List(L);
? ?printf("請輸入查詢的序號\n");
? ?scanf("%d",&e);
? ?ElemType f = ?List_find(L,e);
? ?printf("所查找的元素為 %d \n", f);
}
2.6 按序號查找演示結果
?
2.7 順序表按值查找
ElemType List_find2(seqList L, ElemType a)
{
? ? for(int i = 0; i < L.length; i++)
? ? {
? ? ? ? if(L.data[i] == a)
? ? ? ? {
? ? ? ? ? ? return i;
? ? ? ? }
? ? }
? ? return -1;
}
int main()
{
????seqList L; ?// 定義了一個順序表
? ? Init_List(L);
? ? Rev_List(L);
? ? printf("\n");
????printf("請輸入查找的元素\n");
? ??ElemType a = 0;
? ? scanf("%d",&a);
? ? ElemType d = List_find2(L,a);
? ? if(d == -1)
? ? {
? ? ? ? printf("查找失敗,順序表中無當前查找數據\n");
? ? }
? ? else
? ? {
? ? ? ? printf("查找成功 ?查找的數據下標為 %d \n", d);
? ? }
}
2.8 按值查找的結果演示
?
三 總代碼如下
#include<iostream>
using namespace std;
#define ElemType int
#define Ext 30
// 動態初始化
typedef struct seqList
{
? ? ElemType* data; // 指示動態分配數組的指針
? ? ElemType Maxsize,length; ?// 最大容量 和 當前長度
};
#define Maxsize 10
// 函數聲明
void Init_List(seqList &L); ? // 初始化函數
void Rev_List(seqList L); ?// 遍歷函數
void List_Insert(seqList &L, ElemType f, ElemType a); ?// 在指定位置插入 a 操作
ElemType List_delete(seqList &L, ElemType f); // 在指定位置刪除操作
ElemType List_find(seqList L, ElemType f); // 按序號查找
ElemType List_find2(seqList L, ElemType a); ?// 按值查找
int main(void)
{
? ? ElemType a = 0; // 插入的位置
? ? ElemType b = 0; // 插入的元素
? ? ElemType c = 0; ?// 刪除的位置
? ? ElemType e = 0; // 查詢的序號
? ? seqList L; ?// 定義了一個順序表
? ? Init_List(L);
? ? Rev_List(L);
? ? printf("\n");
? ? printf("請輸入插入的位置 插入的元素\n");
? ? scanf("%d,%d",&a,&b);
? ? List_Insert(L, a, b);
? ? Rev_List(L);
? ? printf("請輸入刪除的位置\n");
? ? scanf("%d",&c);
? ? int h = List_delete(L,c);
? ? printf("刪除的元素為 %d \n", h);
? ? Rev_List(L);
? ? printf("請輸入查詢的序號\n");
? ? scanf("%d",&e);
? ? ElemType f = ?List_find(L,e);
? ? printf("所查找的元素為 %d \n", f);
? ? printf("請輸入查找的元素\n");
? ? ElemType g = 0;
? ? scanf("%d",&g);
? ? ElemType d = List_find2(L,g);
? ? if(d == -1)
? ? {
? ? ? ? printf("查找失敗,順序表中無當前查找數據\n");
? ? }
? ? else
? ? {
? ? ? ? printf("查找成功 ?查找的數據下標為 %d \n", d);
? ? }
}
void Init_List(seqList &L)
{
? ? int len = 0; ?// 長度
? ? L.data = (ElemType*)malloc(Maxsize * sizeof(ElemType)); ?// 動態開辟內存
? ? if(L.data == NULL)
? ? {
? ? ? ? printf("開辟空間失敗\n");
? ? ? ? exit(0); ?// 退出程序
? ? }
? ? printf("請輸入順序表的長度\n");
? ? scanf("%d",&(len));
? ? if(len > Maxsize ) ?// len > Maxsize ?內存會不夠 ?程序會崩
? ? {
? ? ? ? int * Newp = ?(int *)malloc(Ext * sizeof(int)); ?// 重新開辟空間
? ? ? ? L.data = Newp;
? ? }
? ? L.length = len;
? ? for(int i = 0; i < len; i++)
? ? {
? ? ? ? printf("第 %d 個元素內容為\n", i+1);
? ? ? ? scanf("%d",&(L.data[i]));
? ? }
}
void Rev_List(seqList L)
{
? ? for(int i = 0; i < L.length; i++)
? ? {
? ? ? ? printf("第 %d 個元素內容為 %d \n", i+1,L.data[i]);
? ? }
}
void List_Insert(seqList &L, ElemType f,ElemType a)
{
? ? if(f < 1 || f > L.length+1)
? ? {
? ? ? ? printf("插入位置有誤,請重新輸入\n");
? ? ? ? exit(-1);
? ? }
? ? if(f >= Maxsize)
? ? {
? ? ? ? printf("順序表空間已滿\n");
? ? ? ? exit(-1);
? ? }
? ? for(int i = L.length; i >= f; i--)
? ? {
? ? ? ? L.data[i] = L.data[i-1];
? ? }
? ? L.data[f-1] = a;
? ? L.length++;
}
ElemType List_delete(seqList &L, ElemType f)
{
? ? int a = 0;
? ? a = L.data[f-1]; ? // 查看被刪除的元素
? ? if(f < 1 || f >= L.length)
? ? {
? ? ? ? printf("刪除位置有誤,請重新輸入\n");
? ? ? ? exit(-1);
? ? }
? ? for(int i = f; i < L.length; i++)
? ? {
? ? ? ? L.data[i-1] = L.data[i];
? ? }
? ? L.length--;
? ? return a;
}
ElemType List_find(seqList L, ElemType f)
{
? ? ElemType a = 0;
? ? if(f < 1 || f > L.length)
? ? {
? ? ? ? printf("你輸入的查找位置有誤,請重新輸入\n");
? ? ? ? exit(-1);
? ? }
? ? a = L.data[f-1]; ?// 所查找位置上的值賦給a
? ? return a;
}
ElemType List_find2(seqList L, ElemType a)
{
? ? for(int i = 0; i < L.length; i++)
? ? {
? ? ? ? if(L.data[i] == a)
? ? ? ? {
? ? ? ? ? ? return i;
? ? ? ? }
? ? }
? ? return -1;
}
?
四 總結
1 順序表的插入和刪除操作是類似的,關于插入和刪除需要判斷插入位置的合法性,其次就是把數組里面的元素移動即可
2 由于順序表是用數組存起來的,所以查找操作很容易想到
?
總結
以上是生活随笔為你收集整理的2 顺序表的插入,删除,查找操作(详细)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue-cli3实现web百度离线地图(
- 下一篇: 提高项目7-太乐