数据结构(c语言版)严蔚敏--顺序表的操作实现

本文档展示了如何使用C语言实现一个顺序表,包括初始化、创建、插入、删除、查找功能,以及相应的菜单驱动程序。
摘要由CSDN通过智能技术生成

本代码主要参考严蔚敏(c语言版)第二版教材和CSDN上的博文完成

此代码为可执行代码,持续修改更新中

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define MaxSize 100
#define ElemType int
#define Status int
#define  MAXSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2 
#define FALSE 0
#define TRUE 1
#define ElemType int
#define Status int
using namespace std;
typedef struct
{
	ElemType *elem;//顺序表元素
	int length;            //顺序表当前长度
}SqList;

//初始化线性表L (参数用引用)
Status InitList_Sq(SqList &L)       //构造一 个空的顺序表L
{
    L.elem=new ElemType[MAXSIZE];   //为顺序表分配空间
    if(!L.elem) exit(OVERFLOW);    //存储分配失败为空值
    L.length=0;				       //空表长度为0
    return OK;
}
//输出线性表 
void PrintList(SqList L){
	for(int i=0;i<L.length;i++)
		cout<<L.elem[i]<<' ';
	cout<<'\n';			
}
bool CreateList_Sq(SqList &L,int n){
	if(n<0||n>MAXSIZE) {
		cout<<"输入数据不合法"<<'\n'; 
		return FALSE;
	}
	else{
		cout<<"请输入"<<n<<"个数(空格隔开)"<<'\n';
		for(int i=0;i<n;i++){
			
			cin>>L.elem[i];
			++L.length;
		}		
	}
	return TRUE;
} 
void Create_Sq(SqList &L){
	cout<<"请输入要插入数据的个数n"<<'\n';
	int n;cin>>n;
	bool flag=CreateList_Sq(L,n);
	if(flag){
	    cout<<"创建成功"<<'\n';
		PrintList(L);
	}else{
		cout<<"创建失败"<<'\n'; 
	}	 
}

//销毁线性表
void DestroyList(SqList &L)
{
  if (L.elem)  delete L.elem;   
 //释放存储空间
}
//清空线性表
void ClearList(SqList &L) 
{
   L.length=0;               
 //将线性表的长度置为0
}

//求线性表的长度
int GetLength(SqList L)
{
   return (L.length);             
}
//判断线性表L是否为空
int IsEmpty(SqList L)
{
  if (L.length==0) return 1;      
   else return 0;
}
//取值(根据位置i获取相应位置数据元素的内容)
int GetElem(SqList L,int i,ElemType &e)
{
  if (i<1||i>L.length) return ERROR;   //判断i值是否合理,若不合理,返回ERROR
  e=L.elem[i-1];   //第i-1的单元存储着第i个数据
  return OK;
}
//查找(根据指定数据获取数据所在的位置)
int LocateELem(SqList L,ElemType e)
{//在线性表L中查找值为e的数据元素,返回其序号(是第几个元素)
  	for (int i=0;i< L.length;i++)
      	if (L.elem[i]==e) return i+1; //查找成功,返回序号       
 	 return 0;                        //查找失败,返回0
}

void search(SqList L){
	cout<<"请输入需要查找的数据"<<'\n';
	int e;cin>>e; 
	int flag =LocateELem(L,e);
	if(flag){
		cout<<"该元素位置为"<<flag<<'\n'; 
	}
	else cout<<"未找到该元素位置"<<'\n'; 
}
//在线性表L中第i个数据元素之前插入数据元素e
Status ListInsert_Sq(SqList &L)
{
   int i; ElemType e;
   cout<<"请输入插入位置i"<<'\n';
   cin>>i;
   if(i<1 || i>L.length+1) {
   	  cout<<"i值不合法"<<'\n'; 
   	  return ERROR;	         //i值不合法
   }
   if(L.length==MAXSIZE) return ERROR;    //当前存储空间已满  
   cout<<"请输入插入数据e"<<'\n';  
   cin>>e; 
   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;
}
//将线性表L中第i个数据元素删除
Status ListDelete_Sq(SqList &L)
{
   cout<<"请输入删除位置i"<<'\n';
   int i;cin>>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;
}

//菜单
void menu()
{
	printf("********1.创建        2.插入*********\n");
	printf("********3.删除        4.查找*********\n");
	printf("********5.输出        6.清空*********\n");
	printf("********9.退出              *********\n");
}
int main()
{
	SqList L; int choice;
	InitList_Sq(L);
	while (1)
	{
		menu();
		printf("请输入菜单序号:\n");
		scanf("%d", &choice);
		if (9 == choice) break;
		switch (choice)
		{
		case 1:Create_Sq(L); break;
		case 2:ListInsert_Sq(L); break;
		case 3:ListDelete_Sq(L); break;
		case 4:search(L); break;
		case 5:PrintList(L); break;
		case 6:ClearList(L); break;
		default:printf("输入错误!!!\n");
		}
	}
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值