顺序表实验

#ifndef SeqStack_H
#define SeqStack_H  
const int StackSize=100;  
template<class DataType>  
    class SeqStack  
    {  
    public:  
        SeqStack();  
        ~SeqStack(){};  
        void Push(DataType x);  
        DataType Pop();  
        DataType GetTop();  
        int Empty();  
        void Decimaltor(int num,int r);  
    private:  
        DataType data[StackSize];  
        int top;  
};  

#endif;

#include<iostream>
using namespace std;
#include"seqstack.h"
template<class DataType>  
SeqStack<DataType>::SeqStack()  
{  
    top=-1;  
}  
  
template<class DataType>  
void SeqStack<DataType>::Push(DataType x)  
{  
    if(top==StackSize-1)throw"上溢";  
    top++;  
    data[top]=x;  
}  
  
template<class DataType>  
DataType SeqStack<DataType>::Pop()  
{  
    DataType x;  
    if(top==-1)throw"下溢";  
    x=data[top--];  
    return x;  
}  
  
template<class DataType>  
DataType SeqStack<DataType>::GetTop()  
{  
    if(top!=-1)  
    return data[top];  
}  
  
template<class DataType>  
int SeqStack<DataType>::Empty()  
{  
    if(top==-1)return 1;  
    else return 0;  

}  

#include <iostream>
using namespace std;
#include "seqstack.cpp"
void main()
{SeqStack<int>S;
if(S.Empty())
cout<<"栈为空"<<endl;
else cout<<"栈非空"<<endl;
cout<<"对9和21执行进栈操作"<<endl;
S.Push(9);
S.Push(21);
cout<<"栈顶元素为:"<<endl;
cout<<S.GetTop()<<endl;
cout<<"执行一次出栈操作:"<<endl;
S.Pop();
cout<<"栈顶元素为:"<<endl;
cout<<S.GetTop()<<endl;

}

#include <iostream>
using namespace std;
#include "seqstack.cpp"
void main()
{SeqStack<int>S;
if(S.Empty())
cout<<"栈为空"<<endl;
else cout<<"栈非空"<<endl;
cout<<"对9和21执行进栈操作"<<endl;
S.Push(9);
S.Push(21);
cout<<"栈顶元素为:"<<endl;
cout<<S.GetTop()<<endl;
cout<<"执行一次出栈操作:"<<endl;
S.Pop();
cout<<"栈顶元素为:"<<endl;

cout<<S.GetTop()<<endl;}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 C 语言顺序表实验示例: ```c #include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 // 定义顺序表的最大长度 typedef int ElemType; // 定义顺序表元素类型 typedef struct { ElemType data[MAXSIZE]; // 顺序表存储空间 int length; // 顺序表当前长度 } SqList; // 顺序表类型定义 // 初始化顺序表函数 void InitList(SqList *L) { L->length = 0; // 初始化顺序表长度为0 } // 插入元素函数 int ListInsert(SqList *L, int i, ElemType e) { int j; if (i < 1 || i > L->length + 1) // 判断插入位置是否非法 { return 0; } if (L->length >= MAXSIZE) // 判断顺序表是否已满 { return 0; } for (j = L->length; j >= i; j--) // 从后往前依次移动元素 { L->data[j] = L->data[j - 1]; } L->data[i - 1] = e; // 插入新元素 L->length++; // 顺序表长度加1 return 1; } // 删除元素函数 int ListDelete(SqList *L, int i, ElemType *e) { int j; if (i < 1 || i > L->length) // 判断删除位置是否非法 { return 0; } *e = L->data[i - 1]; // 将删除元素的值赋给e for (j = i; j < L->length; j++) // 从前往后依次移动元素 { L->data[j - 1] = L->data[j]; } L->length--; // 顺序表长度减1 return 1; } // 输出顺序表函数 void PrintList(SqList L) { int i; for (i = 0; i < L.length; i++) { printf("%d ", L.data[i]); } printf("\n"); } int main() { SqList L; InitList(&L); ListInsert(&L, 1, 1); ListInsert(&L, 2, 2); ListInsert(&L, 3, 3); printf("原顺序表:"); PrintList(L); ListDelete(&L, 2, NULL); printf("删除第二个元素后的顺序表:"); PrintList(L); return 0; } ``` 上述代码实现了顺序表的初始化、插入、删除和输出功能。你可以自行修改其中的元素类型和最大长度,以满足自己的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值