数据结构——C++工程实现线性表

实验一线性表(字符串分类统计)
定义和声明一个线性表ADT
基于顺序表或者链表实现线性表ADT(具体采用顺序表或者哪种链表可自行选择)
输入一行字符,存入线性表中,要求编写测试程序,能统计出其中英文字母、数字、其他字符的个数,并输出删除所有数字后得到的新的字符串。
包含两行:
第一行:统计值,包括英文字母、数字、其他字符的个数,依次输出,数据和数据之间使用空格隔开
第二行:输出删除数字后的新字符串

//main.cpp
#include<iostream>
#include<cstring>
#include "list.h"
#include "list.cpp"
using namespace std;
string str;
int main()
{
    SeqList<char> p;
    getline(cin,str);
    int l=str.length();
    for(int i=0;i<l;i++)
    p.Insert(i+1,str[i]);
    p.PrintList();
    cout<<p.Length2()<<" "<<p.Length1()<<" "<<p.Length3()<<endl;
    for(int i=0,j=0;j<p.Length1();i++)
    {
    	if(str[i]>='0'&&str[i]<='9')
    	{
		p.Delete(i-j+1);j++;
		}
	} 
    p.PrintList();
    return 0;
}
//LIST.H
#ifndef __LIST_H__
#define __LIST_H__
#define MaxSize 1000
using namespace std;
template <class DataType>
class SeqList
{
public:
    SeqList(){length=0;length_num=0;length_word=0;length_fuhao=0;}            
    SeqList(DataType a[],int n);    
    ~SeqList(){}                    
    int Length(){return length;}  
	int Length1(){return length_num;}  
	int Length2(){return length_word;}
	int Length3(){return length_fuhao;}
    DataType Get(int i);            
    int Locate(DataType x);         
    void Insert(int i,DataType x);  
    DataType Delete(int i);         
    void PrintList();               
private:
    DataType data[MaxSize];         
    int length;
	int length_num;
	int length_word;
	int length_fuhao;                     
};
#endif 
//LIST.CPP
#include <iostream>
#include "list.h"

using namespace std;
template <class DataType>
SeqList<DataType>::SeqList(DataType a[],int n)
{
    if(n>MaxSize) throw "wrong parameter";
    for(int i=0;i<n;i++)
        data[i]=a[i];
    length=n;
}

template <class DataType>
DataType SeqList<DataType>::Get(int i)
{
    if(i<1 && i>length) throw "wrong Location";
    else return data[i-1];
}

template <class DataType>
int SeqList<DataType>::Locate(DataType x)
{
    for(int i=0;i<length;i++)
        if(data[i]==x) return i+1;
    return 0;
}

template <class DataType>
void SeqList<DataType>::Insert(int i,DataType x)
{
    if(length>=MaxSize) throw "Overflow";
    if(i<1 || i>length+1) throw "Location";
    for(int j=length;j>=i;j--)
        data[j]=data[j-1];
    data[i-1]=x;
    if(x>='0'&&x<='9') {
	length_num++;}
    else if((x>='a'&&x<='z')||(x>='A'&&x<='Z')) length_word++;
    else length_fuhao++;
    length++;
}

template <class DataType>
DataType SeqList<DataType>::Delete(int i)
{
    char x;
    if(length==0) throw "Underflow";
    if(i<1 || i>length) throw "Location";
    x = data[i-1];
    for(int j=i;j<length;j++)
        data[j-1] = data[j];
    length--;
    return x;
}

template <class DataType>
void SeqList<DataType>::PrintList()
{
    for(int i=0;i<length;i++)
        cout<<data[i];
		cout<<endl;
}

在这道题里面定义的许多基本操作都没有用到,仅做模板提供。题目思想简单,但在实现线性表时思路有些乱;代码比较简单,可以继续精简。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值