数据结构:顺序表的基本操作

顺序表作业:

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef int T;
class SeqList{
private:
    T *data;
    int MaxSize; ///顺序表最多可以存放的元素个数。
    int last; ///顺序表最后一个元素的下标,初始值为-1。
public:
    SeqList(int sz);
    void Input();///首先输入元素的个数,然后顺次输入元素的值。
    void Output();///输出线性表的所有元素。
    void Insert(const T x, int i );///在线性表中第i个位置插入值为x的元素。
    void Remove ( T x );///从线性表中删除第一个值等于x的元素。
};

SeqList::SeqList(int sz) {
    data = new T[sz];
    MaxSize = sz;
    last = -1;
}
void SeqList::Input() {
    int n;
    cin >> n;
    int value;
    for(int i = 0; i < n; ++i){
        cin >>value;
        *(data+i) = value;
    }
    last=n-1;
}
void SeqList::Output(){
    cout <<"The elements are:" <<endl;
    for(int i = 0; i <= last; ++i){
        cout <<*(data+i) <<endl;
    }
}
void SeqList::Insert(int value, int locate){
    if(last == -1){
        *data = value;
        last = 0;
    }
    else if(locate > last){
        ++last;
        *(data + last) = value;
    }
    else{
        ++last;
        int i;
        for(i = last; i > locate; --i){
            *(data+i) = *(data+i-1);
        }
        *(data+i) = value;
    }
}
void SeqList::Remove(int value){
    int i;--last;
    for(i = 0; i <= last&&*(data+i)!=value; ++i){
        *(data+i) = *(data+i+1);
    }
    for(i = 0; i <= last; ++i){
        *(data+i) = *(data+i+1);
    }
}
int main(){
    SeqList myList(100);
    myList.Input();
    myList.Output ();
    int i;
    for( i=0; i<5; i++)
        myList.Insert(i+10,i);
    myList.Output ();
    for( i=10; i<15; i++)
        myList.Remove(i);
    myList.Output ();
    return 0;
}
时间: 2025-01-18 07:28:18

数据结构:顺序表的基本操作的相关文章

数据结构中线性表的基本操作-合并两个线性表-依照元素升序排列

#include<iostream> #include<stdlib.h> #define LIST_INIT_SIZE 10/*线性表初始长度*/ #define LIST_CREATENT 2/*每次的增量*/ typedef int ElemType; using namespace std; typedef struct SqList/*线性表的数据结构定义*/ { ElemType *elem;/*线性表基址*/ int length;/*当前线性表所含的元素个数*/ i

数据结构中线性表的基本操作-合并两个线性表

#include<iostream> #include<stdlib.h> #define LIST_INIT_SIZE 10/*线性表初始长度*/ #define LIST_CREATENT 2/*每次的增量*/ typedef int ElemType; using namespace std; typedef struct SqList/*线性表的数据结构定义*/ { ElemType *elem;/*线性表基址*/ int length;/*当前线性表所含的元素个数*/ i

hrbust-1545-基础数据结构——顺序表(2)

http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1545 基础数据结构——顺序表(2) Time Limit: 1000 MS Memory Limit: 10240 K Total Submit: 412(165 users) Total Accepted: 188(150 users) Rating:  Special Judge: No Description 在长度为n(n<10

C++ 顺序表的基本操作

顺序表的基本操作: "seqlist.h"头文件 #ifndef SEQLIST_H_INCLUDED #define SEQLIST_H_INCLUDED #include <iostream> #include <stdlib.h> using namespace std; template <class Type> class SeqList { public: SeqList(size_t sz = INIT_SIZE); bool IsFu

8.基本数据结构-顺序表和链表

一.内存 - 计算机的作用:对数据进行存储和运算.首先我们需要知道我们目前使用的计算机都是二进制的计算机,就以为着计算机只可以存储和运算二进制的数据.例如下载好的一部电影,该电影可以存储到计算机中,计算机中存储的是基于二进制的电影数据,然后我们可以通过相关的视频播放软件结合相关的硬件对电影的二进制数据进行相关的运算操作,所产生的结果就是我们可以看到电影的画面和听到音频的声音. - 问题:阐述计算机如何计算1+2的结果? - 阐述:简单理解为,首先可以将1和2输入到计算机中,然后计算机会将1和2转

数据结构----顺序表与单链表(JAVA)

下面为学习顺序表和单链表的一些基本操作函数: 1 public class SeqList<T> extends Object { 2 protected int n; 3 protected Object[] element; 4 5 public SeqList(int length) { 6 this.element = new Object[length]; 7 this.n = 0; 8 } 9 10 public SeqList() { 11 this(64); 12 } 13 1

数据结构——顺序表的实现

/* 线性结构的基本特征: 1. 集合中必存在唯一的一个"第一元素" 2. 集合中必存在唯一的一个"最后元素" 3. 除最后元素之外,均有唯一的后继 4. 除第一元素之外,均有唯一的前驱 对线性表的基本操作: {结构初始化} InitList(&L); //构造一个空的线性表L {结构的销毁} DestroyList(&L); //销毁线性表L {引用型操作} ListEmpty(L); //线性表判空 ListLength(L); //返回线性表长

数据结构顺序表思想以及完整代码实现

本文转载自趣学算法,方便个人学习参考使用 http://blog.csdn.net/rainchxy/article/details/77946835 数据结构 第3讲 顺序表 顺序表是最简单的一种线性结构,逻辑上相邻的数据在计算机内的存储位置也是相邻的,可以快速定位第几个元素,中间不允许有空,所以插入.删除时需要移动大量元素. 顺序表可以分配一段连续的存储空间Maxsize,用elem记录基地址,用length记录实际的元素个数,即顺序表的长度, 结构体的定义: 结构体定义后,如果要定义个顺序

数据结构----顺序表的增和遍历(2018/10/23)

数据结构包含顺序表和链表 顺序表方便改和查 链表便于增删 顺序表的增和遍历: 代码一: 1 using System; 2 namespace deaiWith 3 { 4 class MySeqList<T> 5 { 6 //实际的元素个数 7 private int _flag; 8 //存储的空间 9 private T[] _ints; 10 public void AddItem(T Item) //增加元素到末尾 ,追加 11 { 12 if (_flag >= _ints.