zoukankan      html  css  js  c++  java
  • 顺序表实现代码

    #include<iostream>
    using namespace std;


    typedef int ElemType; const int N = 80; struct SqList { ElemType *elem; int length; void Init(); void Clear(); int Locate(ElemType e); void Insertbefore(int i, ElemType e); void Delete(int i, ElemType &e); bool Empty(); void Travese(); }; int main() { SqList L; L.Init(); int n; cin >> n; for (int i = 0; i < n; i++) { ElemType t; cin >> t; L.Insertbefore(i + 1, t); } L.Travese(); cout<<endl; cout << L.Empty() << endl; ElemType m; cin >> m; cout << L.Locate(m) << endl; L.Delete(3, m);//删除位序为3的元素,放到m里面; cout<<m<<endl; L.Travese(); L.Clear(); } void SqList::Init() { elem = new ElemType[N]; length = 0; } void SqList::Clear() { length = 0; } bool SqList::Empty() { return length==0; } void SqList::Insertbefore(int i, ElemType e) { if (i<1 || i>length + 1) return; for (int j = length - 1; j >= i - 1; j--) { elem[j + 1] = elem[j];//插入位置及其之后元素右移 } elem[i - 1] = e; length++; } void SqList::Delete(int i, ElemType &e) { if (i<1 || i>length) return; e = elem[i - 1]; for (int j = i; j <= length - 1; j++) elem[j - 1] = elem[j]; length--; } void SqList::Travese() { for (int i = 0; i < length; i++) { if (i > 0) cout << " "; cout << elem[i]; } } int SqList::Locate(ElemType e) { int i = 1; while (i <= length && elem[i - 1] != e) { i++; } if (i <=length) return i; else return 0; }
  • 相关阅读:
    设计模式 设计原则 何为设计
    面向对象 多态
    两个简易的对拍程序
    各类有用的神奇网站
    乘法逆元
    树链剖分
    Markdown的用法
    vimrc 的配置
    luogu【P1144】最短路计数
    【娱乐】收录各种神奇知乎问答
  • 原文地址:https://www.cnblogs.com/ilovetheworld/p/10515102.html
Copyright © 2011-2022 走看看