【数据结构】(严蔚敏版)顺序表相关功能的实现及代码

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
using namespace std;
typedef int Status;
typedef int ElemType;
typedef struct{
	ElemType *elem;
	int length;
}SqList;


//线性表初始化
Status Init_List(SqList &L){
	L.elem=new ElemType[MAXSIZE];
	memset(L.elem,0,sizeof(L));
	L.length=0;
	return 0;
}

//菜单
void menu(){
	printf("*********1、创建            2、打印***********\n");
	printf("*********3、插入            4、删除***********\n");
	printf("*********5、查找            6、倒置***********\n");
	printf("*********7、清空            8、退出***********\n");
}



//创建
bool CreateList(SqList &L,int n){
	if(n<0||n>MAXSIZE)
	false;
	int i;
	printf("请输入n个数\n");
	for(i=0;i<n;i++){
		scanf("%d",&L.elem[i]);
		L.length++;
	}
	return true;
}


//打印
Status PrintList(SqList L){
	int i;
	printf("打印当前顺序表中的所有元素:"); 
	for(i=0;i<L.length;i++){
		printf("%d ",L.elem[i]);
	}
	printf("\n");
	return 0;
}


//插入
Status InsertList(SqList &L){
	int m,n,i;
	printf("请输入在线性表中插入的位置和数值:\n");
	cin>>m>>n;
	if(m<1||m>L.length+1){
		printf("插入位置不合格"); 
		return ERROR;
		
	}
	for(i=L.length-1;i>=m-1;i--){
		L.elem[i+1]=L.elem[i];
	}
	L.elem[m-1]=n;
	L.length++;
	return OK;
}


//删除
Status DeleteList(SqList &L){
	int m,i;
	printf("请输入要删除第几个元素\n");
	cin>>m;
	for(i=m-1;i<=L.length-2;i++){
		L.elem[i]=L.elem[i+1];
	} 
	L.length--;
	return OK;
	
}


//查找
Status SearchList(SqList L){
	int m;
	printf("请输入你要查找的数值为:\n");
	cin>>m;
	int i;
	for(i=0;i<=L.length-1;i++){
		if(L.elem[i]==m){
			printf("所删除的元素的位置为:%d \n",i+1);
			break;
		}
	}
	return OK;
}



//倒置
Status ReverseList(SqList L){
	sort(L.elem,L.elem+L.length,greater<int>());
	return OK;
}



/*Status SplitSort(SqList L){
	    int i,j;
	    i=0;
	    j=2;
		while(j>=L.length){
			if(L.elem[i]>L.elem[j]){
				int t;
				t=L.elem[j];
				L.elem[j]=L.elem[i];
				L.elem[i]=t;
			    i=i+2;
			    j=j+2;
		    }
		}
		int m,n;
		m=1;
		n=3;
		while(n>=L.length){
		    if(L.elem[m]>L.elem[n]){
			    int temp;
		     	temp=L.elem[m];
		    	L.elem[m]=L.elem[n];
		     	L.elem[n]=temp;
		     	m=m+2;
		     	n=n+2;
		    }
		}
	return OK;
}*/


//清空
void ClearList(SqList &L){
	L.length=0;
}


int main(){
	SqList L;
	Init_List(L);
	int choice;
	while(1){
		menu();
		printf("请输入菜单序号:\n");
		cin>>choice;
		if(choice==8) break;
	    switch(choice){
		    case 1:
	            int n;
	            printf("请输入你要创建的顺序表的长度\n");
       	        cin>>n;
	            CreateList(L,n);
	            printf("创建成功!!!\n");
	            //PrintList(L);
	            break;
	        case 2:
	            //InsertList(L);
	            PrintList(L);
	            break;
	        case 3:
	    	    InsertList(L);
	         	break;
	        case 4:	
	            DeleteList(L);
            //	PrintList(L);
                break;
            case 5:	
	            SearchList(L);
	            break;
	        case 6:
	            ReverseList(L);
	            //PrintList(L);
	            break;
	        case 7:
	    	    ClearList(L);
	    	    break;
	        default:
	    	    printf("输入错误!!!\n");
        }
    }
	//SplitSort(L);
	//PrintList(L);
		
	return 0;
}

写代码心得

1、头文件

#include<cstdio>是c++为了引用c语言里面的scanf、printf函数库#include<stdio.h>,为了区别于c++自身的函数库,所以去掉.h,在前面加上‘c’。类似的有#include<cstring>、#include<cmath>、#include<cstdlib>(其中封装了许多函数,例如free、malloc这些动态分配内存函数)等

而c++里面的cin>>、cout<<需要包含在#include<iostream>头文件中。

#include<cstring>头文件所包含的常用函数

1、strcpy 函数

  • 原型: char* strcpy (char *s1, const char *s2);
  • 作用: 将字符串 2 复制到字符数组 1 当中去
  • 说明:
    • 字符数组 1 的长度应不小于字符串2的长度
    • "字符数组 1" 必须写成数组名形式,"字符串 2" 可以为字符数组名,也可以是一个字符串常量
    • 在未对字符数组 1 赋初值时,复制时将 "字符串 2" 中的字符串和其后的 "/0" 一起复制到字符数组 1 中,取代其前 n+1 个字符,而后面的字符是 "字符数组 1" 原字符

2、strncpy 函数

  • 原型: char* strncpy (char *s1, const char *s2, size_t len);
  • 作用: 将 s2 的前 len 个字符复制到 s1 中指定的地址, 不加 '\0'

5、strxfrm函数

  • 原型: size_t strxfrm (char *s1, const char *s1, size_t len);
  • 作用: 根据程序当前的区域选项,将 s2 的前 len 个字符(字节)复制到 s1 中指定的地址, 不加 '\0'

6、strcat 函数

  • 原型: char* strcat (char *s1, const char *s2);
  • 作用: 把字符串 2 接到字符串 1 后面(字符串 1 要足够大)
  • 说明: 连接前两个字符串都有 "/0" ,连接时将字符串1后 "/0" 丢弃,只在新字符串后保留 '/0'

7、strncat 函数

  • 原型:char* strncat (char *s1, const char *s2, size_t len);
  • 作用: 将字符串 s2 的前 len 个字符连接到 s1 尾部, 不加 '\0'

8、strcmp 函数

  • 原型: int strcmp (const char *s1, const char *s2); ①作用:比较字符串1与字符串2
  • 规律: 两个字符串自左至右逐个字符相比(按 ASCII 码值大小比较)直到出现不同的字符或者遇到 "/0" 为止,如果全部字符相同,则认为相等,若出现不同字符,则以第一个不相同的字符为准
  • 准则:
    • 如果字符串 1=字符串 2,函数返回值为 0
    • 如果字符串 1>字符串 2,函数返回值为正数
    • 如果字符串1<字符串 2,函数返回值为负数

9、函数 strncmp

  • 原型: int strncmp (const char *s1, const char *s2, size_t len);
  • 作用: 对 s1 和 s2 的前len个字符作比较

10、函数 memcmp

  • 原型: int memcmp (const void *s1, const void *s2, size_t len);
  • 作用: 对 s1 和 s2 的前 len 个字节作比较

11、函数 strcoll

  • 原型: int strcoll (const char *s1, const char *s2);
  • 作用: 根据程序当前的区域选项中的 LC_COLLATE, 比较字符串 s1 和 s2

12、函数 strchr

  • 原型: char* strchr (const char *s, int ch);
  • 作用: 在 s 中查找给定字符 ch 第一次出现的位置

13、函数 memchr

  • 原型: void* memchr (const void *s, int ch, size_t len);
  • 作用: 查找在字符串中最后一次出现字符 ’ch’ 的位置。如果 s 中存在字符 ch,返回出现 ch 的位置的指针;否则返回NULL。

14、函数 strrchr

  • 原型: char* strrchr (const char *s, int ch);
  • 作用: 在串 s 中查找给定字符 ch 最后一次出现的位置, r表示从串尾开始

15、函数 strstr

  • 原型: char* strstr (const char *s1, const char *s2);
  • 作用: 在串 s1 中查找指定字符串 s2 第一次出现的位置

20、函数 strlen

  • 原型: size_t strlen (const char *s);
  • 作用: 它是测试字符串长度的函数,函数的值为字符串中的实际长度(不包括 "/0")

21、函数 memset

  • 原型: void* memset (void *s, int val, size_t len);
  • 作用: 将从 s 开始的 len 个字节置为 val

23、函数 _strlwr

  • 原型: char *_strlwr( char *string );
  • 作用: 把字符串中的大写字母换成小写字母

24、函数 _strupr

  • 原型: char *_strupr( char *string );
  • 作用: 把字符串中的小写字母换成大写字母

#include<cmath>头文件所包含的常用函数

1、double pow(double x,double y);计算x的y次幂

2、double sqrt (double);开平方

3、(1). ceil() 向上取整 

      (2). round() 四舍五入取整, 

      (3). floor() 向下取整 

4、绝对值

      (1). int abs(int ); 求整型的绝对值 

      (2). double fabs (double); 求实型的绝对值 

      (3). double cabs(complex); 求复数的绝对值 

#include<algorithm>头文件

sort函数的使用需要加上头文件#include<algorithm>和using namespace std;

2、线性表动态分配和静态分配

typedef struct{
    int data[MaxSize];
    int length;        //当前长度
}SqList;            //顺序表静态定义
typedef struct{
    int *data;
    int MaxSize;    //最大容量
    int length;        //当前长度
}SeqList;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值