//功能:顺序表的实现
//思路:初始化一个顺序表,如果n > INIT_LIST_SIZE 则重新分配内存空间
//反之,将元素存入顺序表
#include <stdio.h>
#include <malloc.h>
#define INIT_LIST_SIZE 2
#define ERROR 0
#define OK 1
#define LISTINCREMENT 10
#define SIZE sizeof(int)
typedef int ElemType;
typedef int Status;
//顺序表定义
typedef struct{
int length;
int listsize;
ElemType *elem;
}sqList;
//初始化顺序表
sqList initSqList()
{
sqList L;
L.elem = (ElemType *)malloc(sizeof(ElemType) * INIT_LIST_SIZE);
if(!L.elem)
{
printf("顺序表初始化失败!");
}
L.length = 0;
L.listsize = INIT_LIST_SIZE;
return L;
}
//创建顺序表
sqList createSqList(sqList L)
{
int i =0, e;
int *newBase;
printf("请输入顺序表元素(以Ctrl + Z结束):");
while(scanf("%d", &e) != EOF)
{
if(i >= L.listsize)
{
newBase = (ElemType *)realloc(L.elem, SIZE * (L.listsize + LISTINCREMENT));
L.elem = newBase;
L.listsize += LISTINCREMENT;
}
L.elem[i++] = e;
}
L.length = i;
printf("输入的顺序表元素为:");
for(i = 0; i < L.length; i++)
{
printf("%d ", L.elem[i]);
}
printf("\n");
return L;
}
//插入顺序表的值:
sqList InsertValue(sqList L, int location, ElemType e)
{
ElemType *newBase;
//当顺序表已存满时
if(L.length >= L.listsize)
{
newBase = (ElemType *)realloc(L.elem, SIZE * (L.listsize + LISTINCREMENT));
L.elem = newBase;
L.listsize += LISTINCREMENT;
}
ElemType * q = &L.elem[location - 1];
ElemType *p = &L.elem[L.length];
for(; p > q; p--)
{
*p = *(p - 1);
}
*q = e;
++L.length;
return L;
}
//按位置查找值
ElemType GetElem(sqList L, int location)
{
ElemType *e;
e = L.elem + location - 1;
return *e;
}
//定位元素,返回第一个匹配的位置
int LocateElem(sqList L, ElemType e)
{
for(int i = 0; i < L.length; i++)
{
if(L.elem[i] == e)
return i+1;
}
return 0;
}
//删除顺序表的元素,按位置删除
sqList deleteElemLocation(sqList L, int location)
{
ElemType *p = &L.elem[location - 1];
for(; p < &L.elem[L.length - 1]; p++)
{
*p = *(p + 1);
}
L.length--;
return L;
}
int main()
{
int len;
sqList L = initSqList();
L = createSqList(L);
//插入值
printf("请输入插入值的位置:");
int location;
int element;
scanf("%d", &location);
while(location < 1 || location > L.length)
{
printf("输入位置有误,请重新输入!\n");
scanf("%d", &location);
}
printf("请输入插入的元素:");
scanf("%d", &element);
L = InsertValue(L, location, element);
printf("插入元素后的顺序表为:\n");
for(int i = 0; i < L.length; i++)
{
printf("%d ", L.elem[i]);
}
printf("\n");
//查找元素
printf("请输入查找的位置:");
scanf("%d", &location);
while(location < 1 || location > L.length)
{
printf("输入位置有误,请重新输入!\n");
scanf("%d", &location);
}
printf("第%d个元素为%d\n", location, GetElem(L, location));
printf("请输入查找的元素值:");
scanf("%d", &element);
if(!LocateElem(L, element))
{
printf("不存在此元素!");
}else
{
printf("元素%d在顺序表第%d个位置!\n", element, LocateElem(L, element));
}
//删除元素
printf("请输入删除元素的位置:");
scanf("%d", &location);
while(location < 1 || location > L.length)
{
printf("输入位置有误,请重新输入!\n");
scanf("%d", &location);
}
L = deleteElemLocation(L, location);
printf("删除后的顺序表元素为:\n");
for(i = 0; i < L.length; i++)
{
printf("%d ", L.elem[i]);
}
printf("\n一共有%d个元素\n", L.length);
return 0;
}
分享到:
相关推荐
顺序表 增删改查顺序表 增删改查顺序表 增删改查顺序表 增删改查顺序表 增删改查顺序表 增删改查顺序表 增删改查顺序表 增删改查顺序表 增删改查
本程序的顺序表是利用数组实现的,能对数组元素进行增删改查的基本操作
初学数据结构,顺序表的增删改查自己还不是很熟练,将自己的学习记录在CSDN社区。希望在编程道路的小伙伴共同进步
java语言实现简单的QQ通讯录,具有增删改查功能,无图形化界面,导入eclipse即可使用
数据结构-基于C语言实现线性顺序表的增删改查+排序,合表操作
数据库部分:创建表、增删改查语句的书写等。 前端部分:HTML、CSS、jQuery、JavaScript等。 编码顺序 添加项目需要使用的各种jar包 HTML 页面表单的编写 Apache方法的DBUtils编写 实体类的数据初始化(添加私有...
顺序表的增删改查(5).cpp
顺序表和链表对文件的增删改查.数据库内的存储数据的数据类型之一。
printf("\n\t\t\t* 1-------顺序表初始化 *"); printf("\n\t\t\t* 2-------录入初始数据 *"); printf("\n\t\t\t* 3-------插 入 *"); printf("\n\t\t\t* 4-------删 除 *"); printf("\n\t\t\t* 5-------求...
建立一个有序的顺序表,并实现下列操作 1,把x插入表中并保持有序 2.查找值为x的元素,若找到将其删除 3.输出表中个元素的值 用数据结构(c语言实现)
用C++编写的顺序表基本操作 #include "stdafx.h" #include "iostream" using namespace System; using namespace std; const int Max=10; class seqList {int data[Max]; int i,last; public: seqList(int x[],int...
本教程是一个0基础创建一个基于三层架构的JavaWeb项目,从创建项目到实现对数据库表数据进行增删改查操作。本教程侧重于培养编程习惯,详细讲解了三层架构的编写顺序,讲解了如何快速编写一整个,从头至尾的详细...
适用于考研初试数据结构顺序表,链表,队列,栈,串,二叉树基础操作的代码练习,里面有实现函数和注释,可以通过运行程序来加深对数据结构的理解。顺序表包含静态存储和动态存储的结构体定义,初始化,插入和删除...
创建链表《数据库》 1,链表可以进行增、删、改、查。 2,多个链表可以进行合并。 3,运用了索引,二叉树,平衡二叉树等。
数据库部分:创建表、增删改查语句的书写等。 前端部分:HTML、CSS、jQuery、JavaScript等。 编码顺序 添加项目需要使用的各种jar包 HTML 页面表单的编写 Apache方法的DBUtils编写 实体类的数据初始化(添加私有...
线性表顺序表源代码 将表格以类定义。实现了插入,删除,读取,遍历,归并的基本操作。
链表顺序表的增删改查
线性表之顺序表常见操作 增删改查,逆转等
顺序表和数组对比: 数组: 长度初始化后就不能更改; 顺序表: 可以随便增删改查, 数组的增强版, 对数组进一步封装. 使用C/C++代码 实现数据结构-顺序表, 并顺序表完成满天星空案例
顺序表的增删改查