一 线性表
线性表
组成
顺序存储结构:数组
链式存储结构:链表
单链表
静态链表
循环链表
双向链表
操作
1 初始化
2 增
3 删
4 改
5 遍历
6 取出
顺序存储结构-数组:
#include <iostream>using namespace std;typedef struct {int *p;int data;
}L;
void Make(L &l,int *a,int n)//创建数组
{l.data = n;l.p = new int [20];for(int i = 0;i < n;i++)l.p[i] = a[i];
}void Read(L &l)//遍历
{for(int i = 0;i < l.data;i++)cout << l.p[i] << " ";cout<<endl;
}
bool Add(L &l,int loc,int num)//增加数据
{if(loc < 0 || loc > l.data)//特殊条件 {cout << "out of boundary" << endl;return false;}for(int j = l.data - 1;j >= loc - 1;j --)//移动 {l.p[j + 1] = l.p[j];}l.p[loc - 1] = num;l.data++;//数组数据个数增加 return true;
} bool Delete(L &l,int loc)//删除数据
{if(loc < 0 || loc > l.data)//特殊条件 {cout << "out of boundary" << endl;return false;}for(int i = loc;i < l.data ;i++)//移动位置 {l.p[i - 1] = l.p[i];}l.data--;//数组数据个数减少 return true;
} void Change(L &l,int loc,int num)
{if(loc < 0 || loc > l.data)//特殊条件 {cout << "out of boundary" << endl;return false;}l.p[loc - 1] = num;return true;
} void Destroy(L &l)//销毁数组
{delete [] l.p;
}
int main()
{int n;cin >> n;int i,a[n];for(i = 0;i < n;i++)cin >> a[i];L l;Make(l,a,n);//初始化 Read(l);int m,p;cout<<"add location and data:"<<endl;cin >> m >> p; Add(l,m,p);//增加 Read(l);int loc;cout<<"What do you want to delete? Please input it's location:'"<<endl;cin>>loc;Delete(l,loc);//删除 Read(l);int loca,num;cout<<"What do you want to change?input the location and data:"<<endl;cin>>loca>>num;Change(l,loca,num);//改变 Read(l);Destroy(l);//销毁 return 0;
}